-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
188 lines (153 loc) · 12.5 KB
/
main.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
debug = False
quiet_update = False
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
from bs4 import BeautifulSoup
from win10toast import ToastNotifier
from time import strftime
import requests as r, json, time, webbrowser, os
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
results_file_name = 'hardmob-results.json'
script_dir = os.path.dirname(__file__)
result_file_path = os.path.join(script_dir, results_file_name)
toaster = ToastNotifier()
results = list()
update_seconds_interval = 5
# --------------------------------------------------------------------------------------------------------------------------------------------------------- #
if debug:
print('Initializing.')
print('Interval => {} seconds.'.format(update_seconds_interval))
def scrape():
soup = perform_get_request('https://www.hardmob.com.br/forums/407-Promocoes?s=&pp=30&daysprune=-1&sort=dateline&order=desc')
previous_results = get_previous_values()
threads = soup.select('h3.threadtitle > a.title')
for thread in threads:
thread_id = thread['id'].split('_')[2]
if debug:
print('Thread id: {}'.format(thread_id))
print('Is thread retrieved? {}'.format(thread_id in previous_results))
if thread_id not in previous_results:
previous_results.append(thread_id)
results.append({
"text": thread.text,
"id": thread_id
})
has_new_results = len(results)
if has_new_results:
if not quiet_update:
if debug:
print('Showing new results... Updating {} seconds after showing all results.'.format(update_seconds_interval), end='\n')
else:
pass
for result in results:
print('{} => {}'.format(strftime('%H:%M'), result['text']))
toaster.show_toast('Clique para abrir', result['text'], duration=2, callback_on_click=lambda: open_url(result['id']))
else:
if debug:
print('Dumping retrieved threads to the JSON result file...')
else:
if debug:
print('No new results. Updating after {} seconds...'.format(update_seconds_interval))
else:
pass
if has_new_results:
with open(result_file_path, 'w') as json_file:
json.dump(previous_results, json_file)
if quiet_update:
if debug:
print('Results dumped. Exiting script in {} seconds...'.format(update_seconds_interval))
time.sleep(update_seconds_interval)
exit()
results.clear()
def open_url(thread):
webbrowser.open_new_tab('https://www.hardmob.com.br/threads/{}'.format(thread))
def perform_get_request(url):
s = r.Session()
res = s.get(url)
if debug:
print('Status code => {}'.format(res.status_code))
return BeautifulSoup(res.content, 'html.parser')
def get_previous_values():
prev_results = list()
if debug:
print("Directory: {}".format(script_dir))
print("Final path: {}".format(result_file_path))
try:
with open(result_file_path, 'r') as json_file:
if debug:
print('Found results file. Loading previous results...')
prev_results = json.loads(json_file.read())
except FileNotFoundError:
if debug:
print("Results file not found. Creating file @ script's path...")
with open(result_file_path, 'w') as json_file:
json.dump(results, json_file)
with open(result_file_path, 'r') as json_file:
prev_results = json.loads(json_file.read())
if debug:
print('Previous results => {}'.format(prev_results))
return prev_results
should_scrap = True
if quiet_update:
scrape()
should_scrap = False
while should_scrap:
scrape()
if debug:
print('Sleeping for {} seconds...'.format(update_seconds_interval))
time.sleep(update_seconds_interval)
if debug:
print('Woke up. Retrieving again...')