-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathggn_browsing_website.py
260 lines (211 loc) · 8.57 KB
/
ggn_browsing_website.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import os
import sys
import json
import configparser
import time
import random
import click
import requests
from bs4 import BeautifulSoup
HEADERS = {
'User-Agent': ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/72.0.3626.121 Safari/537.36")
}
BLOCK_KEY = ['#', 'logout', 'ico', 'passkey', 'authkey', 'auth', 'css', 'xml', 'http', 'javascript', 'mailto', 'png', 'jpg', 'gif']
class GGn(object):
def __init__(self, timeout=60):
self.timeout = timeout
dir_path = os.path.dirname(os.path.realpath(__file__))
self.history_path = os.path.join(dir_path, "history.json")
self.config_path = os.path.join(dir_path, "config.ini")
self.cookies_path = os.path.join(dir_path, "ggn.cookies")
self.captcha_path = os.path.join(dir_path, "captcha.jpg")
self.username = None
self.password = None
self.config = configparser.ConfigParser()
self.load_config()
self.s = requests.Session()
self.s.headers.update(HEADERS)
self.cookies = None
self.load_cookies()
if not self.cookies and not self.username:
self.ask_for_account()
self.test_credentials()
self.history = {}
self.load_history()
self.freeleech_torrents = {}
self.smallest_torrents = {}
self.gold = '0'
def ask_for_account(self):
self.username = click.prompt("Username", type=str)
self.password = click.prompt("Password", type=str)
self.config['Account'] = {}
self.config['Account']['username'] = self.username
self.config['Account']['password'] = self.password
self.save_config()
click.echo("-")
def save_config(self):
with open(self.config_path, 'w', encoding='utf-8') as configfile:
self.config.write(configfile)
def load_config(self):
if os.path.isfile(self.config_path):
self.config.read(self.config_path)
self.username = self.config['Account']['username']
self.password = self.config['Account']['password']
def save_cookies(self):
self.cookies = self.s.cookies.get_dict()
with open(self.cookies_path, 'w', encoding='utf-8') as f:
json.dump(self.cookies, f, indent=4, ensure_ascii=False)
def load_cookies(self):
if not os.path.isfile(self.cookies_path):
return
with open(self.cookies_path, 'r', encoding='utf-8') as f:
self.cookies = json.load(f)
self.s.cookies.update(self.cookies)
def clear_cookies(self):
self.s.cookies.clear()
self.cookies = None
try:
os.remove(self.cookies_path)
except OSError:
pass
def save_history(self):
with open(self.history_path, 'w', encoding='utf-8') as f:
json.dump(self.history, f, indent=4)
click.echo(f"Saved {len(self.history)} torrents history")
def load_history(self):
if os.path.isfile(self.history_path):
with open(self.history_path, 'r', encoding='utf-8') as f:
self.history = json.load(f)
#click.echo(f"Loaded {len(self.history)} torrents history")
def test_credentials(self):
url = "https://gazellegames.net"
r = self.s.get(url, allow_redirects=False, timeout=self.timeout)
r.raise_for_status()
if r.status_code == 200:
return
self.clear_cookies()
self.login()
def login(self):
click.echo("Logging in ...")
login_url = "https://gazellegames.net/login.php"
r = self.s.get(login_url, timeout=self.timeout)
r.raise_for_status()
if "nav_userinfo" in r.text:
click.echo("Already logged in")
click.echo("-")
return
if "banned from logging" in r.text:
soup = BeautifulSoup(r.text, 'html.parser')
maincontent_tag = soup.find(id="maincontent")
warning_tags = maincontent_tag.find_all(class_="warning")
click.echo("-")
for warning_tag in warning_tags:
click.echo(warning_tag.get_text().strip())
sys.exit(1)
soup = BeautifulSoup(r.text, 'html.parser')
login_form_tag = soup.find(id="loginform")
captcha_id = login_form_tag.find('input', {'name': 'image'})['value']
captcha_url = login_form_tag.img['src']
answers = []
captcha_answers_tag = login_form_tag.find(id="captcha_answer")
for captcha_answer_tag in captcha_answers_tag.find_all('li'):
answer_id = captcha_answer_tag.input['value']
answer_text = captcha_answer_tag.label.get_text()
answers.append((answer_id, answer_text))
r = self.s.get(captcha_url, timeout=self.timeout)
r.raise_for_status()
with open(self.captcha_path, 'wb') as f:
f.write(r.content)
click.launch(self.captcha_path)
for index, (answer_id, answer_text) in enumerate(answers):
click.echo(f"{index + 1}) {answer_text}")
while True:
choice = click.prompt("Choose captcha answer", type=int)
if choice < 1 or choice > len(answers):
click.echo("Bad choice!")
else:
break
chosen_answer_id, chosen_answer_text = answers[choice - 1]
data = {
'image': captcha_id,
'captcha': chosen_answer_id,
'username': self.username,
'password': self.password,
'keeplogged': "1",
'login': "Login",
}
r = self.s.post(login_url, data=data, timeout=self.timeout)
r.raise_for_status()
if r.url == login_url and "googleauth" in r.text:
authkey = click.prompt("2-factor auth code")
data = {
'act': "authorize",
'login': "Login",
'authkey': authkey,
}
r = self.s.post(login_url, data=data, timeout=self.timeout)
r.raise_for_status()
if r.url == login_url:
click.echo("-")
click.echo("Login failed!")
soup = BeautifulSoup(r.text, 'html.parser')
maincontent_tag = soup.find(id="maincontent")
warning_tags = maincontent_tag.find_all(class_="warning")
for warning_tag in warning_tags:
click.echo(warning_tag.get_text().strip())
click.echo("-")
self.ask_for_account()
return self.login()
self.save_cookies()
click.echo("✔ Successfully logged in!")
click.echo("-")
def browse_website(self, timestop=5):
original_url = "https://gazellegames.net"
url = original_url
while True:
try:
req = self.s.get(url, timeout=self.timeout)
except Exception as result:
req = self.s.get(original_url, timeout=self.timeout)
print('exception detected')
all_link = re.findall(r'href=".*?"', req.text)
userid_list = re.findall(r'var userid = .*?;', req.text)
# if len(userid_list) > 0:
# userid = userid_list[0]
# print(userid)
# userid = userid[13:-1]
# userpage = original_url + '/' + 'user.php?id=' + userid
# print(userpage)
# if url == userpage:
# print('Gold:')
# self.gold = re.findall(r'''<li>Site Usage: <span class="tooltip" title=".*?"''', req.text)
# print(self.gold)
if len(all_link) == 0:
req = self.s.get(original_url, timeout=self.timeout)
all_link = re.findall(r'href=".*?"', req.text)
approve_link = []
for i in all_link:
flag = True
for j in BLOCK_KEY:
if i.find(j) != -1:
flag = False
break
if flag == True:
approve_link.append(i)
rdint = random.randint(0, len(approve_link)-1)
addtion_url = approve_link[rdint][6:-1]
if addtion_url[0] == '/':
url = original_url + addtion_url
else:
url = original_url + '/' + addtion_url
url = url.replace(r'&', '&')
print(url)
timestop = random.randint(0, timestop)
time.sleep(timestop)
if __name__ == "__main__":
ggn = GGn(timeout=60)
ggn.browse_website()