def login(self, username=None, password=None, passkey=None): password = (password or config.get('KG', 'password')) username = (username or config.get('KG', 'username')) response = self.session.post(self.baseURL + "/takelogin.php", data={"username": username, "password": password}).text if response.find('action="takelogin.php"') != -1: raise KGAPIException("Failed to log in")
def download_to_file(self, ID, dest=None): r = self.session.get(self.baseURL + "/down.php/%s/file.torrent" % ID) r.raise_for_status() if not dest: name = re.search(r'filename="(.*)"', r.headers['Content-Disposition']).group(1).replace('/', '_') dest = os.path.join(config.get('Main', 'downloadDirectory'), name) with open(dest, 'wb') as fh: fh.write(r.content)
def download_to_file(self, ID, dest=None): logger = logging.getLogger(__name__) r = self.session.get(self.baseURL + '/download.php', params={'id': ID}) r.raise_for_status() if not dest: name = bencode.bdecode(r.content)['info']['name'].replace('/', '_') + '.torrent' dest = os.path.join(config.get('Main', 'downloadDirectory'), name) logger.debug('Downloading ID {} to {}'.format(ID, dest.encode('utf-8'))) with open(dest, 'wb') as fh: fh.write(r.content)
def need_for_seed(self, filters={}): """List torrents that need seeding""" data = ptpapi.util.snarf_cover_view_data( session.base_get("needforseed.php", params=filters).content) torrents = [] for m in data: torrent = m['GroupingQualities'][0]['Torrents'][0] torrent['Link'] = config.get('Main', 'baseURL') + bs4( torrent['Title'], 'lxml').find('a')['href'] torrents.append(torrent) return torrents
def need_for_seed(self, filters={}): """List torrents that need seeding""" data = ptpapi.util.snarf_cover_view_data( session.base_get("needforseed.php", params=filters).content ) torrents = [] for m in data: torrent = m["GroupingQualities"][0]["Torrents"][0] torrent["Link"] = ( config.get("Main", "baseURL") + bs4(torrent["Title"], "lxml").find("a")["href"] ) torrents.append(torrent) return torrents
def __init__(self, username=None, password=None, passkey=None): self.current_user_id = None j = None self.cookies_file = os.path.expanduser(config.get("Main", "cookiesFile")) logger = logging.getLogger(__name__) LOGGER.info("Initiating login sequence.") if config.has_option("PTP", "ApiUser") and config.has_option("PTP", "ApiKey"): pass elif ( config.has_option("PTP", "password") and config.has_option("PTP", "username") and config.has_option("PTP", "passkey") ): logger.warn( "Using your password/passkey to access the site is deprecated, see README.md for instructions on using the new ApiUser/ApiKey." ) else: logger.critical("No credentials found! Exiting...") sys.exit(78) # EX_CONFIG req = None if config.has_option("PTP", "ApiUser"): session.headers.update( { "ApiUser": config.get("PTP", "ApiUser"), "ApiKey": config.get("PTP", "ApiKey"), } ) elif os.path.isfile(self.cookies_file): self.__load_cookies() # A really crude test to see if we're logged in session.max_redirects = 1 try: req = session.base_get("torrents.php") ptpapi.util.raise_for_cloudflare(req.text) except requests.exceptions.TooManyRedirects: if os.path.isfile(self.cookies_file): os.remove(self.cookies_file) session.cookies = requests.cookies.RequestsCookieJar() session.max_redirects = 3 # If we're not using the new method and we don't have a cookie, get one if not config.has_option("PTP", "ApiUser") and not os.path.isfile( self.cookies_file ): password = password or config.get("PTP", "password") username = username or config.get("PTP", "username") passkey = passkey or config.get("PTP", "passkey") if not password or not passkey or not username: raise PTPAPIException("Not enough info provided to log in.") try: req = session.base_post( "ajax.php?action=login", data={ "username": username, "password": password, "passkey": passkey, }, ) j = req.json() except ValueError: if req.status_code == 200: raise PTPAPIException("Could not parse returned json data.") else: if req.status_code == 429: LOGGER.critical(req.text.strip()) req.raise_for_status() if j["Result"] != "Ok": raise PTPAPIException( "Failed to log in. Please check the username, password and passkey. Response: %s" % j ) self.__save_cookie() # Get some information that will be useful for later req = session.base_get("index.php") ptpapi.util.raise_for_cloudflare(req.text) LOGGER.info("Login successful.")