def fetch_releases(self, session, scene_only=True): """ Generator which yields torrent data to be loaded into backend daemons :param session: :type session: sqlalchemy.orm.session.Session :param scene_only: Only fetch scene releases :type scene_only: bool :return: Matched Downloaded torrents :rtype: TorrentData[] """ found = [] try: releases = list(self.get_torrents_browse(50)['torrents'].values()) except (TypeError, KeyError) as err: self.log.debug("Failed to fetch releases") else: if scene_only: releases = [ rls for rls in releases if rls['Origin'] == "Scene" ] for entry in releases: release_name = entry['ReleaseName'] release_info = parser.parse_release( release_name, guess_type=constants.MEDIA_TV) if not release_info: continue section_name = parser.validate_section(release_info) if not section_name: continue if self.exists(session, release_info.release_key ) and not self.is_replacement(release_info): continue #dl_url = self.get_torrent_url(entry['TorrentID']) torrent_data = net.http_request(entry['DownloadURL'], json=False) if not torrent_data: self.log.error( "Failed to download torrent data from server: {0}". format(entry['link'])) continue data = release.TorrentData(str(release_name), torrent_data, section_name) torrent = Torrent.from_str(torrent_data) if not parser.valid_size(torrent, section_name): continue yield data, release_info
def on_created(self, event): """ Handle a new torrent file being saved to the watch directory :param event: File creation event :type event: FileCreatedEvent """ if event.src_path.lower().endswith(".torrent"): log.info("Got new torrent file save event") torrent_name = splitext(basename(event.src_path))[0] dir_path = dirname(event.src_path) try: section = self._path_sections[dir_path] except KeyError: log.error("Unknown watch path detected: {0}".format(dir_path)) return False else: dl_path = config.get_download_path("section_{}".format(section), torrent_name) torrent_data = release.TorrentData(torrent_name, open(event.src_path).read(), section) self._service_manager.add(torrent_data, self, dl_path=dl_path)
def parse_entry(self, session, entry): """ Parse RSS entry data for qualified torrents to download :param session: DB Session :type session: sqlalchemy.orm.session.Session :param entry: RSS Feed entry data :type entry: dict :return: A parsed release object ready to load into backend client or None on fail :rtype: release.TorrentData, None """ release_name = entry.get('title', "") if not release_name: self.log.warning("No title parsed from RSS feed. Malformed?") return False release_info = parser.parse_release(release_name) if not release_info.release_key: self.log.warning( "No release key parsed from release name: {}".format( release_name)) return False release_key = release_info.release_key section_name = parser.validate_section(release_info) if not section_name or section_name == "section_movie": return False if self.exists(session, release_key) and not self.is_replacement(release_info): return False torrent_data = net.http_request(entry['link'], json=False) if not torrent_data: self.log.error( "Failed to download torrent data from server: {0}".format( entry['link'])) return False data = release.TorrentData(bytes(release_name), torrent_data, section_name), release_info torrent = Torrent.from_str(torrent_data) if not parser.valid_size(torrent, section_name): return False return data
def handler(): """ Called when a user used the popup modal to upload a torrent manually :return: Redirect response :rtype: Response """ form = forms.UploadForm() if form.validate_on_submit(): file_data = request.files['torrent_file'].stream.read() file_name = request.files['torrent_file'].filename if file_name.endswith(".torrent"): file_name = file_name[0:len(file_name) - 8] try: torrent_struct = torrent.Torrent.from_str(file_data) tor_data = release.TorrentData(torrent_struct.name, file_data, form.section.data) if ServiceManager.add(Session(), tor_data, WebProvider()): flash( "Torrent {} uploaded successfully".format( torrent_struct.name), "success") else: flash("Failed to upload torrent", "alert") except exceptions.TrannyException as err: flash(err.message, "alert") elif form.errors: for field, error in form.errors.items(): try: flash("[{}] {}".format(field, ' && '.join(error)), "alert") except: pass try: next_url = request.form['next_url'] except KeyError: next_url = url_for("home.index") return redirect(next_url)