﻿import requests
import bs4
from bs4 import NavigableString
import re
from telegram import Bot, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup, ReplyKeyboardRemove, InlineQueryResultCachedAudio
from telegram.ext import CommandHandler, Updater, CallbackQueryHandler, Filters, MessageHandler
import logging
import threading
from time import strftime
import shlex
from os import remove, listdir
from os.path import isfile
from oneFRM import *
from urllib.parse import quote
import subprocess
import sys
import time

gotit = 0

sent_none = None


iknowthese = []
if isfile('knownurls.txt'):
	file = open('knownurls.txt')
	lines = file.readlines()
	for line in lines:
		if line.strip() != '':
			iknowthese.append(line.strip())
	file.close()
	
#~ print(iknowthese)
#~ exit()

logging.basicConfig(handlers = [logging.FileHandler('gpb.log', 'w', 'utf-8')], format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.DEBUG)
tgbot = Bot('604436383:AAGh4pg0pCixks8M4EXp222g_UVmXeF5p7E')

#~ updater = Updater(token='604436383:AAGh4pg0pCixks8M4EXp222g_UVmXeF5p7E')
#~ dispatcher = updater.dispatcher

#~ def start(bot, update):
	#~ print(update)
	#~ chat_id = update.message.chat.id
#~ test:
#~ tgbot.send_message(415155183,f'Hi, our chat id is `X`', parse_mode = 'Markdown')
#~ exit()
#~ update.message.reply_text(f'Hi, our chat id is `{chat_id}`', parse_mode = 'Markdown')

#~ dispatcher.add_handler(CommandHandler('start', start))

#~ updater.start_polling()
#~ updater.idle()

def sendnotification(text, parse_mode = None):
	global gotit
	global iknowthese
	if parse_mode == None:
		tgbot.send_message(415155183,text, disable_web_page_preview = True)
		if text.startswith('http'):
			
			gotit += 1
			
			iknowthese.append(text)
			file = open('knownurls.txt', 'w')
			for link in iknowthese:
				file.write(f'{link}\n')
			file.close()

			#~ tgbot.send_message(415155183, link)
			#~ DDL = subprocess.check_output(['python3.6','RDtool.py','-slo',link]).decode('utf-8').strip()
			
			#~ tgbot.send_message(415155183,
			
			
			#~ fnamebase = fname.replace('- S01','S01')
			#~ if 'On Demand' in fname:
				#~ ofname = fnamebase[:-28] + '1080p OD x264-kingofusers.mkv'
			#~ else:
				#~ ofname = fnamebase[:-16] + '1080p TV x264-kingofusers.mkv'
			
			
			
						
			'''res = upload1F(DDL)
				
			if res == 'NeedsLogin':
				#~ print(res)
				SID = login('lucas.halbig@gmail.com','werkenntwen')
				#~ print(SID)
				file = open('1fcooker.txt','w')
				file.write(SID)
				file.close()
				res = upload1F(link)
				if res.endswith('You will be notified when the request is done.'):
					#~ remove(ofname)
					tgbot.send_message(415155183,'1fichier returned that it will send an email once done')
				else:
					print('DAMN')
			else:
				if res.endswith('You will be notified when the request is done.'):
					#~ remove(ofname)
					#~ bot
					tgbot.send_message(415155183,'1fichier returned that it will send an email once done')
			#~ exit()'''
	else:
		tgbot.send_message(415155183,text, parse_mode = parse_mode)
		

	

def CheckPenny():
	print(strftime('%H:%M:%S - Rechecking...'))
	
	global iknowthese
	global sent_none
	
	test = 'http://sharemania.us/threads/penny-on-m-a-r-s-season-1-1080i-hdmania.71403/'



	headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0',
	'Cookie': '__cfduid=d7a8b1b4962d18b22df48d25dbc2cdca01528676207; xf_user=50624%2Ccaece79f8f3f82270731bd8e85c95e3ea0e59016;'+
	'xf_session=435dfbadd48ebc31948294bbf1bfb193'}

	r = requests.get(test, headers = headers)

	#~ print(r.text)

	soup = bs4.BeautifulSoup(r.text, 'html.parser')


	find_art = soup.find('article')

	bq = find_art.find('blockquote')

	bqs = bq.find_all('blockquote')


	def findBtag(soup):
		#~ print('Finding closest bold tag...')
		for elem in soup.previous_elements:
			if not type(elem) == NavigableString:
				if elem.name == 'b':
					return elem
	for idx,bq in enumerate(bqs):
		
		#~ print(f'Now handeling item nr. {idx}')
		epnamebase = findBtag(bq)
		
		#~ <<split('\n')[-1]>> removes any kind of unwanted comnent (if posted "correctly")
		epname = epnamebase.text.split('\n')[-1]
		#~ exit()
		#~ epname = list(bq.previous_elements)[18]
		#~ if type(epname) == NavigableString:
			#~ print(epname)
		#~ else:
			#~ print('This seems like a special to me')
			
			
		bqlines = bq.text.split('\n')
		
		#~ print(bqlines)
		 
		#~ logging.info(f'Value of oldgotit: {gotit}')
		 
		oldgotit = gotit
		 
		links = {} 
		for line in bqlines:
			m = re.fullmatch('(https://.+) tv rip', line)
			
			if m != None:
				link = m.groups()[0].strip()
				links['TVRip'] = link
				if not link in iknowthese:
					sendnotification(f'There\'s a new URL for a *tv rip* release of the following episode *{epname}*', parse_mode ='Markdown') 
					sendnotification(link)
				
			else:
				m = re.fullmatch('(https://.+) on demand', line)
				if m != None:
					link = m.groups()[0].strip()
					links['OnDemand'] = link
					if not link in iknowthese:
						sendnotification(f'There\'s a new URL for a *on demand* release of the following episode *{epname}*', parse_mode ='Markdown') 
						sendnotification(link)
				else:
					m = re.fullmatch('(https://.+)', line)
					if m != None:
						link = m.groups()[0].strip()
						links['Unknown'] = link
						if not link in iknowthese:
							sendnotification(f'There\'s a new URL for a *unknown* release of the following *{epname}*', parse_mode ='Markdown') 
							sendnotification(link)
		#~ print(links)
		#~ for eidx, elem in enumerate(bq.previous_elements):
			#~ print(f'{eidx:02} - {elem}')
	if gotit == oldgotit and gotit < 2:
		strt = time.strftime('%Y-%m-%d %H:%M:%S')
		if sent_none == None:
			msg = tgbot.send_message(415155183,f'No new *Penny on M.A.R.S.* episode available.\nLast checked: `{strt}`', parse_mode ='Markdown')
			sent_none = msg.message_id
		else:
			msg = tgbot.edit_message_text(f'No new *Penny on M.A.R.S.* episode available.\nLast checked: `{strt}`', chat_id = 415155183, message_id = sent_none, parse_mode ='Markdown', timeout = 15)
	logging.info(f'Value of gotit: {gotit}')
	if gotit<2:
		threading.Timer(62, CheckPenny).start()	
CheckPenny()