Skip to content

Commit

Permalink
Add support for the new Api{User,Key} system
Browse files Browse the repository at this point in the history
  • Loading branch information
kannibalox committed Dec 4, 2018
1 parent 66e60bc commit c6b8c4b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ Open the file `~/.ptpapi.conf` for editing, and make sure it looks like the foll
[Main]

[PTP]
username=<username>
password=<password>
passkey=<passkey>
ApiUser=<ApiUser>
ApiKey=<ApiKey>
```

This is only the minimum required configuration. See `ptpapi.conf.example` for a full-futured config file with comments.
Both values can be found in the "Security" section of your profile. This is only the minimum required configuration. See `ptpapi.conf.example` for a full-futured config file with comments.

## Concepts

Expand Down Expand Up @@ -111,3 +110,18 @@ By default the script looks for exact matches against file names and sizes. If y
### Notes

I did this mostly for fun and to serve my limited needs, which is why it's not as polished as it could be, and will probably change frequently. Pull requests are welcomed.

#### Deprecated Configuration

The new ApiUser/ApiKey system is preferred, however if you find bugs or limitations, the old cookie-based method can be used as seen here.

Open the file `~/.ptpapi.conf` for editing, and make sure it looks like the following:

```ini
[Main]

[PTP]
username=<username>
password=<password>
passkey=<passkey>
```
12 changes: 9 additions & 3 deletions ptpapi.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
#filter=

[PTP]
# Your ApiUser value
ApiUser=
# Your ApiKey value
ApiKey=

## Deprecated
# Your site username
username=
#username=
# Your site password
password=
#password=
# Your passkey (can be found on upload.php, it's that random string of number and letters inside the announce URL)
passkey=
#passkey=

[Reseed]
# The action to use when creating new files to seed
Expand Down
25 changes: 19 additions & 6 deletions src/ptpapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,30 @@ def __init__(self, username=None, password=None, passkey=None):
j = None
self.cookies_file = os.path.expanduser(config.get('Main', 'cookiesFile'))
LOGGER.info("Initiating login sequence.")
password = (password or config.get('PTP', 'password'))
username = (username or config.get('PTP', 'username'))
passkey = (passkey or config.get('PTP', 'passkey'))
if os.path.isfile(self.cookies_file):
req = None
if config.has_option('PTP', 'ApiUser'):
session.headers.update({
'ApiUser': config.get('PTP', 'ApiUser'),
'ApiKey': config.get('PTP', 'ApiKey')
})
req = session.base_get('index.php')
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 not os.path.isfile(self.cookies_file):
# 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:
Expand All @@ -65,11 +74,15 @@ def __init__(self, username=None, password=None, passkey=None):
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)
ptpapi.util.raise_for_cloudflare(req.text)
LOGGER.info("Login successful.")
self.current_user_id = re.search(r'user.php\?id=(\d+)', req.text).group(1)
self.auth_key = re.search(r'auth=([0-9a-f]{32})', req.text).group(1)

def is_api():
"""Helper function to check for the use of ApiUser"""
return config.has_option('PTP', 'ApiUser')

def logout(self):
"""Forces a logout."""
os.remove(self.cookies_file)
Expand Down
2 changes: 1 addition & 1 deletion src/ptpapi/movie.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def conv_json_torrents(self):

def load_html_data(self):
"""Scrape all data from a movie's HTML page"""
soup = bs4(session.base_get("torrents.php", params={'id': self.ID}).text, "html.parser")
soup = bs4(session.base_get("torrents.php", params={'id': self.ID, 'json': 0}).text, "html.parser")
self.data['Cover'] = soup.find('img', class_='sidebar-cover-image')['src']
# Title and Year
match = re.match(br'(.*)(:? \[(\d{4})\])?', soup.find('h2', class_='page__title').encode_contents())
Expand Down
6 changes: 5 additions & 1 deletion src/ptpapi/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ def request(self, *args, **kwargs):
while not self.consume(1):
LOGGER.debug("Waiting for token bucket to refill...")
sleep(1)
return requests.Session.request(self, *args, **kwargs)
req = requests.Session.request(self, *args, **kwargs)
if req.status_code not in [400, 401, 403]:
# TODO: Any reason to not raise on all bad statuses?
req.raise_for_status()
return req

def get_tokens(self):
if self._tokens < self.capacity:
Expand Down
2 changes: 1 addition & 1 deletion src/ptpapi/torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def load_movie_html_data(self):
if 'GroupId' not in self.data or not self.data['GroupId']:
movie_url = session.base_get('torrents.php', params={'torrentid': self.ID}).url
self.data['GroupId'] = parse_qs(urlparse(movie_url).query)['id'][0]
soup = bs4(session.base_get('torrents.php', params={'id': self.data['GroupId']}).content, "html.parser")
soup = bs4(session.base_get('torrents.php', params={'id': self.data['GroupId'], 'json': 0}).content, "html.parser")
filediv = soup.find("div", id="files_%s" % self.ID)
self.data['Filelist'] = {}
for elem in filediv.find("tbody").find_all("tr"):
Expand Down

0 comments on commit c6b8c4b

Please sign in to comment.