-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
181 lines (159 loc) · 4.6 KB
/
helper.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#EKO APRILI TRISNO
#EKO APRILITRISNO
import os
import re
import sys
import csv
import math
import json
import time
import urllib
import signal
import shutil
import random
import zipfile
import urllib2
import datetime
import requests
import urlparse
import platform
import mysql.connector
from random import randint
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from config import *
curr_fld = os.path.dirname(os.path.abspath(__file__))
gdata = curr_fld+'/'+gdataname
gdriver = curr_fld+"/"+gdrivername
def check_internet():
while(True):
try:
webUrl = urllib2.urlopen("https://www.youtube.com/watch?v=LpCSeB7mF7Y")
if(webUrl.getcode()==200):
print('[+] Checking Connection | OK')
break
else:
print('[-] Retry Check your internet.')
delay()
except Exception as e:
print('[E] Error check internet: {}'.format(e))
def printo(text, algn='left',fill=False):
if algn == 'left':
print "|{:<50}|".format(text)
elif algn == 'center':
if fill == True:
print "|{:-^50}|".format(text)
else:
print "|{:^50}|".format(text)
def linux_distribution():
try:
return platform.linux_distribution()
except:
return "N/A"
def info_OS():
printo('','center',True)
printo('INFO ({})'.format(platform.system()),'center',True)
print("[+] Python version: %s"%sys.version.split('\n'))
print("[+] dist: %s"%str(platform.dist()))
print("[+] linux_distribution: %s"%str(linux_distribution()))
print("[+] system: %s"%platform.system())
print("[+] machine: %s"%platform.machine())
print("[+] platform: %s"%platform.platform())
print("[+] uname: %s"%str(platform.uname()))
print("[+] version: %s"%platform.version())
print("[+] mac_ver: %s"% str(platform.mac_ver()))
printo('','center',True)
def unzip_file(filename,outfolder):
print('[+] Unpacking driver')
with zipfile.ZipFile(filename, 'r') as zf:
for info in zf.infolist():
zf.extract( info.filename, path=outfolder )
out_path = os.path.join( outfolder, info.filename )
perm = info.external_attr >> 16L
os.chmod( out_path, perm )
def check_driver(name,url):
if not (os.path.isfile(name)):
print('[+] Checking driver')
if not (os.path.isfile('chromedriver.zip')):
print('[+] Downloading driver')
r = requests.get(url, allow_redirects=True)
open('chromedriver.zip', 'wb').write(r.content)
#UNZIP
chromedriver_name = './chromedriver.zip'
unzip_file(chromedriver_name,'./')
os.remove(chromedriver_name)
else:
print('[+] Driver | OK')
def init_driver():
ostype = platform.system()
print('[+] Checking Driver Type | {}'.format(ostype))
if (ostype == "Darwin"):
os.system('pkill chromedriver')
os.system('pkill Google Chrome')
url = mac
gdriver = curr_fld+'/chromedriver'
elif (ostype == 'Linux' ):
url = linux
gdriver = curr_fld+'/chromedriver'
elif (ostype == "Windows"):
url = win
gdriver = curr_fld+'/chromedriver.exe'
else:
print "[-] Initialization Driver Failed"
return False
check_driver(gdriver,url)
print("[+] Initialization Driver OK")
return True
def init_browser(headless=True):
try:
options = webdriver.ChromeOptions()
if headless == True:
options.add_argument("headless")
options.add_argument('user-data-dir={}'.format(gdata)) #Your google chrome data
browser = webdriver.Chrome(gdriver,chrome_options=options)
browser.implicitly_wait(20)
print('[+] Initialization Browser OK')
return browser
except Exception as e:
print('[-] Initialization Browser Failed | E: {}'.format(e))
return False
def delay(delay=0):
extraTime = randint(0,5)
newDelay = extraTime + delay
strDelay= str(datetime.timedelta(seconds=newDelay))
print('[@] Sleep - {}'.format(strDelay))
time.sleep(newDelay)
def goto_URL(browser,url):
print('[+] Open : {}'.format(url))
curr_url = browser.current_url
if curr_url != url:
browser.get(url)
else:
browser.refresh()
new_url = browser.current_url
# print('[+] NOW : {}'.format(new_url))
def run_sql(sql,t='put'):
gmydb=mysql.connector.connect(
host=ghost,
user=guser,
passwd=gpasswd,
database=gdatabase,
port=gport)
mycursor = gmydb.cursor()
try:
mycursor.execute(sql)
if t == 'get':
return mycursor.fetchall()
elif t == 'put':
gmydb.commit()
print("[+] {} Row affected.".format(mycursor.rowcount))
except Exception as e:
print("[-] Error : {}".format(e))
print("[-] Mysql : {}".format(sql))
gmydb.close()