2018-01-31 12:32:25 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-01-30 13:47:33 +01:00
|
|
|
import logging
|
|
|
|
import os
|
2018-01-31 12:30:08 +01:00
|
|
|
import sys
|
2018-01-30 13:47:33 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import youtube_dl
|
|
|
|
from telegram import MessageEntity
|
|
|
|
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO,
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
|
logger = logging.getLogger("kunsax")
|
|
|
|
|
|
|
|
DIR = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
OUT_DIR = DIR + '/out'
|
|
|
|
|
|
|
|
|
|
|
|
def ytdl_has(url):
|
|
|
|
ies = youtube_dl.extractor.gen_extractors()
|
|
|
|
for ie in ies:
|
|
|
|
if ie.suitable(url) and ie.IE_NAME != 'generic' \
|
|
|
|
and '/channel/' not in url:
|
|
|
|
# Site has dedicated extractor
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def date():
|
2018-01-31 12:30:58 +01:00
|
|
|
return datetime.now().strftime("%Y-%m-%d@%H%M")
|
2018-01-30 13:47:33 +01:00
|
|
|
|
|
|
|
|
2018-01-30 14:21:50 +01:00
|
|
|
def download_ydl(urls):
|
2018-01-30 13:47:33 +01:00
|
|
|
ydl_opts = {
|
|
|
|
'noplaylist': True,
|
|
|
|
'download_archive': DIR + '/downloaded.lst',
|
|
|
|
'outtmpl': OUT_DIR + '/' + date() + '__%(title)s__%(id)s.%(ext)s'
|
|
|
|
}
|
|
|
|
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
2018-01-30 14:21:50 +01:00
|
|
|
ydl.download(urls)
|
2018-01-30 13:47:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
def download_raw(url):
|
2018-01-30 14:21:50 +01:00
|
|
|
ext = ''
|
|
|
|
if 'api.telegram.org' in url and 'animation' in url:
|
|
|
|
ext = '.mp4'
|
|
|
|
local_filename = OUT_DIR + '/' + date() + '__' + url.split('/')[-1] + ext
|
2018-01-30 13:47:33 +01:00
|
|
|
r = requests.get(url, stream=True)
|
|
|
|
with open(local_filename, 'wb') as f:
|
|
|
|
for chunk in r.iter_content(chunk_size=1024):
|
|
|
|
if chunk:
|
|
|
|
f.write(chunk)
|
|
|
|
|
|
|
|
|
2018-01-31 12:30:08 +01:00
|
|
|
# noinspection PyBroadException
|
2018-01-30 13:47:33 +01:00
|
|
|
def handle_url(bot, update):
|
2018-01-30 14:21:50 +01:00
|
|
|
ytdl_urls = list(filter(ytdl_has,
|
|
|
|
map(lambda e: update.message.text[e.offset:e.offset + e.length],
|
|
|
|
filter(lambda e: e.type == 'url',
|
|
|
|
update.message.entities))))
|
|
|
|
if len(ytdl_urls) > 0:
|
|
|
|
logger.info("Downloading %s" % ytdl_urls)
|
|
|
|
update.message.reply_text('Downloading now...')
|
2018-01-31 12:30:08 +01:00
|
|
|
try:
|
|
|
|
download_ydl(ytdl_urls)
|
|
|
|
except:
|
|
|
|
type, value, _ = sys.exc_info()
|
|
|
|
update.message.reply_text("Something is FUCKED: %s, %s" % (type, value))
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection PyBroadException
|
|
|
|
def handle_rest(bot, update):
|
|
|
|
file = None
|
|
|
|
if update.message.photo is not None:
|
|
|
|
photo = max(update.message.photo, key=lambda p: p.width)
|
|
|
|
file = photo.file_id
|
|
|
|
elif update.message.document is not None:
|
|
|
|
file = update.message.document.file_id
|
|
|
|
elif update.message.audio is not None:
|
|
|
|
file = update.message.audio.file_id
|
|
|
|
elif update.message.video is not None:
|
|
|
|
file = update.message.video.file_id
|
|
|
|
if file is not None:
|
|
|
|
url = bot.getFile(file).file_path
|
|
|
|
update.message.reply_text('Downloading now...')
|
|
|
|
logger.info("Downloading '%s'" % url)
|
|
|
|
try:
|
|
|
|
download_raw(url)
|
|
|
|
except:
|
|
|
|
type, value, _ = sys.exc_info()
|
|
|
|
update.message.reply_text("Something is FUCKED: %s, %s" % (type, value))
|
2018-01-30 13:47:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
def start(bot, update):
|
|
|
|
update.message.reply_text('WOOP WOOP')
|
|
|
|
|
|
|
|
|
|
|
|
def error(bot, update, error):
|
|
|
|
logger.error(error)
|
2018-01-30 15:18:17 +01:00
|
|
|
if update is not None:
|
|
|
|
update.message.reply_text("Something is fucked: %s" % error)
|
2018-01-30 13:47:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
updater = Updater("***REMOVED***")
|
|
|
|
|
|
|
|
dp = updater.dispatcher
|
|
|
|
|
|
|
|
dp.add_handler(CommandHandler("start", start))
|
|
|
|
|
|
|
|
dp.add_error_handler(error)
|
|
|
|
|
|
|
|
dp.add_handler(MessageHandler(Filters.entity(MessageEntity.URL), handle_url))
|
2018-01-31 12:30:08 +01:00
|
|
|
dp.add_handler(MessageHandler(Filters.photo | Filters.video | Filters.audio | Filters.document, handle_rest))
|
2018-01-30 13:47:33 +01:00
|
|
|
|
|
|
|
updater.start_polling()
|
|
|
|
|
|
|
|
logger.info("Started Telegram bot...")
|
|
|
|
|
|
|
|
updater.idle()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|