/
main.py
274 lines (232 loc) · 8.94 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
271
272
273
274
#coding=utf-8
import requests, json, datetime, time, re, BeautifulSoup
s = requests.session()
channelID = 0 # INPUT YOUR DEFAULT CHANNEL ID
channels = [] # [{''}]
s = requests.session()
channelID = 0 # INPUT YOUR DEFAULT CHANNEL ID
#channels = [] # [{''}]
botToken = '' # INPUT YOUR BOT TOKEN
# DEFAULT PARAMS - CHANGE THEM IF NEEDED
params = {
'method':0, # 0 for list, 1 for specific currency,
# Method 0 (List)
'limit':20, # number of currencies to scrape
# Starting at a specific position?
'specific_pos':False,
'position':1, # Indicate specific position
# Method 1 (Specific currency)
'name':'bitcoin', # input name of currency (check currency url to get his id) Ex: bitcoin, iota, ripple, etc...
# Mobile syntax alert activated ?
'mobile':{'activated':False,'specific_channel':True,'channel_id':""},
'ico':{'channel_id':''}, # INPUT YOUR 'SPECIFIC' CHANNEL ID FOR ICO ALERT
'news':{'channel_id':'','token':''}, # INPUT YOUR 'SPECIFIC' CHANNEL ID FOR NEWS ALERT ####### REQUIRES A NEWSAPI.ORG API ACCOUNT
}
def log(event):
print(datetime.datetime.now().strftime('%H:%M') + " :: @azerpas@ :: " + str(event))
class CMC():
def __init__(self):
self.s = requests.session()
self.currency = "https://api.coinmarketcap.com/v1/ticker/" # + the currency we want (specific currency)
self.start_position = 1 # which start position
self.limit = 1 # how much currency from start position
self.top = "?limit={}".format(params['limit']) # + how much currencies
self.start = "?start={}&limit={}".format(params['position'],params['limit'])
self.convert = {'active':False,'fiat':'EUR'} # "AUD", "BRL", "CAD", "CHF", "CLP", "CNY", "CZK", "DKK", "EUR", "GBP", "HKD", "HUF", "IDR", "ILS", "INR", "JPY", "KRW", "MXN", "MYR", "NOK", "NZD", "PHP", "PKR", "PLN", "RUB", "SEK", "SGD", "THB", "TRY", "TWD", "ZAR"
def scrape(self,params):
log('Scraping with method: ' + str(params['method']))
if params['method'] == 0:
if params['specific_pos'] == True:
log('Specific position option actived')
rurl = self.currency + self.start
r = self.s.get(rurl)
log('Scraped successfully') if(r.status_code == 200) else log("Can't scrape")
return r.text
else:
rurl = self.currency + self.top
r = self.s.get(rurl)
log('Scraped successfully') if(r.status_code == 200) else log("Can't scrape")
return r.text
else:
rurl = self.currency + params['name']
r = self.s.get(rurl)
log('Scraped successfully') if(r.status_code == 200) else log("Can't scrape")
return r.text
class Discord():
def __init__(self):
self.token = botToken
self.s = requests.session()
self.headers = { "Authorization":"Bot {}".format(self.token),"User-Agent":"azerpas (http://azerpas.io, v1.0)","Content-Type":"application/json", }
self.default_channel = channelID
self.url = "https://discordapp.com/api/channels/{}/messages".format(self.default_channel)
self.url_mobile = "https://discordapp.com/api/channels/{}/messages".format(params['mobile']['channel_id'])
self.url_ico = "https://discordapp.com/api/channels/{}/messages".format(params['ico']['channel_id'])
self.url_news = "https://discordapp.com/api/channels/{}/messages".format(params['news']['channel_id'])
def send(self,content,channel):
log('Sending infos to Discord channel(s)')
# Need to add multiple channel sending
message = json.dumps ( {"content":content} )
r = requests.post(channel, headers = self.headers, data = message)
log('Infos sent successfully') if(r.status_code == 200) else log("Can't send infos")
# Need to add an alert in case if it can't send the infos anymore, like contact channel owner...
def encode(self,content):
ret = content.encode('ascii','ignore')
ret = json.loads(ret)
return ret
def top(self,content):
message = """
-------------------------⏰ """ + datetime.datetime.now().strftime("%H:%M:%S") + """⏰ -------------------------"""
message += """
--------------------------------------------------------------------
Rank | Name | Price | Change(1h) | Change(24h) | Change(7d)
--------------------------------------------------------------------
"""
for i in content:
message += """
{} | {} | {} | {} | {} | {}
""".format(i['rank'],i['symbol'],i['price_usd'][:6]+" $",i['percent_change_1h']+" %",i['percent_change_24h']+" %",i['percent_change_7d']+" %")
message += """
"""
return message
def mobile_notif(self,content):
message = """⏰: {} | """.format(datetime.datetime.now().strftime("%H:%M"))
for i in content:
message += """{} : {}$ | """.format(i['symbol'],i['price_usd'][:6])
message += """
"""
return message
class ico():
def __init__(self):
self.hpages = 3
self.url = "https://icobench.com/icos?filterSort=rating-desc&page="
self.s = requests.session()
self.book = []
self.r = None
def scrape(self):
log("Scraping ICO data...")
for i in range(1,self.hpages+1):
req = self.s.get(self.url+str(i))
soup = BeautifulSoup.BeautifulSoup(req.text)
for i in soup.findAll("div",{"class":"rate color5"}):
current_ico = (i.parent.parent)
if not "ico_data" in str(current_ico):
continue
for z in current_ico.findAll("div"):
if "Start" in z.text and "End" in z.text:
pass
if "Start" in z.text:
pattern = re.compile("Start:(.*)")
start_date = re.findall(pattern,z.text)
if "End" in z.text:
pattern = re.compile("End:(.*)")
end_date = re.findall(pattern,z.text)
for z in current_ico.findAll("a"):
if "href" in str(z):
pattern = re.compile('href="(.*)">')
link = re.findall(pattern,str(z))
title = current_ico.find("a",{"class":"name"})
pattern = re.compile("""href="/ico/(.*)">""")
title = re.findall(pattern,str(title))
title = title[0].replace('-',' ').title()
for z in current_ico.findAll("div",{"class":"content"}):
for p in z.findAll("p"):
description = p.text
try:
sep = "KYC"
description = description.split(sep,1)[0]
except Exception as e:
pass
rating = float(i.text)
start_date = start_date[0]
end_date = end_date[0]
link = "https://icobench.com" + link[0]
self.book.append({'title':title,'start':start_date,'end':end_date,'description':description,'link':link,'rating':rating})
maxi = self.book[0]['rating']
self.r = {'weekWinner':self.book[0]['title'],'rate':maxi}
for i in range(0,len(self.book)):
if self.book[i]['rating'] > maxi:
maxi = self.book[i]['rating']
self.r['rate'] = maxi
self.r['weekWinner'] = self.book[i]['title']
log("Scraped successfully")
def syntax(self):
message = "📈 Meilleure ICO de ces 4 derniers jours: " + self.r['weekWinner'] + " avec un score de: " + str(self.r['rate']) + "/5\n\n"
cutting = 0
for i in range(0,len(self.book)):
cutting += 1
message += "\nNom: {}\nDescription: {}\nDébut: {}\nFin: {}\nLien: {}\nNote: {}\n----------------\n".format(self.book[i]['title'],self.book[i]['description'],self.book[i]['start'],self.book[i]['end'],self.book[i]['link'],self.book[i]['rating'])
if (cutting % 3) == 0:
message += "+++"
# adapted to ICObench syntax
return message
class news():
def __init__(self):
self.s = requests.session()
self.token = params['news']['token']
self.url = "https://newsapi.org/v2/top-headlines?sources=crypto-coins-news&apiKey="
self.req = ""
self.paper = []
def scrape(self):
self.req = json.loads(self.s.get(url+token).text)
message = []
for i in self.req['articles']:
message.append({'title':i['title'],'url':i['url']})
self.paper = message
def syntax(self):
message = []
for i in self.paper:
message.append("\n\n{}\nLien: {}\n----------------\n".format(i['title'],i['url']))
return message
if __name__ == "__main__":
CMC = CMC()
D = Discord()
I = ico()
N = news()
count = 0
countN = 0
while True:
r = CMC.scrape(params)
#D.send("TESTING")
r = D.encode(r)
TOPmessage = D.top(r)
MOBmessage = D.mobile_notif(r)
D.send(TOPmessage,D.url)
#### NEWS ####
if countN == 0:
N.scrape()
D.send("📝 " + datetime.datetime.now().strftime("%A %d %B") + " 📝" + "\n📝 LAST NEWS 📝\n")
NEWSmessage = N.syntax()
for i in NEWSmessage:
D.send(i,D.url_news)
if countN == 72:
N.scrape()
D.send("📝 " + datetime.datetime.now().strftime("%A %d %B") + " 📝" + "\n📝 LAST NEWS 📝\n")
NEWSmessage = N.syntax()
for i in NEWSmessage:
D.send(i,D.url_news)
countN = 1
countN += 1
#### NEWS ####
#### ICO ####
if count == 0:
I.scrape()
ICOmessage = I.syntax()
ICOmessage = ICOmessage.split("+++")
D.send("📍ICO recap📍",D.url_ico)
for i in ICOmessage:
D.send(i,D.url_ico)
if count == 288: # 345600 (4 days) / 1200 = 288 / Each 4 days -> alert
I.book = []
ICOmessage = I.syntax()
ICOmessage = ICOmessage.split("+++")
D.send("📍ICO recap📍",D.url_ico)
for i in ICOmessage:
D.send(i,D.url_ico)
count = 1
#### ICO ####
count += 1
I.book = [] # need to be changed
if params['mobile']['activated'] == True:
print("")
D.send(MOBmessage,D.url_mobile)
time.sleep(1200)