-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from Hungry-Dolphin/HowToContribute
Implemented issue #30, the nyaa search now returns a torrent object instead of a dict
- Loading branch information
Showing
11 changed files
with
502 additions
and
316 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,7 @@ dist/ | |
nyaapy.egg-info | ||
.vscode | ||
env/ | ||
*.pyc | ||
*.pyc | ||
test_files | ||
venv | ||
.idea |
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 |
---|---|---|
@@ -1,11 +1,7 @@ | ||
# Info about the module | ||
__version__ = '0.6.0' | ||
__version__ = '0.6.3' | ||
__author__ = 'Juanjo Salvador' | ||
__email__ = '[email protected]' | ||
__url__ = 'http://juanjosalvador.me' | ||
__copyright__ = '2017 Juanjo Salvador' | ||
__license__ = 'MIT license' | ||
|
||
from NyaaPy.nyaa import Nyaa | ||
from NyaaPy.pantsu import Pantsu | ||
from NyaaPy.sukebei import SukebeiNyaa, SukebeiPantsu | ||
__license__ = 'MIT license' |
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 |
---|---|---|
@@ -1,54 +1,74 @@ | ||
import requests | ||
import urllib.parse | ||
from bs4 import BeautifulSoup | ||
from NyaaPy import utils | ||
from NyaaPy import torrent | ||
|
||
|
||
class Nyaa: | ||
|
||
def __init__(self): | ||
self.URI = "http://nyaa.si" | ||
self.SITE = utils.TorrentSite.NYAASI | ||
self.URL = "https://nyaa.si" | ||
|
||
def last_uploads(self, number_of_results): | ||
r = requests.get(self.URI) | ||
soup = BeautifulSoup(r.text, 'html.parser') | ||
rows = soup.select('table tr') | ||
r = requests.get(self.URL) | ||
|
||
# If anything up with nyaa servers let the user know. | ||
r.raise_for_status() | ||
|
||
return utils.parse_nyaa(table_rows=rows, limit=number_of_results + 1) | ||
json_data = utils.parse_nyaa( | ||
request_text=r.text, | ||
limit=number_of_results + 1, | ||
site=self.SITE | ||
) | ||
return torrent.json_to_class(json_data) | ||
|
||
def search(self, keyword, **kwargs): | ||
url = self.URL | ||
|
||
user = kwargs.get('user', None) | ||
category = kwargs.get('category', 0) | ||
subcategory = kwargs.get('subcategory', 0) | ||
filters = kwargs.get('filters', 0) | ||
page = kwargs.get('page', 0) | ||
|
||
if user: | ||
user_uri = "user/{}".format(user) | ||
user_uri = f"user/{user}" | ||
else: | ||
user_uri = "" | ||
|
||
if page > 0: | ||
r = requests.get("{}/{}?f={}&c={}_{}&q={}&p={}".format( | ||
self.URI, user_uri, filters, category, subcategory, keyword, | ||
url, user_uri, filters, category, subcategory, keyword, | ||
page)) | ||
else: | ||
r = requests.get("{}/{}?f={}&c={}_{}&q={}".format( | ||
self.URI, user_uri, filters, category, subcategory, keyword)) | ||
url, user_uri, filters, category, subcategory, keyword)) | ||
|
||
r.raise_for_status() | ||
|
||
json_data = utils.parse_nyaa( | ||
request_text=r.text, | ||
limit=None, | ||
site=self.SITE | ||
) | ||
|
||
soup = BeautifulSoup(r.text, 'html.parser') | ||
rows = soup.select('table tr') | ||
return torrent.json_to_class(json_data) | ||
|
||
return utils.parse_nyaa(rows, limit=None) | ||
def get(self, view_id): | ||
r = requests.get(f'{self.URL}/view/{view_id}') | ||
r.raise_for_status() | ||
|
||
def get(self, id): | ||
r = requests.get("{}/view/{}".format(self.URI, id)) | ||
soup = BeautifulSoup(r.text, 'html.parser') | ||
content = soup.findAll("div", {"class": "panel", "id": None}) | ||
json_data = utils.parse_single(request_text=r.text, site=self.SITE) | ||
|
||
return utils.parse_single(content) | ||
return torrent.json_to_class(json_data) | ||
|
||
def get_user(self, username): | ||
r = requests.get("{}/user/{}".format(self.URI, username)) | ||
soup = BeautifulSoup(r.text, 'html.parser') | ||
r = requests.get(f'{self.URL}/user/{username}') | ||
r.raise_for_status() | ||
|
||
return utils.parse_nyaa(soup.select('table tr'), limit=None) | ||
json_data = utils.parse_nyaa( | ||
request_text=r.text, | ||
limit=None, | ||
site=self.SITE | ||
) | ||
return torrent.json_to_class(json_data) |
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,17 @@ | ||
def json_to_class(data): | ||
# We check if the data passed is a list or not | ||
if isinstance(data, list): | ||
object_list = [] | ||
for item in data: | ||
object_list.append(Torrent(item)) | ||
# Return a list of Torrent objects | ||
return object_list | ||
else: | ||
return Torrent(data) | ||
|
||
|
||
# This deals with converting the dict to an object | ||
class Torrent(object): | ||
def __init__(self, my_dict): | ||
for key in my_dict: | ||
setattr(self, key, my_dict[key]) |
Oops, something went wrong.