Skip to content

Instantly share code, notes, and snippets.

@melardev
Last active November 18, 2022 00:48
Show Gist options
  • Save melardev/22380961372b41497b2f1f7e6467cd0e to your computer and use it in GitHub Desktop.
Save melardev/22380961372b41497b2f1f7e6467cd0e to your computer and use it in GitHub Desktop.
"""
References:
- https://developer.twitter.com/en/docs/twitter-api/tweets/counts/quick-start/recent-tweet-counts
- https://developer.twitter.com/en/docs/twitter-api/tweets/counts/introduction
- https://stackoverflow.com/questions/79797/how-to-convert-local-time-string-to-utc
- https://stackoverflow.com/questions/12468823/python-datetime-setting-fixed-hour-and-minute-after-using-strptime-to-get-day
- https://www.folkstalk.com/2022/10/1-day-ago-python-datetime-with-code-examples.html
- https://developer.twitter.com/en/docs/twitter-api/v1/rules-and-filtering/search-operators
- https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/main/Recent-Tweet-Counts/recent_tweet_counts.py
- https://developer.twitter.com/apitools/api?endpoint=/2/tweets/counts/recent&method=get
- https://twittercommunity.com/t/twitter-api-v2-search-endpoint-what-is-start-time-and-end-time-actual-default/152679
- https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md
- https://github.com/ccxt/ccxt
- https://developer.twitter.com/en/portal/dashboard
"""
import datetime
import json
import os.path
import time
import requests
import tweepy
bearer_token = "<PUT_YOUR_TOKEN_HERE>"
client = tweepy.Client(bearer_token)
monitor_all = True
if not monitor_all:
coin_names = {
'BTC',
'BCH',
'ETH',
'GMX',
'MASK',
'NEAR',
'SOL'
}
else:
coin_names = set()
try:
import ccxt
markets = ccxt.binance().load_markets()
for _, info in markets.items():
coin_names.add(info.get('base'))
except ImportError:
res: requests.Response
res = requests.get('https://binance.com/api/v3/exchangeInfo')
if res.status_code == 200:
ex_info = json.loads(res.content)
for s in ex_info['symbols']:
coin_names.add(s['baseAsset'])
now = datetime.datetime.now(datetime.timezone.utc)
start_date = datetime.datetime.now() - datetime.timedelta(days=1)
start_date = start_date.replace(hour=0, minute=0, second=0, microsecond=0)
# The twitter API gives error if the end_date is not at least 10 seconds older than the time we make the request
end_date = now - datetime.timedelta(minutes=2)
prev_results_path = 'volume_stats.json'
if os.path.exists(prev_results_path):
with open(prev_results_path) as fd:
try:
coin_stats = json.loads(fd.read())
except:
coin_stats = dict()
else:
coin_stats = dict()
try:
for coin_name in coin_names:
response: tweepy.Response
response = client.get_recent_tweets_count(query=f'#{coin_name}',
granularity='day',
start_time=start_date,
end_time=end_date)
volume_count = response.meta.get('total_tweet_count')
print(f'{coin_name}: {volume_count}', end='')
if coin_name in coin_stats:
prev_volume = coin_stats[coin_name]['volume']
print(f' (prev: {prev_volume})', end='')
if volume_count >= 50 and (volume_count / prev_volume) >= 1.5:
print(f' Considerable increase!')
else:
print()
else:
print()
coin_stats[coin_name] = {
'volume': volume_count,
'updated': now.strftime('%d-%m-%Y %H:%M:%S'),
}
time.sleep(5)
except Exception as exc:
print(f'An exceptcion occurred on {exc}')
print(f'Saving {len(coin_names)} volumes')
with open(prev_results_path, 'w') as fd:
fd.write(json.dumps(coin_stats))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment