-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcron.py
More file actions
1289 lines (1090 loc) · 36 KB
/
cron.py
File metadata and controls
1289 lines (1090 loc) · 36 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import requests
import json
import psycopg2
import sys
import CryptoSite.settings as settings
import datetime
import time
import pprint
from pytrends.request import TrendReq
from apscheduler.schedulers.blocking import BlockingScheduler
pytrends = TrendReq(hl='en-US', tz=360)
import praw
import prawcore
import tweet
'''
pip install requests
pip install apscheduler
pip install pytrends
pip install praw
To Test:
$ python -m unittest -v cron
'''
'''
This is the code for updaing the database with new information on
the coins and ICO ran by cronjobs fround in the
CryptoSite/settings.py file.
'''
numCoins = 10; #Top coins from coinMarketCap.com
trackedCoins = [] #coins currently being tracked
mode = []
historyDays = 184 #default is 6 months = 184 days
CMC = "https://api.coinmarketcap.com/v2/"
coinMarketCap = CMC+"ticker/"
cryptoCompare = "https://min-api.cryptocompare.com/data/"
cryptoCompareList = "https://www.cryptocompare.com/api/data/"
icoWatchList = "https://api.icowatchlist.com/public/v1/"
newsapi = "https://newsapi.org/v2/everything?q="
newsapi_keys = ["e9f31afd6dd54e14930244b5f52cdc45",
"7f9ed31f06e7459b8aa3121e437b30d3",
"4ff3432a39664cb0a21e24e63caef9bf",
"2df0257ef60d402c812c70d47c172612",
"8b211b2c69064b05a69b21989ee7e1ef",
"12ecd0af2710410dbb6a8b982cbe1f70",
"1f625b75875340b094da51d2b0c49d1a",
"5e37b20fc557425aaa9ab746931d28a3",
"221fb27d4dec45c09bed936b96e8255b",
"c2052cc3c5da4159a014d7ec2b386028",
"9cf2a84e2b864bae91ad09143833cced",
"acef470a1f0f4adbb8096611667951c2",
"456094da5e2f44198c1f7b0969a6b779",
"ffbcf0cb9b69498a8a37c7795e174bfc",
"adbfbb9684cb49f796edbe10636f2066",
"d8ae5b58e68c49a08d495994d468dee6",
"5795f2d93e5143a5b6168a7239249ab7",
"08b323fc912548428a5252f9d517b13c",
"0ae45a471f5847ff81bf0f9a96805cef",
"b543fcc6eb534eec9bfc0cd7832f91f8",
"1f064fa3aa2b4e9c9fc80b034f7e889a",
]
newsapi_key = 0
'''
A universal function that retrieves the JSON data from each of the
APIs we are using and returns the raw JSON Object.
@params String api
@returns JSON Object
'''
def getAPI(api):
res = requests.get(api)
#res.raise_for_status()
data = res.json()
return data
'''
A helper function to connect to the DB
@return conn
'''
def getConnected():
db_info = settings.DATABASES["default"]
try:
conn = psycopg2.connect("dbname="+db_info["NAME"]+" user="+db_info["USER"]+" host="+db_info["HOST"]+" password="+db_info["PASSWORD"])
except:
print("I am unable to connect to the db")
return conn;
'''
Pulls the list of coins currently being tracked from the database
and puts them in the trackedCoins array
@returns void
'''
def getTrackedCoins():
conn = getConnected()
cur = conn.cursor()
cur.execute("SELECT * FROM cryptocounter_coin")
row = cur.fetchall()
#clear the entire global array for new data
del trackedCoins[:]
for i in range(0, len(row)):
trackedCoins.append(row[i][2])
conn.close()
'''
Scans the coinmarketcap API for the top numCoins and compares it
with the list retrieved from getTrackedCoins(). If a coin is not
found on the current list, add the new coin to both the database
and the the list of searched coins.
@params int numCoins
@returns void
'''
def setTrackedCoins():
getTrackedCoins() #Make sure we have an updated list
trackedLength = len(trackedCoins)
#Use the list of tracked coins and compare with coinmarketcap
conn = getConnected()
cur = conn.cursor()
market = getAPI(coinMarketCap+"?limit="+str(numCoins))["data"]
for i in market.keys():
if(market[i]["symbol"] not in trackedCoins):
#TODO: find true blockchain
cname = market[i]["name"]
ticker = market[i]["symbol"]
bc = market[i]["name"]
terms = "[\""+market[i]["symbol"]+"\",\""+market[i]["name"]+"\"]"
cur.execute("INSERT INTO cryptocounter_coin (coin_name, ticker, block_chain, search_terms) VALUES('{}','{}','{}','{}')".format(cname,ticker,bc,terms))
conn.commit()
getTrackedCoins() #Get updated list
conn.close()
'''
Parse the JSON data from the cryptocompare API and return an array
of current prices for each coin.
@params String[] coinList
@returns Array of coin:price
'''
def parseCurrentPrice(coinList):
coins = ""
for i in range(0, len(coinList)):
if(coinList[i] == "MIOTA"):
coins += "IOT"
else:
coins += coinList[i]
if(i != len(coinList)-1):
coins += ","
data = getAPI(cryptoCompare+"pricemulti?fsyms="+coins+"&tsyms=USD")
market = getAPI(coinMarketCap)["data"] #TODO:fix to include if rank is > 100
prices = []
for key in data.keys():
want = {}
for i in market.keys():
if(key == "IOT" and market[i]["symbol"] == "MIOTA"):
want = market[i]
break
elif(key == market[i]["symbol"]):
want = market[i]
break
price = {}
if(key == "IOT"):
price["ticker"] = "IOTA"
else:
price["ticker"] = key
price["price"] = data[key]["USD"]
price["circ_supply"] = want["circulating_supply"]
price["percent_change"] = want["quotes"]["USD"]["percent_change_24h"]
price["market_cap"] = want["quotes"]["USD"]["market_cap"]
d = time.time()
now = int(d-(d%86400)) #184
price["date"] = now #want["last_updated"]
prices.append(price)
return prices
'''
Parse the JSON data from the cryptocompare API and return an array
of historical data for each coin on date.
@params String[] coinList, int date
@returns Array of coin:price
'''
def parseHistoricalPrice(coinList, date):
prices = []
for i in range(0, len(coinList)):
cList = coinList[i]
if(coinList[i] == "MIOTA"):
cList = "IOT"
data = getAPI(cryptoCompare+"pricehistorical?fsym="+cList+"&tsyms=USD&ts="+str(date))
market = getAPI(coinMarketCap)["data"] #TODO:fix to include if rank is > 100
for key in data.keys():
want = {}
for j in market.keys():
if(key == "IOT" and market[j]["symbol"] == "MIOTA"):
want = market[j]
break
elif(key == market[j]["symbol"]):
want = market[j]
break
price = {}
if(key == "IOT"):
price["ticker"] = "IOTA"
else:
price["ticker"] = key
price["price"] = data[key]["USD"]
price["circ_supply"] = want["circulating_supply"]
price["percent_change"] = -1
price["market_cap"] = want["quotes"]["USD"]["market_cap"]
price["date"] = date
prices.append(price)
#wait some time to prevent abusing API
#ten coins per second
time.sleep(0.1)
#if we have time, update percent_change
return prices
'''
Parse the JSON data from the icowatchlist API and compare it with
the ICO already in the database. Add any ICOs not in our database
to our database.
@returns String[]
'''
def parseICO():
data = getAPI(icoWatchList)
ico_dict = []
for ico in data.keys():
for status in data[ico].keys():
for i in range(0, len(data[ico][status])):
ico_inner = {}
ico_inner["ico_name"] = data[ico][status][i]["name"]
ico_inner["search_terms"] = '["'+data[ico][status][i]["name"]+'"]' #TODO: update this with more terms, ticker if found
ico_inner["start"] = data[ico][status][i]["start_time"]
ico_inner["end"] = data[ico][status][i]["end_time"]
ico_inner["description"] = data[ico][status][i]["description"]
ico_dict.append(ico_inner)
return ico_dict
'''
Retrive twitter count for given name in 24 hours
@params String
@returns int
'''
def getTwitter(name):
print("-> Working on getting twitter info: "+name)
res = tweet.tweet.getTweetCount(name)
return res
'''
Retrive reddit subs for given list
@params String
@returns int
'''
def getRedditSub(list):
reddit = praw.Reddit(client_id='HPokGrej1cnYhg',
client_secret='8afAIRDxAIGlGtrckbnkO0Dklzo',
password='firerock285',
user_agent='cryptocounter by /u/tomand285',
username='tomand285')
res = []
for l in list:
red = {}
red["name"] = l
try:
sub = reddit.subreddit(l)
red["sub"] = sub.subscribers
except (prawcore.NotFound, prawcore.Redirect, prawcore.Forbidden) as e:
red["sub"] = -1
res.append(red)
if(len(res) < 1):
return([{"error":-1}])
else:
return res
'''
Retive google trends info for given name
@params String
@returns JSON
'''
def getGoogleTrends(name):
print("-> Working on getting Google info: "+name)
resRaw = {}
try:
pytrends.build_payload([name], cat=0, timeframe='today 1-m', geo='', gprop='')
resRaw = json.loads(pytrends.interest_over_time().to_json())
except Exception as e:
print("ALERT: Google response came back invalid, try again later")
if(len(resRaw) < 1):
return [-1]
res = resRaw[name]
goog = []
for k in res.keys():
pg = {}
pg["date"] = datetime.datetime.fromtimestamp(int(k)/1000).strftime('%Y-%m-%d %H:%M:%S')
pg["trend"] = res[k]
goog.append(pg)
time.sleep(1) #to prevent error code 429
return goog
'''
Retive Facebook likes for given name
@params String
@returns int
'''
def getFacebook(name):
num = -1
at="EAACEdEose0cBAOiK6VEShZAuTPGZCZBLDFrpm2ulWBPCZBtRnFPHGjMx7PD8YrBOndrn6qglIudy5DpwiVnFIqbG6hU1VWKSJMP2RRCt6hZAwDn8oO5TTn0GXkJcuIthKpJtXRbwWGU2qp4E61e83Wsis8OUpd3sahh0clXrYA6S0TiU03hIMSXojX6JO880ZD"
res = getAPI("https://graph.facebook.com/"+name+"/?fields=fan_count&access_token="+at)
#print(res)
if("fan_count" in res.keys()):
num = res["fan_count"]
return num
'''
Loops through keys and retrives a good link for the news
@params String
@returns String[]
'''
def newsAPIAdv(data):
global newsapi_key
info = getAPI(newsapi+data+"&apiKey="+newsapi_keys[newsapi_key])
ticker = 0
while(info["status"] == "error"):
newsapi_key = (newsapi_key+1)%len(newsapi_keys)
info = getAPI(newsapi+data+"&apiKey="+newsapi_keys[newsapi_key])
ticker += 1
if(ticker > len(newsapi_keys)):
print("ERROR: Ran out of newsapi keys, please add more or wait 6 hours to continue")
sys.exit(1)
return info
'''
Parse the general news of cryptocurrencies and return as array.
@returns int
'''
def parseGeneralNews(terms):
totalResults = 0;
tr = []
for t in terms:
if(t not in tr):
data = newsAPIAdv(t)
totalResults += data["totalResults"]
tr.append(t)
if(len(tr) > 1):
data = newsAPIAdv(" ".join(tr))
totalResults -= data["totalResults"]
return totalResults
'''
Parse the general Twitter of cryptocurrencies and return number of tweets.
@returns int
'''
def parseGeneralTwitter():
terms = ["cryptocurrency", "blockchain"]
num = 0;
for i in range(0,len(terms)):
num += getTwitter(terms[i])
return num
'''
Parse the general reddit of cryptocurrencies and return numbers of subscribers.
@returns int
'''
def parseGeneralReddit():
return getRedditSub(["cryptocurrency"])[0]["sub"]
'''
Parse the general facebook of cryptocurrencies and return number of likes.
@returns int
'''
def parseGeneralFacebook():
return getFacebook("cryptocurrency")
'''
Parse the specific news for the coin.
@returns int[]
'''
def parseCoinNews():
#3*N calls -> 30
conn = getConnected()
cur = conn.cursor()
cur.execute("SELECT * FROM cryptocounter_coin")
row = cur.fetchall()
news_dict = []
for i in range(0, len(row)):
news_inner = {}
news_inner["id"] = row[i][0]
news_inner["ticker"] = row[i][2]
terms = eval(row[i][4])
news_inner["terms"] = terms
totalResults = 0;
tr = []
for t in terms:
if(t not in tr):
data = newsAPIAdv(t)
totalResults += data["totalResults"]
tr.append(t)
if(len(tr) > 1):
data = newsAPIAdv(" ".join(tr))
totalResults -= data["totalResults"]
news_inner["results"] = totalResults;
news_dict.append(news_inner)
return news_dict
'''
Parse the Twitter API for any new information on the coins being
searched and return the array of data.
@params String[] coinList
@returns int
'''
def parseCoinTwitter(coinList):
id = 0
coin = ""
twitter = []
for i in range(0, len(coinList)):
if(coinList[i] == "MIOTA"):
coin = "IOT"
else:
coin = coinList[i]
tweet = {}
data = getAPI(cryptoCompareList+"coinlist/")
id = data["Data"][coin]["Id"]
tweetRaw = getAPI(cryptoCompareList+"socialstats/?id="+id)
tweet["name"] = coin
tweet["statuses"] = 0
if("statuses" in tweetRaw["Data"]["Twitter"]):
tweet["statuses"] = tweetRaw["Data"]["Twitter"]["statuses"]
else:
tweet["statuses"] = -1
twitter.append(tweet)
return twitter
'''
Parse the Reddit data and return it as an array.
@params String[] coinList
@returns int[]
'''
def parseCoinReddit(coinList):
id = 0
coin = ""
reddit = []
for i in range(0, len(coinList)):
if(coinList[i] == "MIOTA"):
coin = "IOT"
else:
coin = coinList[i]
red = {}
data = getAPI(cryptoCompareList+"coinlist/")
id = data["Data"][coin]["Id"]
redRaw = getAPI(cryptoCompareList+"socialstats/?id="+id)
red["name"] = coin
red["subscribers"] = 0
if("subscribers" in redRaw["Data"]["Reddit"]):
red["subscribers"] = redRaw["Data"]["Reddit"]["subscribers"]
else:
red["subscribers"] = -1
reddit.append(red)
return reddit
'''
Parse the facebook like data and return it as an array.
@params String[] coinList
@returns int[]
'''
def parseCoinFacebook(coinList):
id = 0
coin = ""
facebook = []
for i in range(0, len(coinList)):
if(coinList[i] == "MIOTA"):
coin = "IOT"
else:
coin = coinList[i]
fb = {}
data = getAPI(cryptoCompareList+"coinlist/")
id = data["Data"][coin]["Id"]
fbRaw = getAPI(cryptoCompareList+"socialstats/?id="+id)
fb["name"] = coin
fb["likes"] = 0
if("likes" in fbRaw["Data"]["Facebook"]):
fb["likes"] = fbRaw["Data"]["Facebook"]["likes"]
else:
fb["likes"] = -1
facebook.append(fb)
return facebook
'''
Parse the specific news for the ICO.
@params String[][] row
@returns int[]
'''
def parseICONews(row):
#580+ calls
ico_dict = []
for i in range(0, len(row)):
ico_inner = {}
ico_inner["id"] = row[i][0]
ico_inner["name"] = row[i][1]
terms = eval(row[i][5])
ico_inner["terms"] = terms
totalResults = 0;
tr = []
for t in terms:
if(t not in tr):
data = newsAPIAdv(t)
totalResults += data["totalResults"]
tr.append(t)
if(len(tr) > 1):
data = newsAPIAdv(" ".join(tr))
totalResults -= data["totalResults"]
ico_inner["results"] = totalResults;
ico_dict.append(ico_inner)
return ico_dict
'''
Parse the ICO twitter and return as array.
@params String[] terms
@returns string[]
'''
def parseICOTwitter(terms):
icoList = []
for t in terms:
list = {}
list["name"] = t
list["tweets"] = getTwitter(t)
icoList.append(list)
return icoList
'''
Parse the ICO reddit and return as array.
@params String[] terms
@returns string[]
'''
def parseICOReddit(terms):
return getRedditSub(terms)
'''
Parse the ICO Facebook and return as array.
@params String[] terms
@returns string[]
'''
def parseICOFacebook(terms):
icoList = []
for t in terms:
list = {}
list["name"] = t
list["likes"] = getFacebook(t)
icoList.append(list)
return icoList
'''
Insert coin price into database.
@params String[] plist
'''
def addPriceInfo(plist):
conn = getConnected()
cur = conn.cursor()
cur.execute("SELECT coin_id, ticker FROM cryptocounter_coin")
DBcoins = cur.fetchall()
for i in range(0, len(DBcoins)):
coin_id = DBcoins[i][0]
if(DBcoins[i][1] == "MIOTA"):
ticker = "IOTA"
else:
ticker = DBcoins[i][1]
for data in plist:
if(data["ticker"] == ticker):
dt = str(datetime.datetime.fromtimestamp(int(data["date"])))
p = str(data["price"])
cid = str(coin_id)
cs = str(data["circ_supply"])
mc = str(data["market_cap"])
pc = str(data["percent_change"])
cur.execute("INSERT INTO cryptocounter_price (date, price, coin_id_id, circ_supply, market_cap, percent_change) VALUES('{}',{},{},{},{},{})".format(dt,p,cid,cs,mc,pc))
break
conn.commit()
conn.close()
def updateCurrentPrice():
hour = 3600
day = hour * 24
conn = getConnected()
cur = conn.cursor()
cur.execute("SELECT MIN(coin_id_id) as id, date FROM cryptocounter_price GROUP BY date ORDER BY date DESC LIMIT 1")
DBcoins = cur.fetchall()
dt = int(time.mktime(DBcoins[0][1].timetuple()))
dl = int(dt-(dt%day))
d = time.time()
now = int(d-(d%day))
if(now != dl):
plist = parseCurrentPrice(trackedCoins)
addPriceInfo(plist)
print("Current price added: "+ str(datetime.datetime.fromtimestamp(int(now)).strftime('%Y-%m-%d %H:%M:%S')))
else:
print("ALERT: Skipping current price, duplicate")
def updateHistoricalPrice(days):
hour = 3600
day = hour * 24
conn = getConnected()
cur = conn.cursor()
cur.execute("SELECT MIN(coin_id_id) as id, date FROM cryptocounter_price GROUP BY date ORDER BY date ASC")
DBcoins = cur.fetchall()
#formating dates
dateList = []
for j in range(0, len(DBcoins)):
dt = int(time.mktime(DBcoins[j][1].timetuple()))
dl = int(dt-(dt%day))
dateList.append(dl)
d = time.time()
now = int(d-(d%day))
past = now - day * days
for i in range(past,now,day):
if(i not in dateList):
print("Cron is adding: "+str(datetime.datetime.fromtimestamp(int(i)).strftime('%Y-%m-%d %H:%M:%S')))
plist = parseHistoricalPrice(trackedCoins,i)
addPriceInfo(plist)
#TODO: calculate %change
#updatePC()
def updateICO():
plist = parseICO()
conn = getConnected()
cur = conn.cursor()
cur.execute("SELECT ico_name FROM cryptocounter_ico")
DBico_raw = cur.fetchall()
DBico = []
for i in range(0,len(DBico_raw)):
DBico.append(DBico_raw[i][0])
for data in plist:
if(data["ico_name"] not in DBico):
n = str(data["ico_name"])
s = str(data["start"])+"-00"
e = str(data["end"])+"-00"
d = str(data["description"])
st = str(data["search_terms"])
cur.execute("INSERT INTO cryptocounter_ico (ico_name, startdate, enddate, description, search_terms) VALUES('{}','{}','{}','{}','{}')".format(n,s,e,d,st))
conn.commit()
conn.close()
def updateOverallSocial():
hour = 3600
day = 24 * hour
conn = getConnected()
cur = conn.cursor()
cur.execute("SELECT MIN(id) as id, date FROM cryptocounter_overallsocial GROUP BY date ORDER BY date DESC LIMIT 1")
DBsocial = cur.fetchall()
dl = 0
d = time.time()
now = int(d-(d%day))
if(len(DBsocial) > 0):
dt = int(time.mktime(DBsocial[0][1].timetuple()))
dl = int(dt-(dt%day))
if(now != dl):
terms = ["crypto","cryptocurrency","cryptocurrencies", "blockchain"]
pNews = parseGeneralNews(terms)
pReddit = parseGeneralReddit()
pFB = parseGeneralFacebook()
dt = str(datetime.datetime.fromtimestamp(now))
cur.execute("INSERT INTO cryptocounter_overallsocial (date, num_tweets, num_subs, num_likes, num_articles, num_trends) VALUES('{}',{},{},{},{},{})".format(dt,-1,pReddit,pFB,pNews,-1))
print("Current overall social added: "+ str(datetime.datetime.fromtimestamp(int(now)).strftime('%Y-%m-%d %H:%M:%S')))
else:
print("ALERT: Skipping current overall social, duplicate")
conn.commit()
conn.close()
def updateSocialCoin():
hour = 3600
day = 24 * hour
conn = getConnected()
cur = conn.cursor()
cur.execute("SELECT MIN(id) as id, date FROM cryptocounter_socialcoin GROUP BY date ORDER BY date DESC LIMIT 1")
DBsocial = cur.fetchall()
dl = 0
d = time.time()
now = int(d-(d%day))
if(len(DBsocial) > 0):
dt = int(time.mktime(DBsocial[0][1].timetuple()))
dl = int(dt-(dt%day))
if(now != dl):
pNews = parseCoinNews()
pTweet = parseCoinTwitter(trackedCoins)
pReddit = parseCoinReddit(trackedCoins)
pFB = parseCoinFacebook(trackedCoins)
dt = str(datetime.datetime.fromtimestamp(now))
for i in range(0,len(pNews)):
coinID = pNews[i]["id"]
coinNews = pNews[i]["results"]
coinTweet = pTweet[i]["statuses"]
coinSub = pReddit[i]["subscribers"]
coinLikes = pFB[i]["likes"]
cur.execute("INSERT INTO cryptocounter_socialcoin (date, num_tweets, num_subs, num_likes, num_articles, num_trends, coin_id_id) VALUES('{}',{},{},{},{},{},{})".format(dt,coinTweet,coinSub,coinLikes,coinNews,-1,coinID))
print("Current social coin added: "+ str(datetime.datetime.fromtimestamp(int(now)).strftime('%Y-%m-%d %H:%M:%S')))
else:
print("ALERT: Skipping current social coin, duplicate")
conn.commit()
conn.close()
def updateSocialICO():
hour = 3600
day = 24 * hour
conn = getConnected()
cur = conn.cursor()
cur.execute("SELECT * FROM cryptocounter_ico")
icoInfo = cur.fetchall()
icoList = []
for z in range(0, len(icoInfo)):
icoList.append(icoInfo[z][1])
cur.execute("SELECT MIN(id) as id, date FROM cryptocounter_socialico GROUP BY date ORDER BY date DESC LIMIT 1")
DBsocial = cur.fetchall()
dl = 0
d = time.time()
now = int(d-(d%day))
if(len(DBsocial) > 0):
dt = int(time.mktime(DBsocial[0][1].timetuple()))
dl = int(dt-(dt%day))
if(now != dl):
pNews = parseICONews(icoInfo)
pReddit = parseICOReddit(icoList)
pFB = parseICOFacebook(icoList)
dt = str(datetime.datetime.fromtimestamp(now))
for i in range(0,len(pNews)):
icoID = pNews[i]["id"]
icoNews = pNews[i]["results"]
icoSub = pReddit[i]["sub"]
icoLikes = pFB[i]["likes"]
cur.execute("INSERT INTO cryptocounter_socialico (date, num_tweets, num_subs, num_likes, num_articles, num_trends, ico_id_id) VALUES('{}',{},{},{},{},{},{})".format(dt,-1,icoSub,icoLikes,icoNews,-1,icoID))
print("Current social ICO added: "+ str(datetime.datetime.fromtimestamp(int(now)).strftime('%Y-%m-%d %H:%M:%S')))
else:
print("ALERT: Skipping current social ICO, duplicate")
conn.commit()
conn.close()
def updateTicker():
conn = getConnected()
cur = conn.cursor()
cur.execute("SELECT * FROM cryptocounter_generalmarket")
gm = cur.fetchall()
cm = getAPI(CMC+"global/")["data"]
cap = cm["quotes"]["USD"]["total_market_cap"]
vol = cm["quotes"]["USD"]["total_volume_24h"]
dom = cm["bitcoin_percentage_of_market_cap"]
d = cm["last_updated"]
dt = str(datetime.datetime.fromtimestamp(d))
if(len(gm) > 0):
#update
cur.execute("UPDATE cryptocounter_generalmarket SET market_cap = {}, volume = {}, btc_dominance = {}, date_added = '{}' WHERE id=1".format(cap,vol,dom,dt))
print("Current ticker info updated: "+ str(datetime.datetime.fromtimestamp(d).strftime('%Y-%m-%d %H:%M:%S')))
else:
#insert
cur.execute("INSERT INTO cryptocounter_generalmarket (market_cap, volume, btc_dominance, date_added) VALUES({},{},{},'{}')".format(cap,vol,dom,dt))
print("Current ticker info added: "+ str(datetime.datetime.fromtimestamp(d).strftime('%Y-%m-%d %H:%M:%S')))
conn.commit()
conn.close()
def updateGoogleInfo():
#get google for general, coin, and ICO
#TODO: If works on after school, add limits to SELECT
conn = getConnected()
cur = conn.cursor()
cc = "cryptocounter_"
## general ##
cur.execute("SELECT id, date FROM "+cc+"overallsocial")
gen = cur.fetchall()
genGoogle = getGoogleTrends("cryptocurrency")
if(len(genGoogle) < 0):
return
for i in range(0, len(genGoogle)):
for j in range(0, len(gen)):
if(str(gen[j][1])[:-6] == genGoogle[i]["date"]):
gid = gen[j][0]
gtrend = genGoogle[i]["trend"]
cur.execute("UPDATE "+cc+"overallsocial SET num_trends={} WHERE id={}".format(gtrend,gid))
## coin ##
cur.execute("SELECT coin_id, coin_name FROM "+cc+"Coin")
coinNames = cur.fetchall()
for p in range(0, len(coinNames)): #for each coin
genGoogle = getGoogleTrends(coinNames[p][1]) #get gt
if(len(genGoogle) < 0):
return
if(genGoogle[0] == -1):
continue
for i in range(0, len(genGoogle)): #for each date in gt
cur.execute("SELECT "+cc+"socialcoin.id, "+cc+"Coin.coin_name, "+cc+"socialcoin.date FROM "+cc+"socialcoin INNER JOIN "+cc+"Coin ON "+cc+"socialcoin.coin_id_id ="+cc+"Coin.coin_id WHERE "+cc+"Coin.coin_name ='"+coinNames[p][1]+"'")
genCoin = cur.fetchall()
for j in range(0, len(genCoin)): #find date in DB
if(str(genCoin[j][2])[:-6] == genGoogle[i]["date"] and genCoin[j][1] == coinNames[p][1]):
gid = genCoin[j][0]
gtrend = genGoogle[i]["trend"]
cur.execute("UPDATE "+cc+"socialcoin SET num_trends={} WHERE id={}".format(gtrend,gid))
## ICO ##
cur.execute("SELECT ico_id, ico_name FROM "+cc+"ico ORDER BY ico_name ASC")
icoNames = cur.fetchall()
for p in range(0, len(icoNames)): #for each coin
genGoogle = getGoogleTrends(icoNames[p][1]) #get gt
if(len(genGoogle) < 0):
return
if(genGoogle[0] == -1):
continue
for i in range(0, len(genGoogle)): #for each date in gt
cur.execute("SELECT "+cc+"socialico.id, "+cc+"ico.ico_name, "+cc+"socialico.date FROM "+cc+"socialico INNER JOIN "+cc+"ico ON "+cc+"socialico.ico_id_id ="+cc+"ico.ico_id WHERE "+cc+"ico.ico_name ='"+icoNames[p][1]+"'")
genICO = cur.fetchall()
for j in range(0, len(genICO)): #find date in DB
if(str(genICO[j][2])[:-6] == genGoogle[i]["date"] and genICO[j][1] == icoNames[p][1]):
gid = genICO[j][0]
gtrend = genGoogle[i]["trend"]
cur.execute("UPDATE "+cc+"socialico SET num_trends={} WHERE id={}".format(gtrend,gid))
conn.commit()
conn.close()
def updateTwitterInfo():
#get google for general and ICO
#TODO: If works on after school, add limits (to ICO) to SELECT
print("NOTE: This will take over 5+ hours, you may use the site while cron is working")
conn = getConnected()
cur = conn.cursor()
cc = "cryptocounter_"
day = 86400
d = time.time()
now = int(d-(d%day))
## general ##
cur.execute("SELECT id, date, num_tweets FROM "+cc+"overallsocial ORDER BY date DESC LIMIT 1")
gen = cur.fetchall()
dt = int(time.mktime(gen[0][1].timetuple()))
ld = int(dt-(dt%day))
if(ld == now):
if(gen[0][2] == -1):
numGenTweets = parseGeneralTwitter()
cur.execute("UPDATE "+cc+"overallsocial SET num_tweets={} WHERE id={}".format(numGenTweets,gen[0][0]))
else:
print("SKIPPING GENERAL TWEET: already recorded")
else:
print("SKIPPING GENERAL TWEET: date mismatch")
'''
## ICO ##
print("Note: Working on ICO Twitter, this may take a long time")
cur.execute("SELECT "+cc+"socialico.id, "+cc+"ico.ico_name, "+cc+"socialico.date FROM "+cc+"socialico INNER JOIN "+cc+"ico ON "+cc+"socialico.ico_id_id ="+cc+"ico.ico_id")
genICO = cur.fetchall()
cur.execute("SELECT ico_id, ico_name FROM "+cc+"ico")
icoNames = cur.fetchall()
for p in range(0, len(icoNames)): #for each coin
genGoogle = getGoogleTrends(icoNames[p][1]) #get gt
if(genGoogle == -1):
continue
for i in range(0, len(genGoogle)): #for each date in gt
for j in range(0, len(genICO)): #find date in DB
if(str(genICO[j][2])[:-6] == genGoogle[i]["date"]):
gid = genICO[j][0]
gtrend = genGoogle[i]["trend"]
cur.execute("UPDATE "+cc+"socialico SET num_trends={} WHERE id={}".format(gtrend,gid))
'''
conn.commit()
conn.close()
### TESTING CODE BELOW ###
#truncate all coin and social DB for a clean start
def truncateDB(test=False):
db_info = settings.DATABASES["default"]
try:
conn = psycopg2.connect("dbname="+db_info["NAME"]+" user="+db_info["USER"]+" host="+db_info["HOST"]+" password="+db_info["PASSWORD"])
except:
print("I am unable to connect to the db")
tableList = ["coin", "price","ico","overallsocial","socialcoin","socialico","generalmarket"]
cur = conn.cursor()
for item in tableList:
cur.execute("TRUNCATE TABLE cryptocounter_"+item+" RESTART IDENTITY CASCADE")
if(not(test)):
print("Truncated "+item)
seqList = ["coin_coin", "ico_ico","price","overallsocial","socialcoin","socialico"]
for item in seqList:
cur.execute("ALTER SEQUENCE cryptocounter_"+item+"_id_seq RESTART 1")
conn.commit()
conn.close()
def main(test=False): #setup for initial run
if(not(test)):
#a = time.time()