-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhangoutLinker.py
193 lines (159 loc) · 4.27 KB
/
hangoutLinker.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
182
183
184
185
186
187
188
189
190
191
192
193
# -*- coding: utf-8 -*-
import re
import os
import time
import pickle
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#credentials
email = os.getenv('TELEHANGBOT_GOOGLE_EMAIL')
password = os.getenv('TELEHANGBOT_GOOGLE_PASSWD')
debug = bool(os.getenv('TELEHANGBOT_TELEGRAM_DEBUG') or "False")
timeout_str = os.getenv('TELEHANGBOT_TIMEOUT') or "5"
timeout = int(timeout_str)
selenium_server_address = 'http://localhost:4444/wd/hub'
cookies_file = "cookies.pkl"
driver = None
loggedIn = False
links = 0
def saveCookies():
print("saveCookies")
# driver.get("https://google.com")
# waitForUrl('https://www.google(.+)')
# pickle.dump(driver.get_cookies(), open(cookies_file,"wb"))
def loadCookies():
print("loadCookies")
# try:
# driver.get("https://google.com")
# waitForUrl('https://www.google(.+)')
# if os.path.isfile(cookies_file):
# cookies = pickle.load(open(cookies_file, "rb"))
# for cookie in cookies:
# driver.add_cookie(cookie)
# except:
# pass
def setUpDriver():
"""
init driver and set chrome options
"""
print("setUpDriver")
global driver
try:
driver.close()
except:
pass
#disable mic access request
options = webdriver.ChromeOptions()
options.add_argument("--disable-user-media-security=true")
options.add_argument("--use-fake-ui-for-media-stream")
driver = webdriver.Remote(selenium_server_address, options.to_capabilities())
#set timeout
driver.implicitly_wait(timeout)
def checkLoggedIn():
"""
check on logged in
"""
print("checkLoggedIn")
global driver
global loggedIn
try:
driver.get("https://accounts.google.com")
waitForUrl('https://accounts.google.com(.+)')
elem = driver.find_element_by_name("identifier")
loggedIn = False
except:
loggedIn = True
pass
def login():
"""
login into google account
"""
print("login")
global loggedIn
loggedIn = False
global driver
global loggedIn
loadCookies()
checkLoggedIn()
if loggedIn:
return
#login into google account
driver.get("https://accounts.google.com")
#enter email
setValueAndGo("identifier", email)
#enter password
setValueAndGo("password", password)
# if all is ok should be redirect to myaccount
waitForUrl('https://myaccount.google.com(.+)')
if 'myaccount.google.com' in driver.current_url:
loggedIn = True
else:
#set location if ip was changed and google asks for location
setValueAndGo("answer", "Perm")
checkLoggedIn()
saveCookies()
def setValueAndGo(name, value):
"""
set input value by name and press enter
"""
print("setValueAndGo")
global driver
elem = driver.find_element_by_name(name)
elem.send_keys(value)
elem.send_keys(Keys.RETURN)
# if debug:
# driver.save_screenshot('/shared/screen.png')
def waitForUrl(url):
"""
wait while url loaded
"""
print("waitForUrl")
global driver
regex = re.compile(url)
for _ in range(10):
print(driver.current_url)
if regex.match(driver.current_url):
return True
time.sleep(0.5)
return False
def tryGetLink():
"""
try to get hangouts link
"""
print("tryGetLink")
global driver
global loggedIn
link = "error"
try:
if loggedIn:
driver.get('https://hangouts.google.com/start')
if waitForUrl('https://hangouts.google.com/(hangouts/_|call|calls)/(.+)'):
link = driver.current_url
#leave hangout's page
driver.get('https://google.com')
except Exception as e:
if debug:
link = link + ': ' + str(e)
pass
return link
def getlink():
"""
authorize and get link
"""
print("getlink")
global links
global loggedIn
global driver
links = links + 1
link = tryGetLink()
if 'error' in link:
sepUpAndLogin()
link = getlink()
#restart chrome
elif links > 100 or loggedIn == False:
links = 0
sepUpAndLogin()
return link
def sepUpAndLogin():
setUpDriver()
login()