-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
270 lines (216 loc) · 10.6 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
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
import discord
import asyncio
import requests
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import json
with open('settings.json') as data:
settings = json.load(data)
min_upvotes = int(settings["min_upvotes"])
max_upvotes = int(settings["max_upvotes"])
base_url = settings["base_url"]
pages_to_index = int(settings["pages_to_index"])
discord_api_key = settings["discord_api_token"]
min_price = float(settings["min_price"])
max_price = float(settings["max_price"])
channel_id = int(settings["discord_channel_id"])
time_interval_seconds = int(settings["time_interval_seconds"])
class MyClient(discord.Client):
def __init__(self, channel, *args, **kwargs):
self.outOfStock = []
self.checkUrls = []
self.channelID = channel
super().__init__(*args, **kwargs)
# create the background task and run it in the background
self.bg_task = self.loop.create_task(self.my_background_task())
# Check deals
def checkDealsBeautifulSoup(self, url):
# Imports
import requests
from bs4 import BeautifulSoup
import json
from requests.adapters import HTTPAdapter
import random
# Loads JSON and vars
with open('settings.json') as data:
settings = json.load(data)
min_upvotes = int(settings["min_upvotes"])
max_upvotes = int(settings["max_upvotes"])
min_price = float(settings["min_price"])
max_price = float(settings["max_price"])
proxyvar = settings['proxy']
# Loads proxies
with open('proxies.txt', 'r') as proxiesf:
proxies = proxiesf.readlines()
with open('httpsProxy.txt', 'r') as proxieshttps:
httpsProxies = proxieshttps.readlines()
# Picks random proxy
proxy1 = random.choice(proxies)
httpsProxy = random.choice(httpsProxies)
returnMsgs = []
newArray = []
# Reads already used things
with open('data/usedLinks.txt', 'r') as data:
usedArray = data.readlines()
# Sets up proxy
#session = requests.session()
if proxyvar == "True" or proxyvar == True:
proxy = {"http": "http://" + proxy1,
"https": "https://" + httpsProxy}
page = requests.get(url, proxies=proxy)
else:
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
# Tries to get things
listings = soup.find_all(
'article', attrs={'data-handler': 'history'})
upvotes = soup.find_all('span', attrs={'class': 'cept-vote-temp'})
pricing = soup.find_all('span', attrs={'class': 'thread-price'})
urls = soup.find_all(
'a', attrs={'class': 'cept-thread-image-link'})
var = True
if var == True:
upvotesIndex = 0
index = 0
for x in range(0, len(listings)):
try:
try:
upvote = upvotes[upvotesIndex].text.strip().replace(
" ", "").replace("°", "").replace("\n", "")
if "Deal" in upvote or "alerts" in upvote:
upvotesIndex += 1
upvote = upvotes[upvotesIndex].text.strip().replace(
" ", "").replace("°", "").replace("\n", "")
except:
upvote = 0
try:
price = pricing[index].text.strip().replace("£", "")
except:
price = 0
try:
url = urls[index].get('href')
except:
url = None
if price != "FREE":
try:
price = float(price.replace(",", ""))
except:
price = 0
else:
price = 0
if min_price <= price <= max_price:
if min_upvotes <= int(upvote) <= max_upvotes:
if url != None:
if url + "\n" not in usedArray and "/deals/" in url:
# Return Message
message = url + " Satisfies your deal criteria. It is at " + \
str(upvote) + \
" degrees and costs £" + str(price)
# "Apple iPad Air 3 10.5 2019 model, 64 GB 256 GB"
# "https://www.hotukdeals.com/deals/ipad-air-2019-3293151"
# "\n**Price**: $price \n**Temperature**: $upvote"
print('here yeah' + str(x))
if proxyvar == "True" or proxyvar == True:
r = requests.get(url, proxies=proxy)
else:
r = requests.get(url)
soup2 = BeautifulSoup(r.text, 'html.parser')
image = soup2.find('img', attrs={'class': 'thread-image'})
newPrice = float(soup2.find('span', attrs={'class': 'thread-price'}).text.strip().replace("£", ""))
try:
thumbnail = image.get('src')
except:
thumbnail = "https://proxy.duckduckgo.com/iu/?u=https%3A%2F%2Fsitechecker.pro%2Fwp-content%2Fuploads%2F2017%2F12%2F404.png&f=1&nofb=1"
try:
title = image.get('alt')
except:
title = "Error"
if title != None and thumbnail != None:
json = {
"title": title,
"url": url,
"temp": int(upvote),
"price": float(newPrice),
"thumbnail": thumbnail
}
elif title == None and thumbnail != None:
json = {
"title": "Error",
"url": url,
"temp": int(upvote),
"price": float(newPrice),
"thumbnail": thumbnail
}
elif title != None and thumbnail == None:
json = {
"title": title,
"url": url,
"temp": int(upvote),
"price": float(newPrice),
"thumbnail": "https://proxy.duckduckgo.com/iu/?u=http%3A%2F%2Fegyptianstreets.com%2Fwp-content%2Fuploads%2F2017%2F07%2F404.jpg&f=1&nofb=1"
}
elif title == None and thumbnail == None:
json = {
"title": "Error",
"url": url,
"temp": int(upvote),
"price": float(newPrice),
"thumbnail": "https://proxy.duckduckgo.com/iu/?u=http%3A%2F%2Fegyptianstreets.com%2Fwp-content%2Fuploads%2F2017%2F07%2F404.jpg&f=1&nofb=1"
}
returnMsgs.append(json)
usedArray.append(url)
newArray.append(url)
except:
continue
upvotesIndex += 1
index += 1
# Saves new logged files
with open('data/usedLinks.txt', 'a') as fileObj:
for line in newArray:
fileObj.write(line + "\n")
# Returns stuff
return returnMsgs
# On start
async def on_ready(self):
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
# On message
async def on_message(self, message):
if message.author.id == self.user.id:
return
# Background manager
async def my_background_task(self):
from discord import Embed
import discord
await self.wait_until_ready()
channel = self.get_channel(int(channel_id))
while not self.is_closed():
for page in range(0, int(pages_to_index)):
print('checking page ' + str(page))
try:
res = self.checkDealsBeautifulSoup(
base_url + "?page=" + str(page))
except:
res = []
if res != []:
for msg in res:
try:
embed = discord.Embed(
title=msg['title'], url=msg['url'])
embed.set_thumbnail(url=msg['thumbnail'])
embed.add_field(
name="Price", value=msg['price'], inline=True)
embed.add_field(name="Temperature",
value=msg['temp'], inline=True)
# await self.bot.say(embed=embed)
# await self.bot.say(embed=embed)
await channel.send(embed=embed)
except:
print('Failed on')
print(msg)
await asyncio.sleep(int(time_interval_seconds))
# Main
client = MyClient(channel_id)
client.run(discord_api_key)