From 91f0e19a5e838c5bd502463a361849be4cbe0e21 Mon Sep 17 00:00:00 2001 From: Mohit Gaur <56885276+Mohit-Gaur@users.noreply.github.com> Date: Tue, 14 Apr 2020 12:18:06 +0530 Subject: [PATCH] Update run.py Added a try and except block for error handling and used request library --- run.py | 57 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/run.py b/run.py index 7cfd076..bdd7da4 100644 --- a/run.py +++ b/run.py @@ -3,22 +3,19 @@ import json import time import tweepy - +import requests ssl._create_default_https_context = ssl._create_unverified_context - # Oauth keys -consumer_key ="XXX" -consumer_secret ="XXX" -access_token ="XXX" -access_token_secret ="XXX" - +consumer_key ='ENTER KEY' +consumer_secret = 'ENTER KEY' +access_token = 'ENTER KEY' +access_token_secret = 'ENTER KEY' # Authentication with Twitter auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) #LunarCRUSH API Key -api_key = "XXX" - +api_key = 'ENTER KEY' # Allows adding as many coins as desired coin_list = [ "LTC", @@ -26,7 +23,6 @@ "BTC" ] coins = ','.join(coin_list) - # A list of the fields desired from the API - key is the Lunar Crush key, and the value is the field name outputted to Twitter # {"LUNAR_CRUSH_KEY": "RENDERED_NAME"} # For example, to add tweet_replies, you would add: @@ -48,7 +44,6 @@ {"news": " News: "}, {"close": " Close: "}, ] - def final_render(asset_tweet, value, key, asset): # As the program becomes more complex, this should be written in a more robust manner if key == 'symbol': @@ -58,26 +53,30 @@ def final_render(asset_tweet, value, key, asset): else: asset_tweet += value + str(asset[key]) return asset_tweet - - # Iterates over each of the fields from Lunar Crush, gets the value from Lunar Crush and renders it with the field name def main(): - - url = "https://api.lunarcrush.com/v2?data=assets&key=" + api_key + "&symbol=" + coins - assets = json.loads(urllib.request.urlopen(url).read()) - - for asset in assets['data']: - asset_tweet = "" - for field in map: - key = list(field.keys())[0] - value = list(field.values())[0] - asset_tweet = final_render(asset_tweet, value, key, asset) - print(asset_tweet) - print(len(asset_tweet)) - # Posts tweets - api.update_status(status=asset_tweet) - - # Runs main() every 30 minutes + # url = "https://api.lunarcrush.com/v2?data=assets?&key=" + api_key + "&symbol=" + coins + url = "https://lunarcrush.com/api/v1/assets?symbols=BTC,LTC,ETH&key=" + api_key + print(url) + obj = requests.get(url) + print(obj.text) + try: + assets = json.loads(urllib.request.urlopen(url).read()) + print(assets) + for asset in assets['data']: + asset_tweet = "" + for field in map: + key = list(field.keys())[0] + value = list(field.values())[0] + asset_tweet = final_render(asset_tweet, value, key, asset) + print(asset_tweet) + print(len(asset_tweet)) + # Posts tweets + api.update_status(status=asset_tweet) + except : + print("Authentication Error") + exit() +# Runs main() every 30 minutes while True: main() time.sleep(1800)