-
Notifications
You must be signed in to change notification settings - Fork 5
/
mydealz.py
316 lines (270 loc) · 11.2 KB
/
mydealz.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/python
# coding=utf-8
'''
The MIT License (MIT)
Copyright (c) 2015 Roy Freytag
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
import datetime
import json
import os
import re
import requests
import sys
import telebot
import threading
import time
import traceback
from bs4 import BeautifulSoup as bs
from contextlib import suppress
from colorama import init, Fore, Back, Style
from emoji import emojize
from pyshorteners import Shortener
from threading import Thread
# Emoji definitions
wave = emojize(":wave:", use_aliases=True)
hot = emojize(":fire:", use_aliases=True)
free = emojize(":free:", use_aliases=True)
wish = emojize(":star:", use_aliases=True)
# Basic stuff
os.chdir(os.path.dirname(os.path.realpath(__file__)))
init(autoreset=True) # Colorama
shortener = Shortener("Isgd")
header = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36 OPR/55.0.2994.61"}
# Get settings from file
def get_settings():
global debug_mode; global short_url; global telegram
global sleep_time; global tg_token; global tg_token_priority
global tg_cid; global tg_cid2
debug_mode = 0
short_url = 0
telegram = 0
settings = {}
exec(open("./settings.txt").read(), None, settings)
if settings["debug_mode"]:
debug_mode = 1
if settings["short_url"]:
short_url = 1
if settings["telegram"]:
telegram = 1
sleep_time = settings["sleep_time"]
tg_token = settings["tg_token"]
tg_token_priority = settings["tg_token_priority"]
tg_cid = settings["tg_cid"]
tg_cid2 = settings["tg_cid2"]
get_settings()
# Debug mode
def debug(text):
if debug_mode:
print(Fore.YELLOW + "DEBUG: " + text)
return 0
# Get already found deals from file
def get_found():
global found_deals; global found_deals2
found_deals = [line.rstrip("\n") for line in open ("./found_{}.txt".format(tg_cid))]
found_deals2 = [line.rstrip("\n") for line in open ("./found_{}.txt".format(tg_cid2))]
# Get wanted articles from file
def get_wanted():
global wanted_articles; global wanted_articles2
wanted_articles = [line.rstrip("\n") for line in open ("./wanted_{}.txt".format(tg_cid))]
print(Fore.CYAN + "User 1: Suche nach Deals fuer: " + str(wanted_articles).replace("[", "").replace("]", ""))
wanted_articles2 = [line.rstrip("\n") for line in open ("./wanted_{}.txt".format(tg_cid2))]
print(Fore.CYAN + "User 2: Suche nach Deals fuer: " + str(wanted_articles2).replace("[", "").replace("]", ""))
# Link processing
def process_link(link):
try:
proc_link = shortener.short(link)
except:
print("Shortener-Service nicht erreichbar. Verwende vollen Link.")
proc_link = link
return proc_link
# Telegram bot
bot = telebot.TeleBot(tg_token)
bot_priority = telebot.TeleBot(tg_token_priority)
@bot.message_handler(commands=["hello"])
def hello(msg):
cid = msg.chat.id
bot.send_message(cid, "Hi! " + wave + " Ich bin noch da, keine Sorge.")
@bot.message_handler(commands=["add"])
def add_item(msg):
cid = msg.chat.id
with open("./wanted_{}.txt".format(cid), "a") as f:
f.write(msg.text.replace("/add ", "") + "\n")
bot.send_message(cid, "Schlagwort wurde der Liste hinzugefügt.")
@bot.message_handler(commands=["remove"])
def remove_item(msg):
cid = msg.chat.id
with open("./wanted_{}.txt".format(cid), "r") as list:
lines = list.readlines()
with open("./wanted_{}.txt".format(cid), "w") as remove:
for line in lines:
if line.lower() != (msg.text.replace("/remove ", "") + "\n").lower():
remove.write(line)
bot.send_message(cid, "Schlagwort wurde von der Liste entfernt.")
@bot.message_handler(commands=["reset"])
def reset_found(msg):
cid = msg.chat.id
open("./found_{}.txt".format(cid), "w").close()
bot.send_message(cid, "Liste der gefundenen Deals wurde geleert.")
get_found()
@bot.message_handler(commands=["list"])
def list_items(msg):
cid = msg.chat.id
with open("./wanted_{}.txt".format(cid), "r") as list:
lines = list.readlines()
bot.send_message(cid, "Suche nach Deals für: " + str(lines).replace("[", "").replace("]", "")) # fix \n
def telegram_bot():
while True:
try:
bot.polling(none_stop=True)
except:
debug(traceback.format_exc())
time.sleep(5)
# Scraping routine
def scrape(url, type):
try:
#debug("Scraping " + type + " deals")
site = requests.get(url, headers=header, timeout=20)
soup = bs(site.content, "lxml")
debug("Request completed")
listings = soup.find_all("article", {"id":re.compile("thread_.*")})
if listings is None:
print("Keine Listings gefunden. Seite geändert?")
for thread in listings:
info = thread.find("a", class_="cept-tt thread-link linkPlain thread-title--list")
dealid = thread.attrs["id"]
if dealid in found_deals:
debug("Deal already found " + dealid)
continue
title = info.string.strip()
link = info.get("href")
if short_url:
proc_link = process_link(link)
else:
proc_link = link
# print("[" + type + "] %s: %s" % (re.sub(r"[^\x00-\x7F]+"," ", title), proc_link))
print("[" + "] %s: %s" % (re.sub(r"[^\x00-\x7F]+"," ", title), proc_link))
if telegram:
emoji = free
if type == hot:
emoji = hot
bot.send_message(tg_cid, emoji + " %s: %s" % (title, proc_link), disable_web_page_preview=True)
time.sleep(5)
bot.send_message(tg_cid2, emoji + " %s: %s" % (title, proc_link), disable_web_page_preview=True)
with open("./found_{}.txt".format(tg_cid), "a") as found:
found.write(dealid + "\n")
get_found()
time.sleep(4)
#debug("Scraping " + type + " deals complete")
except:
debug(traceback.format_exc())
time.sleep(60)
# User wanted scraping routine
def scrape_wanted(tg_cid, found_deals, articles, wanted_articles):
for wanted_item in wanted_articles:
deals = articles.find_all("a", string=re.compile("(?i).*("+wanted_item+").*"), class_="cept-tt thread-link linkPlain thread-title--list")
for thread in deals:
dealid = articles.attrs["id"]
if dealid in found_deals:
debug("Deal already found " + dealid)
continue
title = thread.string.strip()
link = thread.get("href")
if short_url:
proc_link = process_link(link)
else:
proc_link = link
print("[WANT] %s: %s" % (re.sub(r"[^\x00-\x7F]+"," ", title), proc_link))
if telegram:
bot_priority.send_message(tg_cid, wish + " %s: %s" % (title, proc_link), disable_web_page_preview=True)
with open("./found_{}.txt".format(tg_cid), "a") as found:
found.write(dealid + "\n")
get_found()
time.sleep(4)
# Hottest deals scraping routine
def scrape_hottest():
try:
debug("Fetching json for hottest deals")
json_url = requests.get("https://www.mydealz.de/widget/hottest?selectedRange=day&threadTypeTranslated=&merchant_name=&merchant_id=&eventId=&groupName=&context=listing", headers=header, timeout=20)
json_data = json_url.json()
debug("Request completed")
for thread in json_data["data"]["threads"]:
title = thread["title"].strip()
link = thread["url"]
if short_url:
proc_link = process_link(link)
else:
proc_link = link
dealid = "hot_" + str(thread["id"])
if dealid in found_deals:
debug("Deal already found " + dealid)
continue
print("[" + "] %s: %s" % (re.sub(r"[^\x00-\x7F]+"," ", title), proc_link))
if telegram:
bot_priority.send_message(tg_cid, hot + " %s: %s" % (title, proc_link), disable_web_page_preview=True)
bot_priority.send_message(tg_cid2, hot + " %s: %s" % (title, proc_link), disable_web_page_preview=True)
time.sleep(5)
with open("./found_{}.txt".format(tg_cid), "a") as found:
found.write(dealid + "\n")
get_found()
time.sleep(4)
debug("Processing hottest deals complete")
except:
debug(traceback.format_exc())
time.sleep(60)
# MyDealz scraper
def mydealz_scraper():
while True:
# Wanted scraper
try:
debug("Scraping for wanted items")
site = requests.get("https://www.mydealz.de/new?page=1", headers=header, timeout=20)
soup = bs(site.content, "lxml")
debug("Request completed")
listings = soup.find_all("article", {"id":re.compile("thread_.*")})
if listings is None:
print("Keine Listings gefunden. Seite geändert?")
for articles in listings:
scrape_wanted(tg_cid, found_deals, articles, wanted_articles)
scrape_wanted(tg_cid2, found_deals2, articles, wanted_articles2)
debug("Scraping for wanted items complete")
except:
debug(traceback.format_exc())
time.sleep(60)
# Hottest today scraper
scrape_hottest()
# Hot deals scraper
scrape("https://www.mydealz.de/hot?page=1", hot)
# Freebie scraper
scrape("https://www.mydealz.de/gruppe/freebies-new?page=1", free)
debug("Now sleeping until next cycle")
time.sleep(sleep_time)
if __name__=="__main__":
# Check for required files
with suppress(Exception):
open("./wanted_{}.txt".format(tg_cid), "x")
with suppress(Exception):
open("./found_{}.txt".format(tg_cid), "x")
with suppress(Exception):
open("./wanted_{}.txt".format(tg_cid2), "x")
with suppress(Exception):
open("./found_{}.txt".format(tg_cid2), "x")
# Initial fetch
get_wanted()
get_found()
Thread(target = telegram_bot).start()
Thread(target = mydealz_scraper).start()