-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.py
83 lines (67 loc) · 2.9 KB
/
crypto.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
'''track cryptocurrencies on the stock markets'''
import coinmarketcap
from apscheduler.schedulers.blocking import BlockingScheduler
import pandas as pd
import urllib
from urllib.request import urlopen
from bs4 import BeautifulSoup
from twilio.rest import Client
def make_soup(url): #in order to parse the webpage for info
the_page = urllib.request.urlopen(url)
soup_data = BeautifulSoup(the_page, 'html.parser')
return soup_data
def coin_info(coin): # parse the top ten news for each coin
coinsoup = make_soup('https://news.google.com/news/search/section/q/' + coin)
coin = coinsoup.find_all("a", class_="nuEeue hzdq5d ME7ew", limit=8)
time_ago = coinsoup.find_all("span", class_="d5kXP YBZVLb", limit=8)
total_news = "\n"
for v, i in zip(coin, time_ago):#print coin and time tag simultaneously
anchor = v.string.strip()
hyperlink = v.get("href")
time_stamp = i.string.strip()
news = '{}'.format(anchor) + '. ' + '{}'.format(time_stamp) + '.' + '\n' + '{}'.format(hyperlink) + '\n'
total_news += news #can't send over 1600 char limit per message re-examine this and split into multiple msgs
return total_news
def coin_price(coin): # connect to api(coinmarketcap.com) and print info
market = coinmarketcap.Market()
currency = market.ticker(coin)
price_table = pd.Series((currency)[0])
#reindex to select particular info from pd.Series
price_table = price_table.reindex(["name", "rank", "symbol", "price_usd", "price_btc",
"percent_change_1h", "percent_change_24h", "percent_change_7d"])
return price_table
def coin_and_news():
bit_price = coin_price("bitcoin")
eth_price = coin_price("ethereum")
iota_price = coin_price("iota")
#string formation needed as twilio body req string
textable = '\n' + '{}'.format(bit_price) + '\n' + '\n' + '\n' + \
'{}'.format(eth_price) + '\n' + '\n' + '\n' +\
'{}'.format(iota_price) + '\n' + '\n' + '\n'
return textable #return > print when passing to another program
def send_txt(body):
account_sid =
auth_token =
client = Client(account_sid, auth_token)
client.messages.create(
to=
from_="
body=body) # string req
def send_coin():
price = send_txt(coin_and_news())
return price
def send_bit_news():
bit_news = send_txt(coin_info("bitcoin"))
return bit_news
def send_eth_news():
eth_news = send_txt(coin_info("ethereum"))
return eth_news
def send_iota_news():
iota_news = send_txt(coin_info("iota"))
return iota_news
sched = BlockingScheduler()
sched.add_job(send_coin, 'cron', hour='10-22', minute='3')#scheduled to run between the hours 10 and 10pm
sched.add_job(send_bit_news, 'cron', hour='10-22', minute='2')
sched.add_job(send_eth_news, 'cron', hour='10-22', minute='1')
sched.add_job(send_iota_news, 'cron', hour='10-22', minute='0')
sched.start()