-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
66 lines (46 loc) · 1.79 KB
/
bot.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
import re
import tweepy
from time import sleep
from crypto import lookup
from symbols import symbols
from twitter import api, store_LSID, retrieve_LSID
def reply():
print('Waiting...')
LSID = retrieve_LSID('ID.txt')
mentions = api.mentions_timeline(LSID)
for mention in reversed(mentions):
# prints the mention
print(f"{mention.id} - {mention.text}")
# looks for hashtags
pattern = re.compile(r"#(\w+)")
hashtags = pattern.findall(mention.text)
hashtags = [hashtag.upper() for hashtag in hashtags]
# stores the tweet ID (used to ignore the current tweet on the next call)
LSID = mention.id
store_LSID(LSID, 'ID.txt')
# creates a list with valid currencies (mentioned on the hashtags)
currencies = list(set(hashtags).intersection(symbols))
# prints all the valid currencies - count
print(f'{currencies}\nCount: "{len(currencies)}"')
prices = []
# gets the price of each currency
for currency in currencies:
price = lookup(currency)
prices.append(price)
sleep(1)
username = mention.user.screen_name
reply_tweet = '@' + username + '\n'
# prepares the string to be tweeted
for currency, price in zip(currencies, prices):
temp_string = '#' + currency + ': ' + price + '\n'
reply_tweet += temp_string
print(f"reply:\n{reply_tweet}")
# tweets back
try:
api.update_status(reply_tweet, mention.id)
except tweepy.error.TweepError:
# If the response cannot fit 280 characters.
api.update_status('@' + username + "Error: The response is too long. Please try again.", mention.id)
while True:
reply()
sleep(2)