~josealberto4444/apodnasabot

b26a3fd35eb174b46db3871b5a3d7156929e7e8a — josealberto4444 5 years ago cf462a8
CronBot running for the first time.
1 files changed, 11 insertions(+), 10 deletions(-)

M cronBot.py
M cronBot.py => cronBot.py +11 -10
@@ 6,11 6,11 @@ import requests
import telebot
from time import gmtime, strftime
from configparser import ConfigParser
from os.path import exists
import os

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):
	if os.path.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']


@@ 33,13 33,13 @@ def ScrapAndSavePic():
	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
	
	flastpic = open("lastpic.txt",'r')
	flastpic = open(os.path.dirname(__file__)+"/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],'pictures/'+strftime("%Y-%m-%d", gmtime())+'.jpg')
		flastpicw = open("lastpic.txt",'w')
		pic = urllib.request.urlretrieve('http://apod.nasa.gov/apod/'+tupla[0], os.path.dirname(__file__)+'/pictures/'+strftime("%Y-%m-%d", gmtime())+'.jpg')
		flastpicw = open(os.path.dirname(__file__)+"/lastpic.txt",'w')
		flastpicw.write(tupla[0])
		flastpicw.close()
	


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

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

def send_APOD_photo(chatid):
	IMGPATH = setImgPath()


@@ 80,11 80,11 @@ def send_APOD_photo(chatid):
		bot.send_photo(chatid, photo, pictitle)
		bot.send_message(chatid, explanation, parse_mode='Markdown', disable_web_page_preview=True)

flastpic = open("lastpic.txt",'r')
flastpic = open(os.path.dirname(__file__)+"/lastpic.txt",'r')
lastpic = flastpic.read()
flastpic.close()

configFile = 'config.cfg'
configFile = os.path.dirname(__file__)+'/config.cfg'
botName = 'ApodNasaBot' # Bot name as written in config file.

# We get the token from the config file or create a config file


@@ 100,7 100,7 @@ else:
bot = telebot.TeleBot(TOKEN)


f = open("subscribers.txt",'r')
f = open(os.path.dirname(__file__)+"/subscribers.txt",'r')

for line in f:
	try:


@@ 108,7 108,7 @@ for line in f:
		#bot.send_message(int(line), "Probando envío automático")
		pass
	except:
		fbug = open("logBugsCron.txt","a")
		fbug = open(os.path.dirname(__file__)+"/logBugsCron.txt","a")
		fbug.write(strftime("%Y-%m-%d", gmtime())+" || "+str(int(line))+"\n")
		fbug.close()
		bot.send_message(int(line), "Te he intentado enviar la APOD del día, pero ha habido un problema. Contacta con @Speedy. =(")


@@ 116,3 116,4 @@ for line in f:
f.close()