-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency_pkmn_bot.py
102 lines (93 loc) · 3.9 KB
/
currency_pkmn_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
import tweepy
import time
import json
import requests
import string
# For Running Bot
from account_keys import *
# Twitter API Config
auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
FILE_NAME = 'last_seen_id.txt'
# Currency Exchange Config
USD = ["usd", "dollar", "dollars", "dólar", "dolar", "dólares", "dolares"]
EUR = ["eur", "euro", "euros"]
GBP = ["gbp", "libra", "libras", "esterlina", "esterlinas", "pound", "sterling"]
JPY = ["jpy", "jpn", "yen", "yens", "yene", "yenes", "ien", "iens", "iene", "ienes"]
ARS = ["ars", "peso", "pesos", "argentina", "argentino", "argentinos", "convertible"]
CNY = ["cny", "rmb", "yuan", "yuans", "iuan", "iuans", "iuane", "iuanes", "renminbi", "renminbis", "renmimbi", "renmimbis", "remimbi", "remimbis", "remmimbi", "remmimbis", "chinês", "chines", "china"]
REQUEST_URL = "https://economia.awesomeapi.com.br/json/"
# Pokémon Config
pokedex = open('pokedex.json', encoding="utf8")
pokemon_list = json.load(pokedex)
def retrieve_last_seen_id(file_name):
f_read = open(file_name, 'r')
last_seen_id = int(f_read.read().strip())
f_read.close()
return last_seen_id
def store_last_seen_id(last_seen_id, file_name):
f_write = open(file_name, 'w')
f_write.write(str(last_seen_id))
f_write.close()
return
def reply_to_tweets():
print('retrieving and replying to tweets...')
last_seen_id = retrieve_last_seen_id(FILE_NAME)
mentions = api.mentions_timeline(last_seen_id, tweet_mode='extended')
for mention in reversed(mentions):
if mention.user.screen_name == "cambiopokemon":
continue
print(str(mention.id) + ' - ' + mention.full_text)
last_seen_id = mention.id
store_last_seen_id(last_seen_id, FILE_NAME)
current_currency = ""
tweet_text = mention.full_text.lower().split()
for tweet_word in tweet_text:
table_punctuation = str.maketrans(dict.fromkeys(string.punctuation))
word = tweet_word.translate(table_punctuation)
print(word)
if word in USD:
current_currency = "USD"
break
if word in EUR:
current_currency = "EUR"
break
if word in GBP:
current_currency = "GBP"
break
if word in JPY:
current_currency = "JPY"
break
if word in ARS:
current_currency = "ARS"
break
if word in CNY:
current_currency = "CNY"
break
if current_currency != "":
currency_info = request_value(current_currency)
currency_value = "{:.2f}".format(float(currency_info['ask']))
pokemon_number = currency_value.replace('.','')
pokemon_name = pokemon_list[int(pokemon_number)-1]['name']['english']
pokemon_image_path = 'pokemon_images/{}.png'.format(pokemon_number)
if current_currency == "ARS" or current_currency == "JPY":
currency_value = "{:.3f}".format(float(currency_info['ask']))
post_message = "@{0} A cotação atual de 1 {1} é: R${2}!\n\nPokémon #{3} - {4}".format(mention.user.screen_name, currency_info['name'], currency_value.replace('.',','), pokemon_number, pokemon_name)
api.update_with_media(
filename = pokemon_image_path,
status = post_message,
in_reply_to_status_id = mention.id
)
print("@{0} respondido por {1}!".format(mention.user.screen_name, pokemon_name))
def request_value(currency):
request = requests.get("{0}{1}".format(REQUEST_URL, currency))
exchange_json = json.loads(request.content.decode('utf-8'))
return exchange_json[0]
while True:
try:
reply_to_tweets()
except:
print("Exception")
time.sleep(15)