Initial commit.
This commit is contained in:
		
						commit
						443f096121
					
				
					 3 changed files with 137 additions and 0 deletions
				
			
		
							
								
								
									
										3
									
								
								requirements.in
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								requirements.in
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
python-telegram-bot
 | 
			
		||||
youtube-dl
 | 
			
		||||
requests
 | 
			
		||||
							
								
								
									
										14
									
								
								requirements.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								requirements.txt
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,14 @@
 | 
			
		|||
#
 | 
			
		||||
# This file is autogenerated by pip-compile
 | 
			
		||||
# To update, run:
 | 
			
		||||
#
 | 
			
		||||
#    pip-compile --output-file requirements.txt requirements.in
 | 
			
		||||
#
 | 
			
		||||
certifi==2018.1.18        # via python-telegram-bot, requests
 | 
			
		||||
chardet==3.0.4            # via requests
 | 
			
		||||
future==0.16.0            # via python-telegram-bot
 | 
			
		||||
idna==2.6                 # via requests
 | 
			
		||||
python-telegram-bot==9.0.0
 | 
			
		||||
requests==2.18.4
 | 
			
		||||
urllib3==1.22             # via requests
 | 
			
		||||
youtube-dl==2018.1.27
 | 
			
		||||
							
								
								
									
										120
									
								
								robot.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										120
									
								
								robot.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,120 @@
 | 
			
		|||
import logging
 | 
			
		||||
import os
 | 
			
		||||
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():
 | 
			
		||||
    return datetime.now().strftime("%Y-%m-%d@%H:%M")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def download_ydl(url):
 | 
			
		||||
    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:
 | 
			
		||||
        ydl.download([url])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def download_raw(url):
 | 
			
		||||
    local_filename = OUT_DIR + '/' + date() + '__' + url.split('/')[-1]
 | 
			
		||||
    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)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def handle_url(bot, update):
 | 
			
		||||
    for entity in update.message.entities:
 | 
			
		||||
        if entity.type == 'url':
 | 
			
		||||
            url = update.message.text[entity.offset:entity.offset + entity.length]
 | 
			
		||||
            if ytdl_has(url):
 | 
			
		||||
                update.message.reply_text('Downloading now...')
 | 
			
		||||
                download_ydl(url)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def handle_photo(bot, update):
 | 
			
		||||
    photo = max(update.message.photo, key=lambda p: p.width)
 | 
			
		||||
    url = bot.getFile(photo.file_id).file_path
 | 
			
		||||
    update.message.reply_text('Downloading now...')
 | 
			
		||||
    logger.info("Downloading '%s'" % url)
 | 
			
		||||
    download_raw(url)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def handle_audio(bot, update):
 | 
			
		||||
    url = bot.getFile(update.message.audio.file_id).file_path
 | 
			
		||||
    update.message.reply_text('Downloading now...')
 | 
			
		||||
    logger.info("Downloading '%s'" % url)
 | 
			
		||||
    download_raw(url)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def handle_video(bot, update):
 | 
			
		||||
    url = bot.getFile(update.message.video.file_id).file_path
 | 
			
		||||
    update.message.reply_text('Downloading now...')
 | 
			
		||||
    logger.info("Downloading '%s'" % url)
 | 
			
		||||
    download_raw(url)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def handle_document(bot, update):
 | 
			
		||||
    url = bot.getFile(update.message.document.file_id).file_path
 | 
			
		||||
    update.message.reply_text('Downloading now...')
 | 
			
		||||
    logger.info("Downloading '%s'" % url)
 | 
			
		||||
    download_raw(url)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def start(bot, update):
 | 
			
		||||
    update.message.reply_text('WOOP WOOP')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def error(bot, update, error):
 | 
			
		||||
    logger.error(error)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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))
 | 
			
		||||
    dp.add_handler(MessageHandler(Filters.photo, handle_photo))
 | 
			
		||||
    dp.add_handler(MessageHandler(Filters.audio, handle_audio))
 | 
			
		||||
    dp.add_handler(MessageHandler(Filters.video, handle_video))
 | 
			
		||||
    dp.add_handler(MessageHandler(Filters.document, handle_document))
 | 
			
		||||
 | 
			
		||||
    updater.start_polling()
 | 
			
		||||
 | 
			
		||||
    logger.info("Started Telegram bot...")
 | 
			
		||||
 | 
			
		||||
    updater.idle()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    main()
 | 
			
		||||
		Loading…
	
	Add table
		
		Reference in a new issue