Merge pull request #40 from vgerak/master

Conform to PEP8 style
This commit is contained in:
Juanjo Salvador
2018-10-09 15:45:13 +02:00
committed by GitHub
7 changed files with 87 additions and 57 deletions

View File

@@ -1,11 +1,11 @@
# Info about the module # Info about the module
__version__ = '0.6.0' __version__ = '0.6.0'
__author__ = 'Juanjo Salvador' __author__ = 'Juanjo Salvador'
__email__ = 'juanjosalvador@netc.eu' __email__ = 'juanjosalvador@netc.eu'
__url__ = 'http://juanjosalvador.me' __url__ = 'http://juanjosalvador.me'
__copyright__ = '2017 Juanjo Salvador' __copyright__ = '2017 Juanjo Salvador'
__license__ = 'MIT license' __license__ = 'MIT license'
from NyaaPy.nyaa import Nyaa from NyaaPy.nyaa import Nyaa
from NyaaPy.pantsu import Pantsu from NyaaPy.pantsu import Pantsu
from NyaaPy.sukebei import SukebeiNyaa, SukebeiPantsu from NyaaPy.sukebei import SukebeiNyaa, SukebeiPantsu

View File

@@ -5,8 +5,9 @@ from NyaaPy.utils import Utils
utils = Utils() utils = Utils()
class Nyaa: class Nyaa:
def __init__(self): def __init__(self):
self.URI = "http://nyaa.si" self.URI = "http://nyaa.si"
@@ -23,9 +24,12 @@ class Nyaa:
user_uri = "" user_uri = ""
if page > 0: if page > 0:
r = requests.get("{}/{}?f={}&c={}_{}&q={}&p={}".format(self.URI, user_uri, filters, category, subcategory, keyword, page)) r = requests.get("{}/{}?f={}&c={}_{}&q={}&p={}".format(
self.URI, user_uri, filters, category, subcategory, keyword,
page))
else: else:
r = requests.get("{}/{}?f={}&c={}_{}&q={}".format(self.URI, user_uri, filters, category, subcategory, keyword)) r = requests.get("{}/{}?f={}&c={}_{}&q={}".format(
self.URI, user_uri, filters, category, subcategory, keyword))
soup = BeautifulSoup(r.text, 'html.parser') soup = BeautifulSoup(r.text, 'html.parser')
rows = soup.select('table tr') rows = soup.select('table tr')
@@ -35,8 +39,8 @@ class Nyaa:
def get(self, id): def get(self, id):
r = requests.get("{}/view/{}".format(self.URI, id)) r = requests.get("{}/view/{}".format(self.URI, id))
soup = BeautifulSoup(r.text, 'html.parser') soup = BeautifulSoup(r.text, 'html.parser')
content = soup.findAll("div", { "class": "panel", "id": None}) content = soup.findAll("div", {"class": "panel", "id": None})
return utils.parse_single(content) return utils.parse_single(content)
def get_user(self, username): def get_user(self, username):
@@ -50,4 +54,4 @@ class Nyaa:
soup = BeautifulSoup(r.text, 'html.parser') soup = BeautifulSoup(r.text, 'html.parser')
rows = soup.select('table tr') rows = soup.select('table tr')
return utils.parse_nyaa(rows, limit=number_of_results + 1) return utils.parse_nyaa(rows, limit=number_of_results + 1)

View File

@@ -3,14 +3,16 @@ from NyaaPy.utils import Utils
utils = Utils() utils = Utils()
class Pantsu: class Pantsu:
def __init__(self): def __init__(self):
self.BASE_URL = "https://nyaa.pantsu.cat/api" self.BASE_URL = "https://nyaa.pantsu.cat/api"
# Torrents - GET # Torrents - GET
def search(self, keyword, **kwargs): def search(self, keyword, **kwargs):
request = requests.get("{}/search{}".format(self.BASE_URL, utils.query_builder(keyword, kwargs))) request = requests.get("{}/search{}".format(
self.BASE_URL, utils.query_builder(keyword, kwargs)))
return request.json() return request.json()
def view(self, item_id): def view(self, item_id):
@@ -29,11 +31,13 @@ class Pantsu:
# Users # Users
def login(self, username, password): def login(self, username, password):
login = requests.post("{}/login/".format(self.BASE_URL), data={'username': username, 'password': password}) login = requests.post("{}/login/".format(
self.BASE_URL), data={'username': username, 'password': password})
return login.json() return login.json()
def profile(self, user_id): def profile(self, user_id):
profile = requests.post("{}/profile/".format(self.BASE_URL), data={'id': user_id}) profile = requests.post("{}/profile/".format(
self.BASE_URL), data={'id': user_id})
return profile.json() return profile.json()

View File

@@ -2,6 +2,7 @@ import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from NyaaPy.utils import Utils as utils from NyaaPy.utils import Utils as utils
class SukebeiNyaa: class SukebeiNyaa:
def search(self, keyword, **kwargs): def search(self, keyword, **kwargs):
category = kwargs.get('category', 0) category = kwargs.get('category', 0)
@@ -10,9 +11,13 @@ class SukebeiNyaa:
page = kwargs.get('page', 0) page = kwargs.get('page', 0)
if page > 0: if page > 0:
r = requests.get("{}/?f={}&c={}_{}&q={}&p={}".format("http://sukebei.nyaa.si", filters, category, subcategory, keyword, page)) r = requests.get("{}/?f={}&c={}_{}&q={}&p={}".format(
"http://sukebei.nyaa.si", filters, category, subcategory,
keyword, page))
else: else:
r = requests.get("{}/?f={}&c={}_{}&q={}".format("http://sukebei.nyaa.si", filters, category, subcategory, keyword)) r = requests.get("{}/?f={}&c={}_{}&q={}".format(
"http://sukebei.nyaa.si", filters, category, subcategory,
keyword))
soup = BeautifulSoup(r.text, 'html.parser') soup = BeautifulSoup(r.text, 'html.parser')
rows = soup.select('table tr') rows = soup.select('table tr')
@@ -22,8 +27,8 @@ class SukebeiNyaa:
def get(self, id): def get(self, id):
r = requests.get("http://sukebei.nyaa.si/view/{}".format(id)) r = requests.get("http://sukebei.nyaa.si/view/{}".format(id))
soup = BeautifulSoup(r.text, 'html.parser') soup = BeautifulSoup(r.text, 'html.parser')
content = soup.findAll("div", { "class": "panel", "id": None}) content = soup.findAll("div", {"class": "panel", "id": None})
return utils.parse_single(content) return utils.parse_single(content)
def get_user(self, username): def get_user(self, username):
@@ -39,17 +44,20 @@ class SukebeiNyaa:
return utils.parse_sukebei(rows, limit=number_of_results + 1) return utils.parse_sukebei(rows, limit=number_of_results + 1)
class SukebeiPantsu: class SukebeiPantsu:
BASE_URL = "https://sukebei.pantsu.cat/api" BASE_URL = "https://sukebei.pantsu.cat/api"
# Torrents - GET # Torrents - GET
def search(self, keyword, **kwargs): def search(self, keyword, **kwargs):
request = requests.get("{}/search{}".format(SukebeiPantsu.BASE_URL, utils.query_builder(keyword, kwargs))) request = requests.get("{}/search{}".format(
SukebeiPantsu.BASE_URL, utils.query_builder(keyword, kwargs)))
return request.json() return request.json()
def view(self, item_id): def view(self, item_id):
request = requests.get("{}/view/{}".format(SukebeiPantsu.BASE_URL, item_id)) request = requests.get("{}/view/{}".format(
SukebeiPantsu.BASE_URL, item_id))
return request.json() return request.json()
@@ -64,11 +72,14 @@ class SukebeiPantsu:
# Users # Users
def login(self, username, password): def login(self, username, password):
login = requests.post("{}/login/".format(SukebeiPantsu.BASE_URL), data={'username': username, 'password': password}) login = requests.post("{}/login/".format(
SukebeiPantsu.BASE_URL), data={'username': username,
'password': password})
return login.json() return login.json()
def profile(self, user_id): def profile(self, user_id):
profile = requests.post("{}/profile/".format(SukebeiPantsu.BASE_URL), data={'id': user_id}) profile = requests.post("{}/profile/".format(
SukebeiPantsu.BASE_URL), data={'id': user_id})
return profile.json() return profile.json()

View File

@@ -4,6 +4,7 @@
import re import re
class Utils: class Utils:
def nyaa_categories(self, b): def nyaa_categories(self, b):
c = b.replace('/?c=', '') c = b.replace('/?c=', '')
@@ -37,7 +38,7 @@ class Utils:
"3": "Raw" "3": "Raw"
} }
}, },
"4": { "4": {
"name": "Live Action", "name": "Live Action",
"subcats": { "subcats": {
"1": "English-translated", "1": "English-translated",
@@ -46,14 +47,14 @@ class Utils:
"4": "Raw" "4": "Raw"
} }
}, },
"5": { "5": {
"name": "Pictures", "name": "Pictures",
"subcats": { "subcats": {
"1": "Graphics", "1": "Graphics",
"2": "Photos" "2": "Photos"
} }
}, },
"6": { "6": {
"name": "Software", "name": "Software",
"subcats": { "subcats": {
"1": "Applications", "1": "Applications",
@@ -61,10 +62,11 @@ class Utils:
} }
} }
} }
try: try:
category_name = "{} - {}".format(categories[cat]['name'], categories[cat]['subcats'][subcat]) category_name = "{} - {}".format(
except: categories[cat]['name'], categories[cat]['subcats'][subcat])
except Exception:
pass pass
return category_name return category_name
@@ -107,7 +109,7 @@ class Utils:
torrents.append(torrent) torrents.append(torrent)
except IndexError as ie: except IndexError as ie:
pass pass
return torrents return torrents
def parse_single(self, content): def parse_single(self, content):
@@ -119,13 +121,14 @@ class Utils:
for div in row.find_all('div', {'class': 'col-md-5'}): for div in row.find_all('div', {'class': 'col-md-5'}):
data.append(div.text.replace("\n", "")) data.append(div.text.replace("\n", ""))
files = content[2].find('div', {'class', 'torrent-file-list'}).find_all('li') files = content[2].find('div',
{'class', 'torrent-file-list'}).find_all('li')
for file in files: for file in files:
torrent_files.append(file.text) torrent_files.append(file.text)
torrent['title'] = re.sub('\n|\r|\t', '', content[0].find('h3', {
torrent['title'] = re.sub('\n|\r|\t', '', content[0].find('h3', {"class": "panel-title"}).text.replace("\n", "")) "class": "panel-title"}).text.replace("\n", ""))
torrent['category'] = data[0] torrent['category'] = data[0]
torrent['uploader'] = data[2] torrent['uploader'] = data[2]
torrent['uploader_profile'] = "https://nyaa.si/user/{}".format(data[2]) torrent['uploader_profile'] = "https://nyaa.si/user/{}".format(data[2])
@@ -136,7 +139,8 @@ class Utils:
torrent['leechers'] = data[5] torrent['leechers'] = data[5]
torrent['completed'] = data[7] torrent['completed'] = data[7]
torrent['hash'] = data[8] torrent['hash'] = data[8]
torrent['description'] = re.sub('\t', '', content[1].find('div', {'id': 'torrent-description'}).text) torrent['description'] = re.sub('\t', '', content[1].find('div', {
'id': 'torrent-description'}).text)
torrent['files'] = torrent_files torrent['files'] = torrent_files
return torrent return torrent
@@ -158,14 +162,15 @@ class Utils:
if td.text.rstrip(): if td.text.rstrip():
block.append(td.text.rstrip()) block.append(td.text.rstrip())
try: try:
torrent = { torrent = {
'id': block[1].replace("/view/", ""), 'id': block[1].replace("/view/", ""),
'category': Utils.sukebei_categories(self, block[0]), 'category': Utils.sukebei_categories(self, block[0]),
'url': "http://sukebei.nyaa.si{}".format(block[1]), 'url': "http://sukebei.nyaa.si{}".format(block[1]),
'name': block[2], 'name': block[2],
'download_url': "http://sukebei.nyaa.si{}".format(block[4]), 'download_url': "http://sukebei.nyaa.si{}".format(
block[4]),
'magnet': block[5], 'magnet': block[5],
'size': block[6], 'size': block[6],
'date': block[7], 'date': block[7],
@@ -173,9 +178,9 @@ class Utils:
'leechers': block[9], 'leechers': block[9],
'completed_downloads': block[10], 'completed_downloads': block[10],
} }
torrents.append(torrent) torrents.append(torrent)
return torrents return torrents
def sukebei_categories(self, b): def sukebei_categories(self, b):
@@ -204,32 +209,36 @@ class Utils:
} }
} }
} }
try: try:
category_name = "{} - {}".format(categories[cat]['name'], categories[cat]['subcats'][subcat]) category_name = "{} - {}".format(
except: categories[cat]['name'], categories[cat]['subcats'][subcat])
except Exception:
pass pass
return category_name return category_name
# Pantsu Utils # Pantsu Utils
def query_builder(self, q, params): def query_builder(self, q, params):
available_params = ["category", "page", "limit", "userID", "fromID", "status", "maxage", "toDate", "fromDate",\ available_params = ["category", "page", "limit", "userID", "fromID",
"dateType", "minSize", "maxSize", "sizeType", "sort", "order", "lang"] "status", "maxage", "toDate", "fromDate",
"dateType", "minSize", "maxSize", "sizeType",
"sort", "order", "lang"]
query = "?q={}".format(q.replace(" ", "+")) query = "?q={}".format(q.replace(" ", "+"))
for param, value in params.items(): for param, value in params.items():
if param in available_params: if param in available_params:
if param != "category" and param != "status" and param != "lang": if (param != "category" and param != "status" and
param != "lang"):
query += "&{}={}".format(param, value) query += "&{}={}".format(param, value)
elif param == "category": elif param == "category":
query += "&c={}_{}".format(value[0], value[1]) query += "&c={}_{}".format(value[0], value[1])
elif param == "status": elif param == "status":
query += "&s={}".format(value) query += "&s={}".format(value)
elif param == "lang": elif param == "lang":
for lang in value: for lang in value:
query += "&lang={}".format(lang) query += "&lang={}".format(lang)
return query return query

View File

@@ -7,14 +7,15 @@ with open(path.join(currdir, 'README.md'), encoding='utf-8') as f:
setup(name='nyaapy', setup(name='nyaapy',
version='0.6.3', version='0.6.3',
install_requires = [ install_requires=[
"requests", "requests",
"beautifulsoup4", "beautifulsoup4",
], ],
url='https://github.com/juanjosalvador/nyaapy', url='https://github.com/juanjosalvador/nyaapy',
long_description=long_desc, long_description=long_desc,
long_description_content_type='text/markdown', long_description_content_type='text/markdown',
download_url = 'https://github.com/juanjosalvador/nyaapy/archive/0.6.2.tar.gz', download_url=('https://github.com/juanjosalvador/'
'nyaapy/archive/0.6.2.tar.gz'),
license='MIT', license='MIT',
author='Juanjo Salvador', author='Juanjo Salvador',
author_email='juanjosalvador@netc.eu', author_email='juanjosalvador@netc.eu',

View File

@@ -3,5 +3,6 @@ from NyaaPy import Pantsu, Nyaa
pantsu = Pantsu() pantsu = Pantsu()
nyaa = Nyaa() nyaa = Nyaa()
#print(pantsu.search(keyword='koe no katachi', lang=["es", "ja"], category=[1, 3])) # print(pantsu.search(keyword='koe no katachi',
print(nyaa.search(keyword='yuru camp')) # lang=["es", "ja"], category=[1, 3]))
print(nyaa.search(keyword='yuru camp'))