forked from ArRosid/cacti-screnshoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CactiScreenshoot.py
131 lines (109 loc) · 3.73 KB
/
CactiScreenshoot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'''
disini nambahin untuk export ke excel sekaligus
'''
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from PIL import Image
import time
import os
from getpass import getpass
import xlsxwriter
user_login = input("Username: ")
pass_login = getpass()
daftar_site_input = input("Input site list(site1.com,site2.com,site3.com): ")
daftar_site = daftar_site_input.split(",")
print("\n\n")
print("url format: ")
print("no;nama_remote;SID;link_MRTG")
print("\n\n")
while True:
url_file = input("Input url file: ")
urls = open(url_file,"r").readlines()
if len(urls[0].split(";")) == 4:
break
else:
print("Format is wrong, chose another file!")
continue
start_date = input("Input start date (yyyy-mm-dd hh:mm): ")
end_date = input("Input end date (yyyy-mm-dd hh:mm): ")
while True:
try:
folder = input("Input folder name to save the result: ")
os.mkdir(folder)
break
except FileExistsError:
print("Folder exist! Chose another folder!")
continue
file_excel = input("Input excel name: ")
workbook = xlsxwriter.Workbook("{}/{}".format(folder,file_excel))
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
worksheet.set_default_row(112)
worksheet.set_column('E:E', 42)
DRIVER = 'chromedriver'
driver = webdriver.Chrome(DRIVER)
for site in daftar_site:
driver.get('http://{}'.format(site.strip()))
username = driver.find_element_by_name('login_username')
password = driver.find_element_by_name('login_password')
username.send_keys(user_login)
password.send_keys(pass_login)
driver.find_element_by_xpath("//input[@value='Login']").click()
try: #kasih try biar kalau gagal login kan nda ada element date1 dll
date1 = driver.find_element_by_name('date1')
date2 = driver.find_element_by_name('date2')
date1.clear()
date2.clear()
date1.send_keys(start_date)
date2.send_keys(end_date)
try: #try ini artinya jika ada Refresh, kalau nda ada berarti refresh
driver.find_element_by_xpath("//input[@value='Refresh']").click()
except:
driver.find_element_by_xpath("//input[@value='refresh']").click()
continue
except:
continue
for url in urls:
nama_hasil = url.split(";")[2]
url_nya = url.split(";")[3]
if "http" in url_nya:
driver.get(url_nya)
time.sleep(1)
element = driver.find_element_by_class_name('graphimage')
location = element.location
size = element.size
driver.save_screenshot('{}/{}x.png'.format(folder, nama_hasil))
x = location['x']
y = location['y']
width = location['x']+size['width']
height = location['y']+size['height']
imgx = Image.open('{}/{}x.png'.format(folder, nama_hasil))
imgy = imgx.crop((int(x), int(y), int(width), int(height)))
os.remove('{}/{}x.png'.format(folder, nama_hasil))
imgy.save('{}/{}y.png'.format(folder, nama_hasil))
imgz = Image.open('{}/{}y.png'.format(folder, nama_hasil))
img = imgz.resize((297, 165), Image.BICUBIC) #pilihan lain bisa pake ini (NEAREST, BILINEAR, ANTIALIAS)
os.remove('{}/{}y.png'.format(folder, nama_hasil))
img.save('{}/{}.png'.format(folder, nama_hasil))
driver.quit()
print("\n\nInput the picture to excel. Whait a minutes.......")
worksheet.write("A5", "No", bold)
worksheet.write("B5", "Nama Remote", bold)
worksheet.write("C5", "SID", bold)
worksheet.write("D5", "Link MRTG", bold)
worksheet.write("E5", "Capture MRTG", bold)
row = 6
col = 0
for url in urls:
no = url.split(";")[0]
nama_remote = url.split(";")[1]
sid = url.split(";")[2]
link_mrtg = url.split(";")[3]
worksheet.write(row, col, no)
worksheet.write(row, col+1, nama_remote)
worksheet.write(row, col+2, sid)
worksheet.write(row, col+3, link_mrtg)
worksheet.insert_image(row, col+4, "{}/{}.png".format(folder, sid), {'x_scale': 0.5, 'y_scale': 0.6})
row = row + 1
workbook.close()
print("\n\nProgram finished!\n\n")