Feature/Torrentleech v5 API#2121
Feature/Torrentleech v5 API#2121cvium merged 9 commits intoFlexget:developfrom carlba:feature/torrentleech_v5_api
Conversation
…ture/torrentleech_v5_api
|
What would be a good User-Agent to use? The default FlexGet one didn't work so I tried with a couple of others and the default User-Agent of cURL seems to work. |
| """ | ||
| Search for name from torrentleech. | ||
| """ | ||
| task.requests.headers.update({'User-Agent': 'curl/7.54.0'}) |
There was a problem hiding this comment.
This changes the user agent for all plugins in the same task run. Not ideal.
There was a problem hiding this comment.
Agreed when I tested in the beginning I thought setting it globally was needed. But when I retested now it wasn't. I also didn't realise it messed with the requests of the other plugins used in the task.
Will fix.
There was a problem hiding this comment.
Unfortunately my initial tests where correct. The User-Agent has to be set globally otherwise the task will fail when using the download output plugin flexget.plugins.output.download.download_entry. When it tries to get the file it fails if the User-Agent is not set to curl/7.54.0 or something else supported.
Setting it globally affects other plugins so that is not good. I suppose the headers could be passed through the entry entry['download_headers']= {'User-Agent': 'curl/7.54.0'} much like auth is handled in the flexget.plugins.output.download.download_entry method.
| url = ('https://www.torrentleech.org/torrents/browse/list/query/' + | ||
| quote(query.encode('utf-8')) + filter_url) | ||
| log.debug('Using %s as torrentleech search url' % url) | ||
| log.debug('Using {} as torrentleech search url'.format(url)) |
There was a problem hiding this comment.
Change to log.debug('Using %s as torrentleech search url', url)
| entry['content_size'] = parse_filesize(size.group(0)) | ||
| # construct download URL | ||
| torrent_url = 'https://www.torrentleech.org/rss/download/{}/{}/{}'.format( | ||
| torrent['fid'], rss_key, torrent['filename']) |
There was a problem hiding this comment.
Is the linebreak necessary? We allow up to 120 chars on one line.
There was a problem hiding this comment.
I will adjust my IDE accordingly :)
There was a problem hiding this comment.
This particular line is still above 120 chars though.
| entry['torrent_leeches'] = torrent['leechers'] | ||
| entry['search_sort'] = torrent_availability(entry['torrent_seeds'], | ||
| entry['torrent_leeches']) | ||
| entry['content_size'] = torrent['size'] |
There was a problem hiding this comment.
It is in bytes I realized now that is should be in MiB.
There was a problem hiding this comment.
That is why we have a utility function to do the heavy lifting.
There was a problem hiding this comment.
Yes I did of course look at the util but it didn't seem like it was a perfect fit since it is meant to handle strings not integers.
There was a problem hiding this comment.
I will use the util according to your other comment.
| url = ('https://www.torrentleech.org/torrents/browse/list/query/' + | ||
| quote(query.encode('utf-8')) + filter_url) | ||
| log.debug('Using %s as torrentleech search url' % url) | ||
| log.debug('Using {} as torrentleech search url'.format(url)) |
There was a problem hiding this comment.
leave the logger to do its own interpolation:
log.debug('Using %s as torrentleech search url', url)There was a problem hiding this comment.
Good point having gone through have the python logger actually works your suggestion makes more sense :)
| # urllib.quote will crash if the unicode string has non ascii characters, | ||
| # so encode in utf-8 beforehand | ||
|
|
||
| url = ('https://www.torrentleech.org/torrents/browse/list/query/' + |
There was a problem hiding this comment.
i don't think you meant to make the url a tuple
There was a problem hiding this comment.
This will not make it into a tuple it is just a method to construct a multiline string.
| entry['torrent_leeches'] = torrent['leechers'] | ||
| entry['search_sort'] = torrent_availability(entry['torrent_seeds'], | ||
| entry['torrent_leeches']) | ||
| entry['content_size'] = torrent['size'] |
There was a problem hiding this comment.
we have a util that converts size to our requested format:
Flexget/flexget/utils/tools.py
Line 441 in c616922
There was a problem hiding this comment.
The util is according to it's docstring meant to parse a string containing a size to a MiB float. The value returned from the torrentleach API is a int of bytes.
There was a problem hiding this comment.
Yes, so just append 'b' to the int. Not the nicest way and I've been wanting to change the function signature, but it's also very low priority.
There was a problem hiding this comment.
Alright that works for me! Will fix
|
|
||
| try: | ||
| response = task.requests.get(url, auth=auth, raise_status=False) | ||
| response = task.requests.get(url, auth=auth, raise_status=False, headers=headers) |
There was a problem hiding this comment.
This overwrites any header set using the header plugin. None should not be the default.
There was a problem hiding this comment.
No it doesn't override the custom headers specified by the headers plugin. The default value of the headers parameter in the requests library is actually None so it has no affect. But I changed the method to retrieve the custom headers configured in the headers plugin from the config anyway.
There was a problem hiding this comment.
task.requests is a requests Session instance, so it may or may not contain headers set by other plugins. The default should be task.requests.headers.
There was a problem hiding this comment.
Yes but if you pass in headers=None to the get request it will not override the existing headers. Any existing headers are merged with headers passed in to the function.
I did change to task.requests.headers because it is a lot easier to understand and generally nicer.
There was a problem hiding this comment.
Thanks for all the patience with the PR btw :)
|
|
||
| # construct download URL | ||
| torrent_url = 'https://www.torrentleech.org/rss/download/{}/{}/{}'.format(torrent['fid'], rss_key, | ||
| torrent['filename']) |
There was a problem hiding this comment.
I don't think this line break is necessary though
There was a problem hiding this comment.
The limit was 120 chars? Because the line is over that limit.
There was a problem hiding this comment.
I think my mono-space font is missing.
There was a problem hiding this comment.
I just use my IDE ruler :)

Motivation for changes:
The last PR I made used the Torrentleech V4 API as a temporary fix to get the plugin working. This could stop working any time. Implementing support for the new API will futureproof the plugin
Detailed changes:
Addressed issues:
Notes:
This is the first real PR I make to this project so if I have made any mistakes feel free to point them out and I will gladly fix them :)