def torrent_view(request): qb = Client('http://127.0.0.1:8080/') qb.login('admin', 'adminadmin') BASE_DIR = Path(__file__).resolve().parent.parent d_path = os.path.join(BASE_DIR, 'static/folder/') print("Location : ", qb.get_default_save_path()) if request.method == 'POST': ezflix = Ezflix(query=request.POST['title1'], media_type='movie', quality='720p', limit=1) shows = ezflix.search() # magnet = 'https://webtorrent.io/torrents/big-buck-bunny.torrent' magnet = 'https://webtorrent.io/torrents/cosmos-laundromat.torrent' if shows is not None: for s in shows: if s['imdb'] == request.POST['imdb1']: print(s['link']) # torrent_location=Torrent.objects.filter(Info_code=s['title'])[0] # print ("Finally",torrent_location.title) # print(torrent_location) qb.download_from_link(magnet, savepath=d_path) time.sleep(90) torrents = qb.torrents(filter='downloading') # for k in info_hash # k.Info code # print("This is the info") path_torrent = Torrent_details(torrents, qb, s['title']) print( '--------------------------------------------------------------------------------------' ) else: print("Not found") return HttpResponse(path_torrent)
def descargar(link): #aqui poner puerto 8080 o el puerto definido en qbit torrent cliente = Client("http://127.0.0.1:4000/") cliente.login("admin", "adminadmin") #en savepath poner donde quiere guardar las peliculas cliente.download_from_link( link, savepath="/home/jaime/compartida/codigo/downloader/descargas/")
def download_torrent(order): qb = Client(URL) qb.login(user, password) app_data = ad.get_library_path() qb.download_from_link(order['magnet'], savepath=app_data['path']) db.insert_order(order) return 'downloading torrent'
def establishRPC(self, client, magnet_link=None, type="magnet"): print "• Establishing connection to", client try: if client == "transmission": tc = transmissionrpc.Client(self.transmission_url.split(":")[0], port=self.transmission_url.split(":")[1], user=self.transmission_user, password=self.transmission_password) if type == "magnet": print "• Adding magnet to", client tc.add_torrent(magnet_link) else: print "• Adding torrent to", client tc.add_torrent('file://' + os.path.abspath('torrent.torrent')) elif client == "qbittorrent": qb = Client(self.qbittorrent_url) qb.login(self.qbittorrent_user, self.qbittorrent_password) if qb._is_authenticated is True: if type == "magnet": print "• Adding magnet to", client qb.download_from_link(magnet_link) else: print "• Adding torrent to", client qb.download_from_file(file('torrent.torrent')) except: traceback.print_exc() raise IOError
class QClientManager(object): def __init__(self, url='http://127.0.0.1:8080/', user='', passwd=''): self.name = self.__class__.__name__ # Default Parameters for QClient Session self.defautl_url = url self.user = user self.passwd = passwd # Launching Session in QClient self.session = Client(self.defautl_url) if self.user != '' and self.passwd != '': self.session.login(self.user, self.passwd) def session_shutdown(self): try: self.session.shutdown() except Exception as e: print(e) return True def get_torrent_info(self): torrents = self.session.torrents() for torrent_item in torrents: print('%s: [%s] \n\t\t- %s' % (self.name, torrent_item['hash'], torrent_item['name'])) def load_magnet(self, magnet_uri): try: self.session.download_from_link(magnet_uri) except Exception as e: print(e) return True
def watch_movie(torrent_link): try: qb = Client('http://' + qbittorrent_conf['ip'] + ':' + qbittorrent_conf['port'] + '/') qb.login(qbittorrent_conf['username'], qbittorrent_conf['password']) qb.download_from_link(torrent_link) except: print(error_message("You must enable qBittorrent Web UI for this to work."))
class Bittorrent: def __init__(self): self.bittorrent = Client('http://localhost:8080/') self.bittorrent.login('pavo', 'buffalo12') def download(self, movie): filename = str(movie.metadata.title) + ' (' + str(movie.metadata.year) + ')' self.bittorrent.download_from_link(movie.metadata.magnet_link, savepath=filename)
def dw_torrent(torrents, magnet_urls, host, login, password): qb = Client(f'http://{host}/') qb.login(login, password) if qb._is_authenticated: qb.download_from_file(torrents) qb.download_from_link(magnet_urls) else: raise NotTorrentClient
def download(logger): # logger 설정 # filename = re.sub('.py', '.log', os.path.basename(__file__)) # log_dir = os.path.dirname(os.path.realpath(__file__)) # logger = custom_logger_v2.set_logger(log_dir, filename) con = sqlite3.connect('db/avlist.db') cur = con.cursor() table_name = 'av_list' # 변수선언 with open('conf/data.yml', 'rt', encoding='UTF8') as f: conf = yaml.load(f, Loader=yaml.FullLoader) qburl = conf['qburl'] qbid = conf['qbid'] qbpwd = conf['qbpwd'] # qBittorrent 연결 qb = Client(qburl) qb.login(qbid, qbpwd) # 미 다운로드 목록을 조회 urls = [] try: sql = "select title, url from av_list where qbittorrent_add = 'n' and url <> ''" cur.execute(sql) rows = cur.fetchall() # print(len(rows)) for row in rows: urls.append((row[0], row[1])) except: logger.info("쿼리 실패") total_count = len(urls) count = 0 for u in urls: logger.info("TITLE : " + u[0] + "\t\t" + "URL : " + u[1]) try: qb.download_from_link(u[1]) logger.info(u[0] + "\t" + "다운로드 요청 전송 성공") sql = "update av_list set qbittorrent_add = 'y' where title = '" + u[ 0] + "'" con.execute(sql) count += 1 # print(sql) except Exception as ex: logger.info(u[0] + "\t" + "다운로드 요청 전송 실패") logger.error(ex) logger.info("총 " + total_count.__str__() + "건 중 " + count.__str__() + "건을 신규로 다운로드 요청하였습니다.") con.commit() con.close() return
def download(): # logger 설정 filename = re.sub('.py', '.log', os.path.basename(__file__)) log_dir = os.path.dirname(os.path.realpath(__file__)) logger = custom_logger_v2.set_logger(log_dir, filename) # sqlite3 db 연결 # 로그 저장할 폴더 생성 # current_dir = os.path.dirname(os.path.realpath(__file__)) # db_dir = '{}/db'.format(current_dir) # if not os.path.exists(db_dir): # os.makedirs(db_dir) con = sqlite3.connect('../db/avlist.db') cur = con.cursor() table_name = 'av_list' # 변수선언 with open('../conf/data.yml', 'rt', encoding='UTF8') as f: conf = yaml.load(f, Loader=yaml.FullLoader) qburl = conf['qburl'] qbid = conf['qbid'] qbpwd = conf['qbpwd'] # qBittorrent 연결 qb = Client(qburl) qb.login(qbid, qbpwd) # 미 다운로드 목록을 조회 urls = [] try: sql = "select title, url from av_list where qbittorrent_add = 'n' and url <> ''" cur.execute(sql) rows = cur.fetchall() print(len(rows)) for row in rows: urls.append((row[0], row[1])) except: logger.info("쿼리 실패") for u in urls: logger.info("TITLE : " + u[0] + "\t\t" + "URL : " + u[1]) try: qb.download_from_link(u[1]) logger.info(u[0] + "\t" + "다운로드 요청 전송 성공") sql = "update av_list set qbittorrent_add = 'y' where title = '" + u[ 0] + "'" con.execute(sql) # print(sql) except: logger.info(u[0] + "\t" + "다운로드 요청 전송 실패") con.commit() con.close() return
def search_database(request): found = False for root, dirs, files in os.walk("/media/HDD1/PlexContent/Movies"): for file in files: if temp[0] in file: found = True if found: return render( request, "movies/dbsearch.html", {"temp": temp[0] + " Found on Plex Server, no need to download!!"}) else: options = Options() options.add_argument("--headless") # Runs Chrome in headless mode. options.add_argument('--no-sandbox') # # Bypass OS security model options.add_argument('start-maximized') options.add_argument('disable-infobars') options.add_argument("--disable-extensions") driver = webdriver.Chrome( chrome_options=options, executable_path= r'/home/littlejiver/Downloads/chromedriver_linux64/chromedriver') # driver = webdriver.Chrome(executable_path=r'/home/littlejiver/Downloads/chromedriver_linux64/chromedriver') wait = WebDriverWait(driver, 10) driver.get("https://www.zooqle.com/") wait.until( ec.element_to_be_clickable( (By.XPATH, '//div[@id="anp2-wrapper"]//div[text()="NO THANKS"]' ))).click() wait.until(ec.element_to_be_clickable( (By.XPATH, '//input[@name="q"]'))).send_keys(temp[0]) searchBar = wait.until( ec.element_to_be_clickable( (By.XPATH, '//div[@class="tt-dataset tt-datas' + 'et-qs"]//p[@class="tt-wrap tt-suggestion ' + 'tt-selectable"]//span[text()="' + temp[0] + '"]'))) searchBar.click() wait.until( ec.element_to_be_clickable(( By.XPATH, '//*[@id="body_container"]/div/div[1]/div[2]/div/table/tbody/tr[2]/td[2]/a' ))).click() magnetLink = wait.until( ec.element_to_be_clickable( (By.XPATH, '//*[@id="dlPanel"]/div[2]/ul/li[2]/a'))) torrentLink = magnetLink.get_attribute("href") qb = Client("http://www.on-demandlogistics.com:8080/") qb.login("admin", "adminadmin") qb.download_from_link(torrentLink) task = torrent_downloading_progress.delay(1) return render( request, "movies/dbsearch.html", { "temp": "Downloading " + temp[0].title() + " now!", 'task_id': task.task_id })
def download_torrent(): """ - Establece la conexión con qBitTorrent - Descarga los ficheros """ qb = Client(config.TORRENT['server']) qb.login(config.TORRENT['user'], config.TORRENT['pass']) qb.download_from_link(torrent_link, savepath=save_path) qb.logout() logger.info('✅ - {} en cola'.format(torrent_link))
def start_download(magnet, name, path): open_qb() try: qb = Client('http://127.0.0.1:8080/') qb.login() qb.download_from_link(magnet, savepath=path) print(name + " started downloading. The files will be saved to " + path + "\n") except Exception as e: print(e)
def Fmodule(n, lst, quality): qb = Client("http://127.0.0.1:8080/") qb.login("admin", "adminadmin") i = 0 j = 0 tor_not_found = [] while n > i: torr_name = str(lst[j] + " " + str(quality)) print("Downloading: " + torr_name) browser = webdriver.Chrome(ChromeDriverManager().install()) browser.get('https://1337x.to/') print("Browser Stated") torr_input = browser.find_element_by_xpath('//*[@id="autocomplete"]') torr_input.send_keys(torr_name) print("Torrent Name Entred") try: click_search = browser.find_element_by_xpath( '//*[@id="search-form"]/button') except NoSuchElementException: click_search = browser.find_element_by_xpath( '//*[@id="search-index-form"]/button') click_search.click() print("Element Searched") try: first_sel = browser.find_element_by_xpath( '/html/body/main/div/div/div/div[3]/div[1]/table/tbody/tr[1]/td[1]/a[2]' ) first_sel.click() print("Torrent Found") except NoSuchElementException: print(torr_name + " Not found") tor_not_found.append(torr_name) j = j + 1 browser.close() continue Murl = browser.find_element_by_xpath( '/html/body/main/div/div/div/div[2]/div[1]/ul[1]/li[1]/a') magnet_link = Murl.get_attribute("href") print("Magnet link copied") browser.close() qb.download_from_link(magnet_link) print("\n Downloading: " + torr_name) i = i + 1 j = j + 1 return tor_not_found
def torrent(self, msg, args): # a command callable with !tryme """ url or magnet add (one by one) """ send_id = msg.to if msg.to == self.bot_identifier: send_id = msg.frm if msg.frm != self.build_identifier(bot_define.BOT_ADMIN_ID): # deny! stream = self.send_stream_request( send_id, open(os.getcwd() + '/resources/deny_new.jpg', 'rb'), name='deny_new.jpg', stream_type='photo') return self.log.info('args: ' + args) validations = ['http://', 'magnet:', 'https://', 'bc://bt/'] if all(not (val in args) for val in validations): stream = self.send_stream_request( send_id, open(os.getcwd() + '/resources/nooo.gif', 'rb'), name='nooo.gif', stream_type='document') return qb = Client(bot_define.TORRENT_URL) yield "Request Login" res = qb.login(bot_define.TORRENT_USER_NAME, bot_define.TORRENT_PASSWORD) if res: yield "Failed to Login" return yield "Request Torrent Job!" res = qb.download_from_link(args) if res: yield "Something has wrong!" return stream = self.send_stream_request( send_id, open(os.getcwd() + '/resources/sloth.gif', 'rb'), name='sloth.gif', stream_type='document') yield "Request Done." qb.logout()
class AutoTorrentDownloader(object): def __init__(self): self.torrent_db_location = "torrents_db_location/torrent_db.txt" self.added_torrents_db_location = "added_torrents_db_location/added_torrent_db.txt" self.client = Client('http://host.docker.internal:8080/') def add_torrent(self, link: str) -> None: self.client.download_from_link(link) def refresh_torrent_list(self): torrent_db = self.get_torrent_db() added_torrent_db = self.get_added_torrent_db() for torrent_link in torrent_db: if torrent_link in added_torrent_db: continue self.add_torrent(torrent_link) self.add_added_torrent(torrent_link) def get_db(self, location: str, mode: str) -> []: db_object = [] with open(location, mode) as db: for line in db: if 'str' in line: break db_object.append(line) return db_object def get_torrent_db(self): return self.get_db(self.torrent_db_location, "r") def get_added_torrent_db(self): return self.get_db(self.added_torrents_db_location, "r") def add_added_torrent(self, link: str): with open(self.added_torrents_db_location, "a") as added_db: added_db.write(link)
class TorrentClient: def __init__(self): self.qb = Client('http://127.0.0.1:8081/') self.qb.login() def get_active_downloads(self): return self.qb.torrents(filter='downloading') def get_completed_downloads(self): return self.qb.torrents(filter='completed') def start_torrents(self, torrent_file_list, save_path): for file in torrent_file_list: self.qb.download_from_file(open(file, 'rb'), savepath=save_path) def start_magnets(self, link_list, save_path): for link in link_list: self.qb.download_from_link(link, savepath=save_path) def get_torrent_status(self): return self.qb.torrents() def get_global_transfer_info(self): return self.qb.global_transfer_info
class TorrentClient(object): def __init__(self): self.conn = None def connect(self, host, username, password, test=False): if self.conn is not None: return self.connect if not host: return {'status': False, 'error': 'host not specified'} try: self.client = Client(host) except Exception as e: logger.error('Could not create qBittorrent Object %s' % e) return {'status': False, 'error': e} else: try: self.client.login(username, password) except Exception as e: logger.error('Could not connect to qBittorrent: %s' % host) return {'status': False, 'error': e} else: if test is True: version = self.client.qbittorrent_version return {'status': True, 'version': version} else: return self.client def find_torrent(self, hash): logger.debug('Finding Torrent hash: %s' % hash) torrent_info = self.get_torrent(hash) if torrent_info: return True else: return False def get_torrent(self, hash): logger.debug('Getting Torrent info hash: %s' % hash) try: torrent_info = self.client.get_torrent(hash) except Exception as e: logger.error('Could not get torrent info for %s' % hash) return False else: logger.info('Successfully located information for torrent') return torrent_info def load_torrent(self, filepath): if not filepath.startswith('magnet'): logger.info('filepath to torrent file set to : %s' % filepath) if self.client._is_authenticated is True: logger.info('Checking if Torrent Exists!') if filepath.startswith('magnet'): torrent_hash = re.findall("urn:btih:([\w]{32,40})", filepath)[0] if len(torrent_hash) == 32: torrent_hash = b16encode(b32decode(torrent_hash)).lower() hash = torrent_hash.upper() logger.debug('Magnet (load_torrent) initiating') else: hash = self.get_the_hash(filepath) logger.debug('FileName (load_torrent): %s' % os.path.basename(filepath)) logger.debug('Torrent Hash (load_torrent): "%s"' % hash) #Check if torrent already added if self.find_torrent(hash): logger.info('load_torrent: Torrent already exists!') return {'status': False, 'error': 'Torrent already exists'} #should set something here to denote that it's already loaded, and then the failed download checker not run so it doesn't download #multiple copies of the same issues that's already downloaded else: logger.info('Torrent not added yet, trying to add it now!') # Build an arg dict based on user prefs. addargs = {} if not any([mylar.CONFIG.QBITTORRENT_LABEL is None, mylar.CONFIG.QBITTORRENT_LABEL == '', mylar.CONFIG.QBITTORRENT_LABEL == 'None']): addargs.update( { 'category': str(mylar.CONFIG.QBITTORRENT_LABEL) } ) logger.info('Setting download label to: %s' % mylar.CONFIG.QBITTORRENT_LABEL) if not any([mylar.CONFIG.QBITTORRENT_FOLDER is None, mylar.CONFIG.QBITTORRENT_FOLDER == '', mylar.CONFIG.QBITTORRENT_FOLDER == 'None']): addargs.update( { 'savepath': str(mylar.CONFIG.QBITTORRENT_FOLDER) } ) logger.info('Forcing download location to: %s' % mylar.CONFIG.QBITTORRENT_FOLDER) if mylar.CONFIG.QBITTORRENT_LOADACTION == 'pause': addargs.update( { 'paused': 'true' } ) logger.info('Attempting to add torrent in paused state') if filepath.startswith('magnet'): try: tid = self.client.download_from_link(filepath, **addargs) except Exception as e: logger.error('Torrent not added') return {'status': False, 'error': e} else: logger.debug('Successfully submitted for add as a magnet. Verifying item is now on client.') else: try: torrent_content = open(filepath, 'rb') tid = self.client.download_from_file(torrent_content, **addargs) except Exception as e: logger.error('Torrent not added') return {'status': False, 'error': e} else: logger.debug('Successfully submitted for add via file. Verifying item is now on client.') if mylar.CONFIG.QBITTORRENT_LOADACTION == 'force_start': logger.info('Attempting to force start torrent') try: startit = self.client.force_start(hash) logger.info('startit returned: %s' % startit) except: logger.warn('Unable to force start torrent - please check your client.') else: logger.info('Client default add action selected. Doing nothing.') try: time.sleep(5) # wait 5 in case it's not populated yet. tinfo = self.get_torrent(hash) except Exception as e: logger.warn('Torrent was not added! Please check logs') return {'status': False, 'error': e} else: logger.info('Torrent successfully added!') filelist = self.client.get_torrent_files(hash) #logger.info(filelist) if len(filelist) == 1: to_name = filelist[0]['name'] else: to_name = tinfo['save_path'] torrent_info = {'hash': hash, 'files': filelist, 'name': to_name, 'total_filesize': tinfo['total_size'], 'folder': tinfo['save_path'], 'time_started': tinfo['addition_date'], 'label': mylar.CONFIG.QBITTORRENT_LABEL, 'status': True} #logger.info(torrent_info) return torrent_info def get_the_hash(self, filepath): import hashlib import bencode # Open torrent file torrent_file = open(filepath, "rb") metainfo = bencode.decode(torrent_file.read()) info = metainfo['info'] thehash = hashlib.sha1(bencode.encode(info)).hexdigest().upper() return thehash
root.update() print("Please select the file of your list") sleep(2) csv_filepath = filedialog.askopenfilename(initialdir="/", title="Select list file", filetypes=[("csv files", "*.csv") ]) print("Please select directory for the result") directory1 = filedialog.askdirectory(initialdir="/", title="Select target directory") failed = [] with open(csv_filepath) as csv_file, open(directory1 + "/magnets.txt", 'w') as res: movies = csv.reader(csv_file, delimiter=",") while not (movie := next(movies)) or not movie[2].isnumeric(): pass # skip until the movies start handle_movie(movie) for movie in movies: handle_movie(movie) print("Here's the movies I couldn't find:") print("\n\t" + "\n\t".join(failed)) print("Select target directory") directory2 = filedialog.askdirectory(initialdir="/", title="Select target directory") qb = Client("http://127.0.0.1:8080/") qb.login("admin", "administrator") with open(directory1 + "/magnets.txt", 'r') as magnets: for magnet in magnets: qb.download_from_link(magnet, savepath=directory2)
download_id = re.match(r'.*?details.php\?id\=(\d+)(.*)',str(soup_result_td)).group(1) size = getSize(soup_result_td[9].text) seeder = soup_result_td[11].text return download_id, size, seeder t=login() for tr in getFreeTorrent(t,"sticky_top"): if 'Free' in str(tr.contents[3]) and 'hitandrun' not in str(tr.contents[3]): download_id, size, seeder=getInfo(tr) download_link = "https://ourbits.club/download.php?id=" + download_id + "&passkey=" + passkey + "&https=0" result = my_set.find_one({"id":download_id}) link_list = [download_link] if result is None: qb.download_from_link(link_list) my_set.insert_one({"id":download_id,"href":download_link,"size":size,"seeder":seeder,"top":1,"deal":0}) else: my_set.update_one({"id":download_id},{"$set":{"seeder":seeder}}) for tr in getFreeTorrent(t,"sticky_normal"): if 'Free' in str(tr.contents[3]) and 'hitandrun' not in str(tr.contents[3]): download_id, size, seeder=getInfo(tr) download_link = "https://ourbits.club/download.php?id=" + download_id + "&passkey=" + passkey + "&https=0" link_list = [download_link] result = my_set.find_one({"id":download_id}) if result is None: if len(qb.torrents(filter='downloading')) < 2: qb.download_from_link(link_list) my_set.insert_one({"id":download_id,"href":download_link,"size":size,"seeder":seeder,"top":0,"deal":0}) else:
class AutoTorrent: def __init__(self): self.init_google_sheets() self.init_qb() self.downloading = {} def init_google_sheets(self): creds = None # The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', SCOPES) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open('token.pickle', 'wb') as token: pickle.dump(creds, token) self.google_sheets = build('sheets', 'v4', credentials=creds) def init_qb(self): self.qb = TorrentClient("http://127.0.0.1:8080/") self.qb.login("admin", "adminadmin") def get_movies(self): sheet = self.google_sheets.spreadsheets() result = sheet.values().get(spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME).execute() values = result.get('values', []) if not values: print('No data found.') filtered = filter( lambda m: m[0] in ["No Descargada", "Auto Descargando"] and (len(m) < 5 or m[4] not in ["No", "Pocos Seeds"]), values) movies = map(lambda m: Movie(m[1], m[2]), filtered) return movies def download_movie(self, movie): torrent = movie.get_torrent() if not torrent: print(" Torrent not found for {}. Skipping".format(movie)) self.update_movie_yts(movie, "No") return magnet = torrent.magnet path = movie.local_path if not os.path.isdir(path): os.mkdir(path) self.qb.download_from_link(magnet, savepath=path) self.update_movie(movie, "Auto Descargando") for t in self.qb.torrents(): if os.path.normpath(t["save_path"]) == os.path.normpath(path): torrent_hash = t["hash"] break self.downloading[torrent_hash] = movie print(" Started downloading {}".format(movie)) def update_movie(self, movie, status): sheet = self.google_sheets.spreadsheets() result = sheet.values().get(spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME).execute() values = list(result.get('values', [])) for value in values: if value[1] == movie.name and int(value[2]) == movie.year: value[0] = status body = {'values': values} sheet.values().update(spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME, valueInputOption=VALUE_INPUT_OPTION, body=body).execute() def update_movie_yts(self, movie, yts): sheet = self.google_sheets.spreadsheets() result = sheet.values().get(spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME).execute() values = list(result.get('values', [])) for value in values: if value[1] == movie.name and int(value[2]) == movie.year: if len(value) == 4: value.append(yts) else: value[4] = yts body = {'values': values} sheet.values().update(spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME, valueInputOption=VALUE_INPUT_OPTION, body=body).execute() def should_download(self): current_torrents = len(self.qb.torrents(filter="downloading")) less_than_max = current_torrents < MAX_CONCURRENT_DOWNLOADS just_started = len(self.downloading) < MAX_CONCURRENT_DOWNLOADS return less_than_max or just_started def start(self): movies = self.get_movies() while True: sleep(1) self.clean_torrents() if not self.should_download(): continue movie = next(movies) print(movie) self.download_movie(movie) def clean_torrents(self): timed_out = [] done = [] for torrent in self.qb.torrents(): if torrent["progress"] == 1: done.append(torrent["hash"]) elif is_timed_out(torrent): timed_out.append(torrent["hash"]) for h in timed_out: if h not in self.downloading: continue movie = self.downloading[h] print("{} timed out. Removing".format(movie)) self.update_movie(movie, "No Descargada") self.update_movie_yts(movie, "Pocos Seeds") self.qb.delete(h) movie.delete_folder() for h in done: if h not in self.downloading: continue movie = self.downloading[h] self.update_movie(movie, "Descargada") self.qb.delete(h) movie.clean_folder() movie.move_to_drive() print("{} finished downloading. Moving to Google Drive".format( movie))
to_download = list(to_download) # If there aren't any items, exit if not to_download: print('All links already available, no magnets added') exit() # Save new episodes to file with open('hliang_tvbs_queen_episode_list.txt', 'a+') as filehandle: filehandle.writelines("%s\n" % episode for episode in sorted(to_download)) # Run through every url, extracting the magnet links magnets = [] #for _, url in items.items(): for episode in to_download: url = items[episode] r = requests.get(url) soup = BeautifulSoup(r.text, 'html.parser') magnets.append(soup.find_all('a', attrs={'id':'magnet'})[0]['href']) # Add to torrent client qb = Client('http://127.0.0.1:8080/') qb.login('admin', 'password') # Password omitted dl_path = '/location' for magnet in magnets: qb.download_from_link(magnet, savepath=dl_path) amount = len(magnets) print(f'{amount} magnets added!')
def qbittorren_torrent(magnetic_link, name, quality): qb = Client('http://127.0.0.1:8080/') qb.login() qb.download_from_link(magnetic_link, label=f'{name}+{quality}')
def Download(url): qb = Client("http://localhost:8080/") qb.login("your username", "your password") qb.download_from_link(url)
new_files = list(set(files) - set(last_torrent_files)) if last_info_hash is None: print("Добавление торрента с {} файлами: {}".format( len(new_files), new_files)) else: print('Торрент изменился, пора его перекачивать') print("Добавлено {} файлов: {}".format( len(new_files), new_files)) last_info_hash = info_hash last_torrent_files = files # Say qbittorrent client download torrent file # OR: qb.download_from_file qb.download_from_link(torrent_file_url) # Отправляю смс на номер text = "Вышла новая серия '{}'".format( torrent['info']['name']) simple_send_sms(text) # Даем 5 секунд на добавление торрента в клиент import time time.sleep(5) remove_previous_torrent_from_qbittorrent(qb, info_hash) else: print('Изменений нет')
class Server: MAGNET_URI = r"magnet:\?xt=urn:btih:[a-zA-Z0-9]*" COMMAND_GET_DLS = "__listdownloaded__" COMMAND_GET_TORRENTS = "__listtorrents__" COMMAND_GET_TIME = "__gettime__" COMMAND_GET_DIRECTORIES = "__getdirectories__" COMMAND_POST_REFRESH = "__refreshplex__" def __str__(self): return \ "Handles TCP connections between this and client and manages downloads." def __init__(self, config, _logger, buffer=1024): self.config = config self.logger = _logger self.buffsize = buffer self.DEFAULT_DOWNLOADED_FILE_PATH = config.get("General", "savepath") self.plex_directories = literal_eval(config.get("Plex", "directories")) # set time as a property self._time = datetime.datetime.now() self.login() self.autoUpdater = AutoUpdater(config, _logger=self.logger) @property def time(self): return str(self._time)[:-7] @time.setter def time(self, value): self._time = value def start(self): # self.listen() # self.autoUpdater.start() server_prcs = Process(target=self.listen) updater_prcs = Process(target=self.autoUpdater.start) server_prcs.start() updater_prcs.start() def login(self): self.myPlex = myplex.MyPlexAccount( username=self.config.get("Plex", "username"), password=self.config.get("Plex", "password")) self.client = Client(self.config.get("qBittorrent", "host")) self.client.login(self.config.get("qBittorrent", "username"), self.config.get("qBittorrent", "password")) def listen(self): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind((self.config.get("Server", "host"), self.config.getint("Server", "port"))) print("listening on %s:%i...." % (self.config.get( "Server", "host"), self.config.getint("Server", "port"))) self.logger.log("SERVER LISTENING") try: while 1: s.listen() cnn, addr = s.accept() threading.Thread(target=self.acceptClient, args=(cnn, addr)).start() # occasional crash, restart the listen? except ConnectionResetError as e: self.logger.log(str(e)) def acceptClient(self, cnn, addr): try: while 1: data = cnn.recv(self.buffsize) decoded = data.decode() self.logger.log("CONNECTION: %s: %s" % (str(addr), decoded)) if not data: break if re.match(self.MAGNET_URI, decoded[1:]): resp = self.downloadTorrent(decoded[1:], decoded[0]) encoding_resp = bytes(resp, encoding="utf-8") cnn.sendall(encoding_resp) elif decoded == self.COMMAND_GET_DLS: resp = bytes(self.getDownloadList, encoding="utf-8") cnn.sendall(resp) elif decoded == self.COMMAND_GET_TORRENTS: resp = bytes(self.getTorrentList, encoding="utf-8") cnn.sendall(resp) elif decoded == self.COMMAND_POST_REFRESH: self.updateLibrary() cnn.sendall(b"updated plex library") elif decoded == self.COMMAND_GET_TIME: resp = bytes(self.time, encoding="utf-8") cnn.sendall(resp) elif decoded == self.COMMAND_GET_DIRECTORIES: resp = bytes(self.getPlexDirectories, encoding="utf-8") cnn.sendall(resp) else: cnn.sendall(b"invalid request") except ServerRequestError as e: cnn.sendall(b"An error has occurred please try again.") self.logger.log("SERVER ERROR - " + str(e)) except Exception as e: cnn.sendall( bytes(f"An unkown error has occurred. {str(e)}", encoding="utf-8")) self.logger.log(f"SERVER ERROR - {type(e)} - {str(e)}") def downloadTorrent(self, uri, pathIndex): def download(): self.client.download_from_link( uri, savepath=self.DEFAULT_DOWNLOADED_FILE_PATH) # return the extracted hash from the uri # hash appears before the name in the uri and is wrapped in identifiable tags # parse out any http uri syntax magnet = unquote(uri) magnet = re.sub(r'magnet:\?xt=urn:btih:', '', magnet) magnet = magnet[:magnet.index('&dn=')] # if the string now matches a hash regex if re.match('[a-zA-Z0-9]*', magnet): return magnet else: return None def getAppropriateFilePath(torrent, pathIndex): import difflib def getSeasonSubDir(name, season, path): dirs = [ f for f in os.listdir(path) if not os.path.isfile(os.path.join(path, f)) ] # find existing season subfolder for d in dirs: if ("s" + season).lower() in d.lower() or \ ("season " + season).lower() in d.lower(): return path + "\\" + d # directory doesnt exist, create and return newdir = "%s\\%s Season %s" % (path, name, season) os.mkdir(newdir) return newdir client_path = self.plex_directories[pathIndex] try: if '.' in torrent['name']: t_split = torrent['name'].split('.') elif ' ' in torrent['name']: t_split = torrent['name'].split(' ') # find if torrent has seasons. # usual syntax is SXXEXX # group1- name # group2 - season (value) # group3 - episode (value) # group4 - rest regex = re.match(r'(.*?)\.S?(\d{1,2})E?(\d{2})\.(.*)', torrent['name'].replace(' ', '.')) if regex: # find the best fitting season folder # gets the media name for i, st in enumerate(t_split): if re.match(r"[Ss](\d{1,2})[Ee](\d{1,2})", st) or st.lower() == "season": # presume name is up to this index media_name = ' '.join(t_split[:i]) self.logger.log(f"media_name: {media_name}") break # get all folders dirs = [ f for f in os.listdir(client_path) if not os.path.isfile(os.path.join(client_path, f)) ] for d in dirs: # if the ratio is acceptable if difflib.SequenceMatcher( None, a=media_name.lower(), b=d.lower()).ratio() >= 0.55: # we should try to find the right season folder tv_dir = getSeasonSubDir(media_name, regex.group(2), client_path + "\\" + d) return tv_dir return client_path + "\\" + torrent['name'] except NameError: return client_path + "\\" + torrent['name'] except Exception as e: self.logger.log("ERROR getAppropiateFilePath: %s - %s" % (e, torrent["name"])) return self.DEFAULT_DOWNLOADED_FILE_PATH def overrideFilePath(hash): # use old api for torrent.set_location from qbittorrentapi import Client as xClient xc = xClient(self.config.get("qBittorrent", "host"), self.config.get("qBittorrent", "username"), self.config.get("qBittorrent", "password")) xt = next((x for x in xc.torrents.info.downloading() if x["hash"].lower() == hash.lower()), None) if xt is not None: # print("suitable location for %s %s" % (xt["name"], new_save_path)) self.logger.log("WRITING %s TO %s" % (xt["name"], new_save_path)) xt.set_location(new_save_path) try: torrent_hash = download() self.logger.log("TORRENT ADDED: %s" % uri) self.client.sync() # use the extracted hash to fetch the just added torrent t = next((x for x in self.client.torrents() if x["hash"].lower() == torrent_hash.lower()), None) if t is not None: #kinda bugs me how this call is here new_save_path = getAppropriateFilePath(t, int(pathIndex)) # override file path. overrideFilePath(torrent_hash) return "%s\n%s" % (t["name"], new_save_path) except HTTPError as e: self.login() raise ServerRequestError(e, self.downloadTorrent.__name__) except Exception as e: raise ServerRequestError(e, self.downloadTorrent.__name__) def updateLibrary(self): res = self.myPlex.resource(self.config.get("Plex", "server")).connect() res.library.update() @property def getDownloadList(self): # list all downloaded folders paths = [f for f in os.listdir(self.DEFAULT_DOWNLOADED_FILE_PATH)] return ','.join(paths) @property def getTorrentList(self): torrents = [] try: _t = self.client.torrents() except HTTPError as e: # 403 forbiddent exception (cant find exception type) # try relogin self.login() raise ServerRequestError(e, self.getTorrentList.__name__) else: for t in _t: torrents.append( str(t["hash"]) + "~" + t["name"] + "~" + str(t["progress"]) + "~" + t["state"]) return '\n'.join(torrents) @property def getPlexDirectories(self): # ? is a protected char why not return '?'.join(self.plex_directories) def getDiskUsages(self): from shutil import disk_usage usages = [(str, float, int)] for dir in self.plex_directories: du = disk_usage(dir) # disk usage # (total, used, free) usages.append((dir, (du[2] / du[0]) * 100, du[2])) return usages
def main_proc(): print('\n#################### [ Begin - Running at ' + time.ctime() + ' ] ##########') # load config with codecs.open('config.json', 'r', encoding='utf8') as f: cfg = json.loads(f.read()) torrentcfg = cfg['torrent'] policycfg = cfg['downloadpolicy'] dbcfg = cfg['db'] crawlcfg = cfg['crawl'] # init db db = DownloadDb(dbcfg) # get qbittorrent connection q = Client(torrentcfg['addr']) errmsg = q.login(torrentcfg['user'], torrentcfg['pwd']) if errmsg: print('Torrent server ' + errmsg, file=sys.stderr) # crawl t = TorrentKim3Net( crawlinfo = crawlcfg['torrentkim3.net'], downloadpolicy = policycfg ) l = [] for i in range(1, 3+1): l += t.getlist_tvdrama(page=i) l += t.getlist_variety(page=i) l += t.getlist_docu(page=i) print('\n########## Crawl torrentkim3.net') for each in l: subj = each['subject'] matched = t.filtersubject(subj) if not matched: try: print('not matched : {}'.format(subj)) except UnicodeEncodeError: print('not matched : {}'.format(subj.encode('cp949', 'replace'))) continue magnet = t.getmagnetfrom(each['href']) if not magnet: print('failed to get magnet : ' + subj) continue if db.isadded(magnet): print('already added : ' + subj) else: q.download_from_link(magnet) print('added : '+ subj) db.added(magnet) db.sync() time.sleep(1) # check qbittorrent status print('\n########## QBittorrent Status') for each in q.torrents(): progress = each['progress'] percent = str(100 * progress) + ' %' name = each['name'] magnet = 'magnet:?xt=urn:btih:' + each['hash'].lower() tr_files = map(lambda x: x['name'], q.get_torrent_files(each['hash'])) print('+ ', percent + ' | ' + name + ' | ' + magnet) for each_file in tr_files: try: print('+-- ' + each_file) except UnicodeEncodeError: print('+-- {}'.format(each_file.encode('cp949', 'replace'))) if progress == 1 and not db.isdownloaded(magnet): db.downloaded(magnet) db.sync() print('\n#################### [ End - Running at ' + time.ctime() + ' ] ##########') time.sleep (300)
if (magnetLinks[0] == 'noResult'): magnetLinks.pop(0) searchAr.pop(0) continue #get torrents downloading now torrents = qb.torrents() currentTorrent = None #figure out which folder to go into: pathString = createMovie_dir(searchAr[0]) try: os.mkdir(pathString) except: pass qb.download_from_link(magnetLinks[0], savepath=pathString, category=catgegory_) torrents = qb.torrents() for i in torrents: if catgegory_ == i['category']: qb.resume(i['hash']) downloading = True while downloading: tor = qb.torrents(category=catgegory_) wait_time = None while wait_time == None: try: wait_time = int(tor[0]['eta']) / 3
class QBittorrentDownloader: """ Class that uses the qBittorrent Server API to download torrents """ def __init__(self, url: str, username: str, password: str, download_dir: str): """ :param url: :param username: :param password: :param download_dir: """ self.client = Client(url) self.client.login(username, password) self.download_dir = download_dir @classmethod def from_config(cls) -> "QBittorrentDownloader": """ :return: A QBittorrentDownloader object based on the stored configuration files """ config = Config.load() return cls(config.qbittorrent_address, config.qbittorrent_username, config.qbittorrent_password, config.qbittorrent_download_dir) def download(self, torrents: List[TorrentDownload]): """ Downloads a list of torrent files :param torrents: The torrents to download :return: None """ for torrent in torrents: torrent_info = torrent.torrent_info print(f"Downloading Torrent: {torrent_info.filename}") if torrent_info.magnet_link is not None: self.client.download_from_link(torrent_info.magnet_link) else: assert torrent_info.torrent_file is not None torrent_file = torrent_info.torrent_file if not os.path.isfile(torrent_file): torrent_file = "/tmp/torrentdltemp.torrent" content = requests.get(torrent_info.torrent_file).content with open(torrent_file, "wb") as f: f.write(content) with open(torrent_file, "rb") as f: self.client.download_from_file(f) time.sleep(1) while len(self.client.torrents()) > 0: for active in self.client.torrents(): if active["state"] not in [ "downloading", "metaDL", "stalledDL" ]: print("Done. ") torrent_path = os.path.join(self.download_dir, active["name"]) if os.path.isdir(torrent_path): children = [ os.path.join(torrent_path, x) for x in os.listdir(torrent_path) ] children.sort(key=lambda x: os.path.getsize(x), reverse=True) torrent_path = children[0] ext = torrent_path.rsplit(".", 1)[1] torrent.add_extension(ext) self.client.delete(active["hash"]) if os.path.isdir(torrent.destination): shutil.move( torrent_path, os.path.join(torrent.destination, os.path.basename(torrent_path))) else: shutil.move(torrent_path, torrent.destination) else: print(f"{(100 * active['progress']):.2f}%", end="\r") time.sleep(1)
if magnet: season[title].insert(0, magnet) finally: browser.quit() return season def get_dl_path(): # TODO: Check if this drive has space, else check another drive # if there's no free space, crash return 'M:/' if __name__ == "__main__": season = get_season() try: # Web UI -> 'Bypass authentication for hosts on localhost' should be enabled # Downloads -> 'Do not start download automatically' should be enabled qb = Client('http://127.0.0.1:8080/') # Use DP to decrease show fetch time for path, magnets in season.items(): for magnet in magnets: qb.download_from_link(magnet, savepath='{}{}'.format( get_dl_path(), path), category='anime') qb.resume_all() except ConnectionError: print('[!] qBittorrent not active!')
def bit(bot): global update_id global current_queue for update in bot.get_updates(offset=update_id, timeout=10): update_id = update.update_id + 1 if update.message: if update.message.text == None: continue elif update.message.text.startswith("magnet:?"): qb = Client('http://127.0.0.1:8081/') qb.download_from_link(update.message.text) temp = qb.torrents() for dictn in temp: if dictn['hash'] not in current_queue: current_queue[dictn['hash']] = [update.message.chat_id, dictn['name']] update.message.reply_text("Torrent has been added to the list.") elif update.message.text.startswith("/anime"): qb = Client('http://127.0.0.1:8081/') dl_path = 'Y:/' qb.download_from_link(update.message.text[7:], savepath = dl_path) temp = qb.torrents() for dictn in temp: if dictn['hash'] not in current_queue: current_queue[dictn['hash']] = [update.message.chat_id, dictn['name']] update.message.reply_text("Torrent has been added to the list ~desu \n\n^_^") elif update.message.text.startswith("/tv") or update.message.text.startswith("/TV"): qb = Client('http://127.0.0.1:8081/') dl_path = 'X:/' qb.download_from_link(update.message.text[4:], savepath = dl_path) temp = qb.torrents() for dictn in temp: if dictn['hash'] not in current_queue: current_queue[dictn['hash']] = [update.message.chat_id, dictn['name']] update.message.reply_text("Torrent has been added to the list.") elif update.message.text == "/status": temp = gettorrents() queue = 0 downloading = 0 for item in temp: if item['state'] == ("State: " + 'downloading'): downloading += 1 if item['state'] == ("State: " + 'queuedDL'): queue += 1 update.message.reply_text(str(len(temp)) + "/20 torrents are active.\n" + str(downloading) +"/5 are downloading, and " + str(queue) + "/15 are in the queue.") elif update.message.text == "/queue": temp = gettorrents() tempstring = "Current Active Torrents:\n \n" for dictn in temp: tempstring += dictn['name'] + '\n' tempstring += dictn['eta'] + ", " + dictn['progress'] + ", " + dictn['state'] + "\n" + "\n" update.message.reply_text(tempstring) elif update.message.text.startswith("/delete"): temp = gettorrents() for item in temp: if item['name'] == ("Torrent Name: " + update.message.text[8:]): qb = Client('http://127.0.0.1:8081/') qb.delete_permanently(item['hash']) current_queue.pop(item['hash']) temp = gettorrents() tempstring = "Current Active Torrents:\n \n" for dictn in temp: tempstring += dictn['name'] + '\n' tempstring += dictn['eta'] + ", " + dictn['progress'] + ", " + dictn['state'] + "\n" + "\n" update.message.reply_text(tempstring)