Advertisement
Guest User

Untitled

a guest
Sep 16th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.50 KB | None | 0 0
  1. #============================================================
  2. #Так было
  3. #============================================================
  4.  
  5. #!/usr/bin/env python
  6. # -*- coding: utf-8 -*-
  7.  
  8. import logging
  9. import subprocess
  10.  
  11. from telegram.ext import Updater, CommandHandler
  12.  
  13. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  14.                     level=logging.INFO)
  15.  
  16. logger = logging.getLogger(__name__)
  17.  
  18. TOKEN = "TOKEN"
  19. TRANSMISSION_USER = "TRANSMISSION_USER"
  20. TRANSMISSION_PASSWORD = "TRANSMISSION_PASSWORD"
  21. DEFAULT_DOWNLOAD_PATH = '/DEFAULT/DOWNLOAD/PATH'
  22. DEFAULT_DOWNLOAD_FOLDER = "DEFAULT_DOWNLOAD_FOLDER"
  23. AUTHORIZED_USERS = []
  24.  
  25.  
  26. def error(bot, update, error):
  27.     logger.warn('Update "%s" caused error "%s"' % (update, error))
  28.  
  29.  
  30. def execute_command(cmd, returns=False):
  31.     result = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  32.     output, err = result.communicate()
  33.     if result.returncode is 0:
  34.         if returns:
  35.             return output
  36.     else:
  37.         print(err)
  38.         print(result.returncode)
  39.         print(output)
  40.         raise IOError
  41.  
  42.  
  43. def cmd_add_torrent(magnet=None, location=DEFAULT_DOWNLOAD_FOLDER):
  44.     download_dir = DEFAULT_DOWNLOAD_PATH + location
  45.     cmd = "transmission-remote -n '" + TRANSMISSION_USER + ":" + TRANSMISSION_PASSWORD + "' --add '" + magnet + "'"
  46.     if location != "Temp":
  47.         cmd = cmd + " --download-dir " + download_dir
  48.     execute_command(cmd)
  49.  
  50.  
  51. def add_torrent(bot, update, args):
  52.     if update.message.from_user.id not in AUTHORIZED_USERS:
  53.         bot.sendMessage(update.message.chat_id, text='Get the fuck out of here ' + str(update.message.from_user.id))
  54.         return
  55.  
  56.     magnet_url = args[0]
  57.     try:
  58.         if len(args) == 1:
  59.             cmd_add_torrent(magnet=magnet_url)
  60.             bot.sendMessage(chat_id=update.message.chat_id, text="Torrent added in Temp")
  61.         else:
  62.             content_type = args[1]
  63.             cmd_add_torrent(magnet=magnet_url, location=content_type)
  64.             bot.sendMessage(chat_id=update.message.chat_id, text="Torrent added in " + content_type)
  65.     except Exception as e:
  66.         bot.sendMessage(chat_id=update.message.chat_id, text="Ups, the torrent was not added: " + str(e))
  67.  
  68.  
  69. def main():
  70.     updater = Updater(TOKEN)
  71.     dp = updater.dispatcher
  72.     add_torrent_handler = CommandHandler('add', add_torrent, pass_args=True)
  73.     dp.add_handler(add_torrent_handler)
  74.     dp.add_error_handler(error)
  75.     updater.start_polling()
  76.     updater.idle()
  77.  
  78.  
  79. if __name__ == '__main__':
  80.     main()
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. #============================================================
  103. #А вот так я сделал
  104. #============================================================
  105. #!/usr/bin/env python
  106. # -*- coding: utf-8 -*-
  107.  
  108. import logging
  109. import subprocess
  110.  
  111. proxies = {'https': 'https://66.70.147.196:3128'}
  112.  
  113. from telegram.ext import Updater, CommandHandler
  114.  
  115. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  116.                     level=logging.INFO)
  117.  
  118. logger = logging.getLogger(__name__)
  119.  
  120. TOKEN = "API:KEY"
  121. REQUEST_KWARGS={
  122.     'proxy_url': 'https://66.70.147.196:3128',
  123. #    # Optional, if you need authentication:
  124. #    'urllib3_proxy_kwargs': {
  125. #        'username': 'PROXY_USER',
  126. #        'password': 'PROXY_PASS',
  127. #    }
  128. }
  129.  
  130. TRANSMISSION_USER = "USER"
  131. TRANSMISSION_PASSWORD = "PASSWORD"
  132. DEFAULT_DOWNLOAD_PATH = '/home/liljane/downloads/'
  133. DEFAULT_DOWNLOAD_FOLDER = "torrents"
  134. AUTHORIZED_USERS = [67098759,234252586]
  135.  
  136.  
  137. def error(bot, update, error):
  138.     logger.warn('Update "%s" caused error "%s"' % (update, error))
  139.  
  140.  
  141. def execute_command(cmd, returns=False):
  142.     result = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  143.     output, err = result.communicate()
  144.     if result.returncode is 0:
  145.         if returns:
  146.             return output
  147.     else:
  148.         print(err)
  149.         print(result.returncode)
  150.         print(output)
  151.         raise IOError
  152.  
  153.  
  154. def cmd_add_torrent(magnet=None, location=DEFAULT_DOWNLOAD_FOLDER):
  155.     download_dir = DEFAULT_DOWNLOAD_PATH + location
  156.     cmd = "transmission-remote -n '" + TRANSMISSION_USER + ":" + TRANSMISSION_PASSWORD + "' --add '" + magnet + "'"
  157.     if location != "Temp":
  158.         cmd = cmd + " --download-dir " + download_dir
  159.     execute_command(cmd)
  160.  
  161.  
  162. def add_torrent(bot, update, args):
  163.     if update.message.from_user.id not in AUTHORIZED_USERS:
  164.         bot.sendMessage(update.message.chat_id, text='Get the fuck out of here ' + str(update.message.from_user.id))
  165.         return
  166.     magnet_url = args[0]
  167.     try:
  168.         if len(args) == 1:
  169.             cmd_add_torrent(magnet=magnet_url)
  170.             bot.sendMessage(chat_id=update.message.chat_id, text="Загрузка началась")
  171.         else:
  172.             content_type = args[1]
  173.             cmd_add_torrent(magnet=magnet_url, location=content_type)
  174.             bot.sendMessage(chat_id=update.message.chat_id, text="Torrent added in " + content_type)
  175.     except Exception as e:
  176.         bot.sendMessage(chat_id=update.message.chat_id, text="Ups, the torrent was not added: " + str(e))
  177.  
  178.  
  179. def main():
  180.     updater = Updater(TOKEN, request_kwargs=REQUEST_KWARGS)
  181.     dp = updater.dispatcher
  182.     add_torrent_handler = CommandHandler('', add_torrent, pass_args=True)
  183.     dp.add_handler(add_torrent_handler)
  184.     dp.add_error_handler(error)
  185.     updater.start_polling()
  186.     updater.idle()
  187.  
  188.  
  189. if __name__ == '__main__':
  190.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement