Beispiel #1
0
class TgHentaiBot(object):

	def __init__(self, config):
		self.CONFIG = config
		self.initDb()
		self.initBot()
		self.initEHentaiDownloader()
		self.initTelegraph()

	def initTelegraph(self):
		self.telegraph = HentaiTelegraph(self.CONFIG['telegraph']['token'], proxy=self.CONFIG['telegraph']['proxy'])

	def initEHentaiDownloader(self):
		self.hentaiDownloader = EHentaiDownloader(self.CONFIG['ehentai']['cookies_file'],
			self.CONFIG['ehentai']['archer_dir'],
			self.CONFIG['ehentai']['proxy'])


	def initDb(self):
		if self.CONFIG['database']['type'].upper() == 'POSTGRESQL':
			self.db = HentaiPgDb(self.CONFIG['database']['host'], 
				self.CONFIG['database']['database'], 
				self.CONFIG['database']['user'],
				self.CONFIG['database']['password'],)
		if self.CONFIG['database']['type'].upper() == 'SQLITE':
			self.db = HentaiSqliteDb(self.CONFIG['database']['path'])

	def initBot(self):
		botRequest = None
		if self.CONFIG['bot']['proxy'] is not None:
			botRequest = telegram.utils.request.Request(
				con_pool_size = 10,
				proxy_url=self.CONFIG['bot']['proxy'],
				connect_timeout=120, read_timeout=1200)
		self.bot = TelegramBot(self.CONFIG['bot']['token'], botRequest=botRequest)


	def addUrls(self, matchUrls, urls=None):
		if urls is None:
			urls = list(map(lambda tmp: tmp[0], matchUrls))
		inUrls = list(map(lambda tmp: tmp['url'], self.db.has_urls(urls)))
		noInUrls = list(filter(lambda url: (url[0] not in inUrls), matchUrls))
		for url in noInUrls:
			res = self.db.get_url(url=url[0])
			if len(res) == 0:
				self.db.add_url(url[0], url[1], url[2], url[3])


	def doUpdates(self):
		maxUpdateId = self.db.get_max_update_id()
		if IS_DEBUG:
			logger.debug('maxUpdateId: {}'.format(maxUpdateId))
		updates = self.bot.get_updates(maxUpdateId + 1)
		for update in updates:
			logger.info('Handle update id: {}'.format(update.update_id))
			if update.channel_post is not None and update.channel_post.chat.type == 'channel' and update.channel_post.chat.id == self.CONFIG['bot']['chat_id'] and update.channel_post.text is not None:
				matchUrls = get_hentai_url_from_text(update.channel_post.text)
				if len(matchUrls) != 0:
					urls = list(map(lambda tmp: tmp[0], matchUrls))
					res = self.db.get_msg(update_id=update.update_id)
					if len(res) == 0:
						self.db.add_msg(update.update_id, update.channel_post.message_id, update.channel_post.chat.id, update.channel_post.text, urls)
					self.addUrls(matchUrls, urls)

	def doSingleDownloadUrl(self, urlData):
		pageUrl = urlData['url']
		logger.info('Download url:{}'.format(pageUrl))
		info = self.hentaiDownloader.download(pageUrl)
		if info is None:
			return None
		self.db.set_downloaded_url(pageUrl, info['title'], info['path'])
		return info

	def doDownload(self):
		datas = self.db.get_undownloaded()
		for data in datas:
			hasDlErr = 0
			updateId = data['update_id']
			for urlData in data['url_array']:
				if urlData is None:
					matchUrls = get_hentai_url_from_text(data['orig_chat_text'])
					self.addUrls(matchUrls)
					continue
				elif urlData['downloaded']:
					continue
				info = self.doSingleDownloadUrl(urlData)
				if info is None:
					hasDlErr = hasDlErr + 1
			if hasDlErr == 0:
				self.db.set_downloaded_msg(updateId)


	def compressImage(self, inputFile, targetSize=5*1024*1024, step=5, quality=100):
		im = Image.open(inputFile)
		if os.path.getsize(inputFile) >= (targetSize-1024):
			while quality>0:
				imgIO = BytesIO()
				im.save(imgIO, 'jpeg', quality=quality)
				if imgIO.tell() < (targetSize-1024):
					im.save(inputFile, 'jpeg', quality=quality)
					break
				quality = quality - step


	def unzip(self, zipFilePath, unZipDir):
		logger.info('Start unzip file: {}'.format(zipFilePath))

		with zipfile.ZipFile(zipFilePath) as existing_zip:
			existing_zip.extractall(unZipDir)


	def upImageDirToTelegraph(self, imageDir):
		logger.info('Upload image dir: {}'.format(imageDir))

		uploadFiles = os.listdir(imageDir)
		uploadFiles = natsorted(uploadFiles)
		uploadFiles = list(map(lambda file: imageDir+'/'+file, uploadFiles))

		# removeFiles = []
		for uploadFile in uploadFiles:
			fileSize = os.path.getsize(uploadFile)
			if int(fileSize/1024) > (5*1024-1):
				self.compressImage(uploadFile)
				# removeFiles.append(uploadFile)

		urls = []
		if len(uploadFiles) > 20:
			up_size = len(uploadFiles)
			part = 20
			temp_pos = 0
			while temp_pos != None :
				new_pos = temp_pos + part
				if new_pos >= up_size:
					new_pos = None
				newUploadFiles = uploadFiles[temp_pos: new_pos]
				temp_urls = upload_file(newUploadFiles, self.CONFIG['telegraph']['proxy'])
				urls.extend(temp_urls)
				temp_pos = new_pos

		else:
			urls = upload_file(uploadFiles, self.CONFIG['telegraph']['proxy'])
		return urls

	def upTelegraphPage(self, title, pathTitle, imageUrls):
		logger.info('Upload telegraph page path: {} title: {}'.format(pathTitle, title))

		telegraphContent = ''
		for imageUrl in imageUrls:
			telegraphContent = telegraphContent + '<img src="'+imageUrl+'">'
		pageRes = self.telegraph.create_page(pathTitle, html_content='None')
		pageRes = self.telegraph.edit_page(pageRes['path'], title, html_content=telegraphContent)
		return pageRes

	def doSingleUpload(self, urlData):
		pageUrl = urlData['url']
		title = urlData['title']
		filePath = urlData['filepath']
		logger.info('Start upload file: {}  url: {}'.format(filePath, pageUrl))
		telegraphTitle = str(self.CONFIG['bot']['chat_id'])  + '-' + urlData['domain'] + '-' + urlData['hentai_id_1'] + '_' + urlData['hentai_id_2']
		unZipDir, ext = os.path.splitext(filePath)

		try:
			self.unzip(filePath, unZipDir)
			urls = self.upImageDirToTelegraph(unZipDir)
			shutil.rmtree(unZipDir)
			if IS_DELETE_ARCHIVER:
				try:
					os.remove(filePath)
				except Exception as delErr:
					logger.info('Delete file error: {} file: '.format(repr(e), filePath))

			createRes = self.upTelegraphPage(title, telegraphTitle, urls)
			logger.info(createRes)
			self.db.set_uploaded_url(pageUrl, 'https://telegra.ph/{}'.format(createRes['path']))
			return createRes
		except Exception as e:
			logger.info('Upload Error: {}'.format(repr(e)))
			return None

	def doUpload(self):
		datas = self.db.get_unuploaded()
		hasErr = 0
		logger.debug('Satrt Upload: {}'.format(datas))
		for data in datas:
			updateId = data['update_id']
			for urlData in data['url_array']:
				if urlData is not None and urlData['uploaded']:
					continue
				res = self.doSingleUpload(urlData)
				if res == None:
					hasErr = hasErr + 1
			if hasErr == 0:
				self.db.set_uploaded_msg(updateId)

	def doEdit(self):
		datas = self.db.get_unedited();
		logger.debug('Satrt Edit: {}'.format(datas))
		for data in datas:
			updateId = data['update_id']
			chatId = data['chat_id']
			msgId = data['message_id']

			originText = data['orig_chat_text']

			urlReps = []
			for urlData in data['url_array']:
				sourceUrl = urlData['url']
				telegraphUrl = urlData['telegraph_url']
				title = urlData['title']
				urlReps.append({
					'title': title,
					'source': sourceUrl,
					'neo': telegraphUrl,
					'domain': urlData['domain']
				})

			logger.info('Edit msg updateId: {} msgId: {} '.format(updateId, msgId))

			data = self.bot.edit_hentai_to_telegraph(chat_id=chatId, message_id=msgId, origin_text=originText, url_datas=urlReps)
			self.db.set_edited(updateId, data)
Beispiel #2
0
from bot import TelegramBot

bot = TelegramBot("config.cfg")


def make_reply(msg):
    reply = None
    if msg is not None:
        reply = 'Okay'
    return reply

update_id = None
while True:
    updates = bot.get_updates(offset=update_id)
    updates = updates["result"]
    if updates:
        for item in updates:
            update_id = item["update_id"]
            try:
                message = str(item["message"]["text"])
            except:
                message = None
            from_ = item["message"]["from"]["id"]
            reply = make_reply(message)
            bot.send_message(reply, from_)