~josealberto4444/apodnasabot

84f3028ac6fc00b4a0b452caf160781f60d8d5cd — josealberto4444 5 years ago 16b0bc2
Minor fixes.
1 files changed, 27 insertions(+), 5 deletions(-)

M cronBot.py
M cronBot.py => cronBot.py +27 -5
@@ 6,6 6,18 @@ import requests
import telebot
from time import gmtime, strftime

def CheckCfg(cfg_file, bName):
	"""Check if the config file exists and return the token if so. If not, create a new config file."""
	if exists(cfg_file):
		config = ConfigParser() # We get the config parser from the module. 
		config.read(cfg_file) # We tell the parser to read this file. ¿Esto no se queda abierto? ¿No habría que cerrar nada? No entiendo esta sintaxis de cosa.algo
		Token = config[bName]['Token']
		return (True, Token)
	else:
		from shutil import copy
		copy("config.cfg.new",cfg_file)
		return (False, "A new configuration file has been created in "+cfg_file+", write there your bot name and the token @BotFather gave you for it.")

def ScrapAndSavePic():
	request = urllib.request.urlopen("http://apod.nasa.gov/apod/astropix.html")
	sourcecode = request.read()


@@ 18,15 30,13 @@ def ScrapAndSavePic():
	tupla = scrap.search(sourcecode).groups() # URL de la imagen
	tupla_a = scrap_a.search(sourcecode).groups() # Nombre del texto explicativo
	tupla_b = scrap_b.findall(tupla_a[0]) # Texto con enlaces TODO: Puede que no pille el texto después del último enlace

	fecha = strftime("%Y-%m-%d", gmtime())
	
	flastpic = open("lastpic.txt",'r')
	lastpic = flastpic.read()
	flastpic.close()
	
	if (tupla[0] != lastpic) or (lastpic == 0):
		pic = urllib.request.urlretrieve('http://apod.nasa.gov/apod/'+tupla[0],fecha+'.jpg')
		pic = urllib.request.urlretrieve('http://apod.nasa.gov/apod/'+tupla[0],'pictures/'+strftime("%Y-%m-%d", gmtime())+'.jpg')
		flastpicw = open("lastpic.txt",'w')
		flastpicw.write(tupla[0])
		flastpicw.close()


@@ 53,7 63,7 @@ def toMarkDown(raw):
	return re.sub('<.*?>', '', charexp) # Quitar todas las etiquetas HTML

def setImgPath():
	return strftime("%Y-%m-%d", gmtime())+'.jpg'
	return 'pictures/'+strftime("%Y-%m-%d", gmtime())+'.jpg'

def send_APOD_photo(chatid):
	IMGPATH = setImgPath()


@@ 72,7 82,19 @@ flastpic = open("lastpic.txt",'r')
lastpic = flastpic.read()
flastpic.close()

TOKEN = '128750232:AAE2n2PAlc1xiql-3kCC7QRi3F3A_YDMfpk'
configFile = 'config.cfg'
botName = 'ApodNasaBot' # Bot name as written in config file.

# We get the token from the config file or create a config file
BToken = CheckCfg(configFile, botName) # We execute the function once and save the result. Comments needed from now on
if BToken[0]:
    TOKEN = BToken[1]
else:
    import sys
    print(BToken[1])
    sys.exit()


bot = telebot.TeleBot(TOKEN)