forked from 8BallDuVal/CryptoTwitterBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI_scrape.py
More file actions
67 lines (59 loc) · 2.9 KB
/
API_scrape.py
File metadata and controls
67 lines (59 loc) · 2.9 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
import tweepy
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
parameters = {
'start':'1',
'limit':'11',
'convert':'USD'
}
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': '', # info from coinmarketcap.com/api/
}
session = Session()
session.headers.update(headers)
symbols = []
price = []
percent_change = []
cc_data = []
try:
response = session.get(url, params=parameters)
cryptodata = json.loads(response.text)
for key, value in cryptodata.items():
if key == 'data':
for item in value:
for key_1,value_1 in item.items():
if key_1 == 'quote':
price_rounded = round(float(value_1['USD']['price']), 3) # rounding the prices to 3 decimals to save characters on twitter
price.append(str(price_rounded)) # storing the rounded prices into an array
percent_change_rounded = round(float(value_1['USD']['percent_change_24h']), 2) # rounding values to 3 decimals as done above
percent_change.append('('+str(percent_change_rounded) + '%)') # storing Percent change in 24 hours into an array
if key_1 == 'symbol':
symbol = '$' + value_1
symbols.append(symbol) # Cryptocurrency ticker symbol/acronym
except (ConnectionError, Timeout, TooManyRedirects) as e:
print(e)
pricedata = []
for i in range(len(symbols)):
pricedata.append(str(symbols[i] +': '+ str(price[i]) +' '+ percent_change[i]+'\n'))
final_pricedata = (pricedata[0] +
pricedata[1] +
pricedata[2] +
pricedata[3] +
pricedata[4] +
pricedata[5] +
pricedata[6] +
pricedata[7] +
pricedata[8] +
pricedata[9] +
pricedata[10])
consumer_key = '' # info from developer.twitter.com
consumer_secret = '' # info from developer.twitter.com
access_token = '' # info from developer.twitter.com
access_token_secret = '' # info from developer.twitter.com
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
api.update_status(final_pricedata)