class cryptonews(): def __init__(self, key): self.key = key self.api = CryptoControlAPI(key) self.proxyApi = CryptoControlAPI( key, "http://cryptocontrol_proxy/api/v1/public") def enableSentiment(self): self.sentiment = True def top_news(self, lang=None): return self.api.getTopNews(language=lang) def news_by_coin(self, coin, lang=None): return self.api.getTopNewsByCoin(coin=coin, language=lang) def get_top_tweets(self, coin, lang=None): return self.api.getTopTweetsByCoin(coin=coin, language=lang) def get_reddit(self, coin, lang=None): return self.api.getLatestRedditPostsByCoin(coin=coin, language=lang) def get_top_feed(self, coin, lang): return self.api.getTopFeedByCoin(coin=coin, language=lang) def get_latest(self, coin, lang): return self.api.getLatestItemsByCoin(coin=coin, language=lang) def get_coinDetails(self, coin, lang): return self.api.getCoinDetails(coin=coin, language=lang)
def latestCoinNews(): try: api = CryptoControlAPI(CRYPTO_NEWS_KEY) res = api.getTopNewsByCoin(request.form['name']) return jsonify(res) except Exception as e: print(e) response = ERR_RESP return response
api = CryptoControlAPI(key2) # Connect to a self-hosted proxy server (to improve performance) that points to cryptocontrol.io proxyApi = CryptoControlAPI(key2, "http://cryptocontrol_proxy/api/v1/public") # Get top news print(pd.DataFrame(api.getTopNews()))\ # returned dataframe can be viewed fully in the csv file, same for the other dataframes pd.DataFrame(api.getTopNews()).to_csv('section1/task3/topnews.csv', index=False) # get latest russian news print(pd.DataFrame(api.getLatestNews("ru"))) # # get top bitcoin news print(pd.DataFrame(api.getTopNewsByCoin("bitcoin"))) # get top EOS tweets print(pd.DataFrame(api.getTopTweetsByCoin("eos"))) # get top Ripple reddit posts print(pd.DataFrame(api.getLatestRedditPostsByCoin("ripple"))) # get reddit/tweets/articles in a single combined feed for NEO feed = pd.DataFrame(api.getTopFeedByCoin("neo")) print(feed) # get latest reddit/tweets/articles (seperated) for Litecoin print(pd.DataFrame(api.getLatestItemsByCoin("litecoin"))) # get details (subreddits, twitter handles, description, links) for ethereum
class News: def __init__(self, api_key): self.language = 'en' self.api = CryptoControlAPI(api_key) def set_language(self, language): # English (en default) # Chinese/Mandarin 'cn' # German 'de' # Italian 'it' # Japanese 'jp' # Korean 'ko' # Portuguese 'po' # Russian 'ru' # Spanish 'es' self.language = language def getTopNews(self): # Get the top news articles. self.data, self.formated = None, None try: self.data = self.api.getTopNews(language=self.language) self.formated = self.format_news(self.data) print(self.formated) except Exception as e: print(e) def getLatestNews(self): # Get the latest news articles. self.data, self.formated = None, None try: self.data = self.api.getLatestNews(language=self.language) self.formated = self.format_news(self.data) print(self.formated) except Exception as e: print(e) def getTopNewsByCoin(self, coin): # Get the top news articles for a specific coin from the CryptoControl API. self.data, self.formated = None, None try: self.data = self.api.getTopNewsByCoin(coin,language=self.language) self.formated = self.format_news(self.data) print(self.formated) except Exception as e: print(e) def getTopNewsByCategory(self): # Get news articles grouped by category. self.data, self.formated = None, None try: self.data = self.api.getTopNewsByCategory(language=self.language) except Exception as e: print(e) return self.formated = pd.concat([self.format_news(self.data[i]) for i in self.data.keys()]) print(self.formated) def getLatestNewsByCoin(self, coin): # Get the latest news articles for a specific coin. self.data, self.formated = None, None try: self.data = self.api.getLatestNewsByCoin(coin, language= self.language) self.formated = self.format_news(self.data) print(self.formated) except Exception as e: print(e) def getTopNewsByCoinCategory(self, coin): # Get news articles grouped by category for a specific coin. self.data, self.formated = None, None try: self.data = self.api.getTopNewsByCoinCategory(coin, language=self.language) self.formated = self.format_news(self.data) print(self.formated) except Exception as e: print(e) def getTopTweetsByCoin(self, coin): # Get top tweets for a particular coin self.tweets, self.formated = None, None try: self.tweets = self.api.getTopTweetsByCoin(coin,language=self.language) self.formated = self.format_tweets(self.tweets) print(self.formated) except Exception as e: print(e) def getLatestTweetsByCoin(self, coin): # Get latest tweets for a particular coin self.tweets, self.formated = None, None try: self.tweets = self.api.getLatestTweetsByCoin(coin,language=self.language) self.formated = self.format_tweets(self.tweets) print(self.formated) except Exception as e: print(e) def getTopRedditPostsByCoin(self, coin): # Get top reddit posts for a particular coin self.reddit, self.formated = None, None try: self.reddit = self.api.getTopRedditPostsByCoin(coin,language=self.language) self.formated = self.format_reddit(self.reddit) print(self.formated) except Exception as e: print(e) def getTopFeedByCoin(self, coin): # Get a combined feed (reddit/tweets/articles) for a particular coin sort by time self.feed, self.formated = None, None try: self.feed = self.api.getTopFeedByCoin(coin,language=self.language) self.formated = self.format_feed(self.feed) print(self.formated) except Exception as e: print(e) def getCoinDetails(self, coin): try: self.coin = self.api.getCoinDetails(coin,language=self.language) print(pd.DataFrame.from_dict(self.coin,orient = 'index')) except Exception as e: print(e) def export_csv(self): # export formated data to csv self.formated.to_csv('section1/task3/Formated_Data.csv', index = 0) @staticmethod def format_news(data): news = pd.DataFrame(data) news = news[['primaryCategory', 'coins', 'title', 'url', 'source','publishedAt']] news['coins'] = [[i['tradingSymbol'] for i in news.coins[j]] for j in range(news.coins.size)] news['source'] = [i['name'] for i in news.source] return news @staticmethod def format_tweets(data): tweets = pd.DataFrame(data) tweets = tweets[['text', 'url', 'publishedAt','retweetCount', 'favoriteCount']] return tweets @staticmethod def format_reddit(data): reddit = pd.DataFrame(data) reddit = reddit[['title', 'url', 'subreddit', 'publishedAt' , 'comments', 'upvotes']] return reddit def format_feed(self, data): feed = pd.DataFrame(data) article = self.format_news([x for x in feed.article.dropna()]) tweet = self.format_tweets([x for x in feed.tweet.dropna()]) reddit = self.format_reddit([x for x in feed.reddit.dropna()]) tweet = tweet.rename(columns={'text':'title'}) df = pd.concat([article,tweet,reddit]).sort_values('publishedAt',ascending=False) df = df.drop('coins', axis = 1) # df = df[['title','url','publishedAt']] return df
axis=1) df.to_csv('top_news_data.csv', index=False) # get latest russian news ru = api.getLatestNews("ru") df = pd.DataFrame(ru) df = df.drop([ 'similarArticles', 'coins', 'source', 'description', 'originalImageUrl', 'url', 'thumbnail' ], axis=1) df['title'].apply(lambda x: x.strip()) df.to_csv('ru_data.csv', index=False) # get top bitcoin news bitcoin = api.getTopNewsByCoin("bitcoin") df = pd.DataFrame(bitcoin) df = df.drop([ 'similarArticles', 'coins', 'source', 'description', 'originalImageUrl', 'url', 'thumbnail' ], axis=1) df.to_csv('bitcoin_data.csv', index=False) # get top EOS tweets eos = api.getTopTweetsByCoin("eos") df = pd.DataFrame(eos) df = df.drop(['text', 'url'], axis=1) df.to_csv('eos_data.csv', index=False) # get top Ripple reddit posts
# but returns the message when status is not '200' # Get top news top_news = api.getTopNews() tn = format_df(top_news) save_j(top_news, 'top_news.json') save_csv(tn, 'top_news.csv') # get top news by category top_category_news = api.getTopNewsByCategory() ctn = format_df(top_category_news['analysis']) save_j(top_category_news, 'top_category_news.json') save_csv(ctn, 'top_category_news.csv') # get coin news top_coin_news = api.getTopNewsByCoin("bitcoin") cn = format_df(top_coin_news) save_j(top_coin_news, 'top_coin_news.json') save_csv(cn, 'top_coin_news.csv') # get latest news latest_news = api.getLatestNews() ln = format_df(latest_news) save_j(latest_news, 'latest_news.json') save_csv(ln, 'latest_news.csv') # get latest news by coin latest_coin_news = api.getLatestNewsByCoin("bitcoin") lcn = format_df(latest_coin_news) save_j(latest_coin_news, 'latest_coin_news.json') save_csv(lcn, 'latest_coin_news.csv')
# Connect to a self-hosted proxy server (to improve performance) that points to cryptocontrol.io proxyApi = CryptoControlAPI(key, "http://cryptocontrol_proxy/api/v1/public") # Get top news top_news = api.getTopNews(language='cn') top_news_filename = f'top-news-cn.json' save_json(top_news, top_news_filename, save=True) # get latest english news latest_news = api.getLatestNews('en') latest_news_filename = f'latest-news-en.json' save_json(latest_news, latest_news_filename, save=True) # get top bitcoin news top_btc_news = api.getTopNewsByCoin("bitcoin") top_btc_news_filename = f'top-btc-news.json' save_json(top_btc_news, top_btc_news_filename, save=True) # get top EOS tweets eos_tweets = api.getTopTweetsByCoin("eos") eos_tweets_filename = f'eos-tweets.json' save_json(eos_tweets, eos_tweets_filename, save=True) # get top Ripple reddit posts reddit = api.getLatestRedditPostsByCoin("ripple") reddit_filename = f'ripple-reddit.json' save_json(reddit, reddit_filename, save=True) # get reddit/tweets/articles in a single combined feed for NEO neo_feed = api.getTopFeedByCoin("neo")