-
Notifications
You must be signed in to change notification settings - Fork 1
/
API_scrape_github_actions.py
81 lines (66 loc) · 2.89 KB
/
API_scrape_github_actions.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
import tweepy
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
import os
consumer_key_ghs = os.environ.get("CONSUMER_KEY")
consumer_secret_ghs = os.environ.get("CONSUMER_SECRET")
access_token_ghs = os.environ.get("ACCESS_TOKEN")
access_token_secret_ghs = os.environ.get("ACCESS_TOKEN_SECRET")
cmc_key_ghs = os.environ.get("CMC_KEY")
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
parameters = {
'start':'1',
'limit':'12',
'convert':'USD'
}
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': cmc_key_ghs,
}
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)):
if 'USD' in symbols[i]:
continue
else:
pricedata.append(str(symbols[i] +': '+ str(price[i]) +' '+ percent_change[i]+'\n'))
final_pricedata = ''.join([item for item in pricedata])
final_pricedata = (final_pricedata[:277] + '...') if len(final_pricedata) > 280 else final_pricedata
#consumer_key = consumer_key_ghs
#consumer_secret = consumer_secret_ghs
#access_token = access_token_ghs
#access_token_secret = access_token_secret_ghs
#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)
#print(final_pricedata)
client = tweepy.Client(consumer_key=consumer_key_ghs,
consumer_secret=consumer_secret_ghs,
access_token=access_token_ghs,
access_token_secret=access_token_secret_ghs)
# Replace the text with whatever you want to Tweet about
response = client.create_tweet(text=final_pricedata)