-
Notifications
You must be signed in to change notification settings - Fork 276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add AniDex torrent provider #2700
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
# coding=utf-8 | ||
|
||
"""Provider code for AniDex.""" | ||
|
||
from __future__ import unicode_literals | ||
|
||
import logging | ||
import traceback | ||
|
||
from dateutil import parser | ||
|
||
from medusa import tv | ||
from medusa.bs4_parser import BS4Parser | ||
from medusa.helper.common import convert_size | ||
from medusa.logger.adapters.style import BraceAdapter | ||
from medusa.providers.torrent.torrent_provider import TorrentProvider | ||
|
||
from requests.compat import urljoin | ||
|
||
log = BraceAdapter(logging.getLogger(__name__)) | ||
log.logger.addHandler(logging.NullHandler()) | ||
|
||
|
||
class AniDexProvider(TorrentProvider): | ||
"""AniDex Torrent provider.""" | ||
|
||
def __init__(self): | ||
"""Initialize the class.""" | ||
super(self.__class__, self).__init__('AniDex') | ||
|
||
# Credentials | ||
self.public = True | ||
|
||
# URLs | ||
self.url = 'https://anidex.info' | ||
self.urls = { | ||
'search': urljoin(self.url, '/ajax/page.ajax.php'), | ||
} | ||
|
||
# Miscellaneous Options | ||
self.headers = { | ||
'X-Requested-With': 'XMLHttpRequest', | ||
} | ||
|
||
# Torrent Stats | ||
self.minseed = None | ||
self.minleech = None | ||
|
||
# Cache | ||
self.cache = tv.Cache(self, min_time=20) | ||
|
||
def search(self, search_strings, age=0, ep_obj=None): | ||
""" | ||
Search a provider and parse the results. | ||
:param search_strings: A dict with mode (key) and the search value (value) | ||
:param age: Not used | ||
:param ep_obj: Not used | ||
:returns: A list of search results (structure) | ||
""" | ||
results = [] | ||
|
||
search_params = { | ||
'page': 'torrents', | ||
'category': 0, | ||
'filename': '', | ||
'limit': 50, | ||
'offset': 0, | ||
} | ||
|
||
for mode in search_strings: | ||
log.debug('Search mode: {0}', mode) | ||
|
||
for search_string in search_strings[mode]: | ||
|
||
if mode != 'RSS': | ||
log.debug('Search string: {search}', | ||
{'search': search_string}) | ||
|
||
search_params.update({'filename': '{0}'.format(search_string)}) | ||
|
||
response = self.get_url(self.urls['search'], params=search_params, returns='response') | ||
if not response or not response.text: | ||
log.debug('No data returned from provider') | ||
continue | ||
|
||
results += self.parse(response.text, mode) | ||
|
||
return results | ||
|
||
def parse(self, data, mode): | ||
""" | ||
Parse search results for items. | ||
:param data: The raw response from a search | ||
:param mode: The current mode used to search, e.g. RSS | ||
:return: A list of items found | ||
""" | ||
items = [] | ||
|
||
with BS4Parser(data, 'html5lib') as html: | ||
table_header = html.find('thead') | ||
|
||
# Continue only if at least one release is found | ||
if not table_header: | ||
log.debug('Data returned from provider does not contain any torrents') | ||
return items | ||
|
||
table_spans = table_header.find_all('span') | ||
# Skip 'Likes' to have the same amount of cells and labels | ||
labels = [label.get('title') for label in table_spans if label.get('title') != 'Likes'] | ||
|
||
torrent_rows = html.find('tbody').find_all('tr') | ||
for row in torrent_rows: | ||
cells = row.find_all('td') | ||
|
||
try: | ||
title = cells[labels.index('Filename')].span.get_text() | ||
download_url = cells[labels.index('Torrent')].a.get('href') | ||
if not all([title, download_url]): | ||
continue | ||
|
||
download_url = urljoin(self.url, download_url) | ||
|
||
seeders = cells[labels.index('Seeders')].get_text() | ||
leechers = cells[labels.index('Leechers')].get_text() | ||
|
||
# Filter unseeded torrent | ||
if seeders < min(self.minseed, 1): | ||
if mode != 'RSS': | ||
log.debug("Discarding torrent because it doesn't meet the" | ||
" minimum seeders: {0}. Seeders: {1}", | ||
title, seeders) | ||
continue | ||
|
||
torrent_size = cells[labels.index('File size')].get_text() | ||
size = convert_size(torrent_size) or -1 | ||
|
||
date = cells[labels.index('Age')].get('title') | ||
pubdate = parser.parse(date) | ||
|
||
item = { | ||
'title': title, | ||
'link': download_url, | ||
'size': size, | ||
'seeders': seeders, | ||
'leechers': leechers, | ||
'pubdate': pubdate, | ||
} | ||
if mode != 'RSS': | ||
log.debug('Found result: {0} with {1} seeders and {2} leechers', | ||
title, seeders, leechers) | ||
|
||
items.append(item) | ||
except (AttributeError, TypeError, KeyError, ValueError, IndexError): | ||
log.error('Failed parsing provider. Traceback: {0!r}', | ||
traceback.format_exc()) | ||
|
||
return items | ||
|
||
|
||
provider = AniDexProvider() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to use log.exception() in these situations. @labrys what you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Its much cleaner to do
log.exception('Failed parsing provider')
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the same for me, but we should decide what to use. I saw @labrys still replacing it with log.error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my replaces its mostly just mass replaces with regex to deprecate the old brace message format so we can remove it. Not spending much effort on content of messages.