Browse Source

Initial commit

master
adderou 2 years ago
commit
429cb6b650
1 changed files with 136 additions and 0 deletions
  1. 136
    0
      bot.py

+ 136
- 0
bot.py View File

@@ -0,0 +1,136 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from telegram.ext import *
import telegram
from telegram import InlineQueryResultArticle, InputTextMessageContent
import feedparser
import logging
import time
import locale

locale.setlocale(locale.LC_ALL, 'es_ES.UTF-8')


#From stackoverflow <3
from HTMLParser import HTMLParser

class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)

def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()


logging.basicConfig(level=logging.NOTSET,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')



updater = Updater(token='229911324:AAEa_1xYrR5HhZ9Cqtr4VKsI-jr5NuB_ilI')
dispatcher = updater.dispatcher


#Bot data

horoscopoData = {
'date':None,
'data':{},
}

def parseData(bot=None,update=None):
if (horoscopoData['date']!= time.strftime("%d/%m/%Y")):
if bot is not None and update is not None:
text = u"Corazón, tengo que conectarme con los astros para resolver tu duda. Dame unos segundos..."
bot.sendMessage(chat_id=update.message.chat_id, text=text)
horoscopoData['date'] = time.strftime("%d/%m/%Y")
d = feedparser.parse('http://www.horoscopo7.com/feed/')
text = d.entries[0].content[0].value.replace(u"\xa0","").replace(u"Á","a").replace(u"Ó","o")
text2 = strip_tags(text.encode("utf-8")).split("\n")
i=0
j = 0
while i < len(text2)-4:
sign = text2[i]
signkey = sign.lower();
print signkey +","
horoscopoData['data'][signkey] = {}
horoscopoData['data'][signkey]['sign'] = sign.replace("a",u"Á").replace("o",u"Ó").encode("utf-8")
horoscopoData['data'][signkey]['order'] = j
horoscopoData['data'][signkey]['birthday'] = text2[i+1]
horoscopoData['data'][signkey]['prediction'] = text2[i+2]
i+=4
j+=1
return horoscopoData['data']
#Consigue de la lista info acerca de los signos consultados
def getData(args):
response = ""
if args!=None:
for arg in args:
arg = arg.replace(u"Á","a").replace(u"Ó","o").replace(u"á","a").replace(u"ó","o")
response += formatData(arg)
if response == "":
response += "Mi amor, necesitas preguntar por un signo para saber que le deparará el futuro. Escribe _/horoscopo <signo>_ y podré contestarte."
else:
response = "Ésta es mi predicción para el día de hoy, "+time.strftime("%A %d de %B de %Y")+"\n\n" + response
return response
def formatData(arg):
response = ""
arg = arg.lower()
if arg in horoscopoData['data']:
data = horoscopoData['data'][arg]
response += "*"+data['sign']+"*\n"+\
"_"+data['birthday']+"_\n"+\
data['prediction']+"\n\n"
return response

def start(bot, update):
text = u"Querido, las estrellas me dijeron que vendrías a visitarme. Pregunta por tu horóscopo o el de tus cercanos a través de este bot."
bot.sendMessage(chat_id=update.message.chat_id, text=text)
def horoscopo(bot,update,args = None):
d = parseData(bot,update)
response = ""
response += getData(args)
if response=="":
response = u"Lo siento mi amor, reformula tu pregunta, los astros no me dijeron nada acerca de tu signo."
bot.sendMessage(chat_id=update.message.chat_id, text=response, parse_mode=telegram.ParseMode.MARKDOWN)
def inline_caps(bot, update):
query = update.inline_query.query.lower().replace(u"Á","a").replace(u"Ó","o").replace(u"á","a").replace(u"ó","o").encode("utf-8")
results = list()
data = parseData()
if query in data:
result = data[query]
sign = result['sign']
prediction = formatData(query)
print sign+" - "+prediction
results.append(InlineQueryResultArticle(sign, sign, InputTextMessageContent(prediction,parse_mode=telegram.ParseMode.MARKDOWN)))
elif query == "":
for res in data:
result = data[res]
sign = result['sign']
prediction = formatData(res)
print sign+" - "+prediction
results.append(InlineQueryResultArticle(sign, sign, InputTextMessageContent(prediction,parse_mode=telegram.ParseMode.MARKDOWN)))
bot.answerInlineQuery(update.inline_query.id, results)

start_handler = CommandHandler('start', start)
start_handler = CommandHandler('horoscopo', horoscopo,pass_args=True)

inline_caps_handler = InlineQueryHandler(inline_caps)
dispatcher.addHandler(inline_caps_handler)

dispatcher.addHandler(start_handler)
updater.start_polling()


Loading…
Cancel
Save