Merge pull request #45 from geraldobraz/master

Removing the Utils class.
This commit is contained in:
Juanjo Salvador
2019-10-04 19:00:24 +02:00
committed by GitHub
4 changed files with 200 additions and 209 deletions

View File

@@ -1,10 +1,7 @@
import requests import requests
import urllib.parse import urllib.parse
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from NyaaPy.utils import Utils from NyaaPy.utils import utils
utils = Utils()
class Nyaa: class Nyaa:

View File

@@ -1,8 +1,5 @@
import requests import requests
from NyaaPy.utils import Utils from NyaaPy.utils import utils
utils = Utils()
class Pantsu: class Pantsu:

View File

@@ -1,7 +1,6 @@
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from NyaaPy.utils import Utils as utils from NyaaPy.utils import utils
class SukebeiNyaa: class SukebeiNyaa:
def search(self, keyword, **kwargs): def search(self, keyword, **kwargs):

View File

@@ -4,100 +4,171 @@
import re import re
def nyaa_categories(self, b):
c = b.replace('/?c=', '')
cats = c.split('_')
class Utils: cat = cats[0]
def nyaa_categories(self, b): subcat = cats[1]
c = b.replace('/?c=', '')
cats = c.split('_')
cat = cats[0] categories = {
subcat = cats[1] "1": {
"name": "Anime",
categories = { "subcats": {
"1": { "1": "Anime Music Video",
"name": "Anime", "2": "English-translated",
"subcats": { "3": "Non-English-translated",
"1": "Anime Music Video", "4": "Raw"
"2": "English-translated", }
"3": "Non-English-translated", },
"4": "Raw" "2": {
} "name": "Audio",
}, "subcats": {
"2": { "1": "Lossless",
"name": "Audio", "2": "Lossy"
"subcats": { }
"1": "Lossless", },
"2": "Lossy" "3": {
} "name": "Literature",
}, "subcats": {
"3": { "1": "English-translated",
"name": "Literature", "2": "Non-English-translated",
"subcats": { "3": "Raw"
"1": "English-translated", }
"2": "Non-English-translated", },
"3": "Raw" "4": {
} "name": "Live Action",
}, "subcats": {
"4": { "1": "English-translated",
"name": "Live Action", "2": "Idol/Promotional Video",
"subcats": { "3": "Non-English-translated",
"1": "English-translated", "4": "Raw"
"2": "Idol/Promotional Video", }
"3": "Non-English-translated", },
"4": "Raw" "5": {
} "name": "Pictures",
}, "subcats": {
"5": { "1": "Graphics",
"name": "Pictures", "2": "Photos"
"subcats": { }
"1": "Graphics", },
"2": "Photos" "6": {
} "name": "Software",
}, "subcats": {
"6": { "1": "Applications",
"name": "Software", "2": "Games"
"subcats": {
"1": "Applications",
"2": "Games"
}
} }
} }
}
try:
category_name = "{} - {}".format(
categories[cat]['name'], categories[cat]['subcats'][subcat])
except Exception:
pass
return category_name
def parse_nyaa(self, table_rows, limit):
if limit == 0:
limit = len(table_rows)
torrents = []
for row in table_rows[:limit]:
block = []
for td in row.find_all('td'):
if td.find_all('a'):
for link in td.find_all('a'):
if link.get('href')[-9:] != '#comments':
block.append(link.get('href'))
if link.text.rstrip():
block.append(link.text)
if td.text.rstrip():
block.append(td.text.rstrip())
try: try:
category_name = "{} - {}".format( torrent = {
categories[cat]['name'], categories[cat]['subcats'][subcat]) 'id': block[1].replace("/view/", ""),
except Exception: 'category': nyaa_categories(self, block[0]),
'url': "http://nyaa.si{}".format(block[1]),
'name': block[2],
'download_url': "http://nyaa.si{}".format(block[4]),
'magnet': block[5],
'size': block[6],
'date': block[7],
'seeders': block[8],
'leechers': block[9],
'completed_downloads': block[10],
}
torrents.append(torrent)
except IndexError as ie:
pass pass
return category_name return torrents
def parse_nyaa(self, table_rows, limit): def parse_single(self, content):
if limit == 0: torrent = {}
limit = len(table_rows) data = []
torrent_files = []
torrents = [] for row in content[0].find_all('div', {'class': 'row'}):
for div in row.find_all('div', {'class': 'col-md-5'}):
data.append(div.text.replace("\n", ""))
for row in table_rows[:limit]: files = content[2].find('div',
block = [] {'class', 'torrent-file-list'}).find_all('li')
for td in row.find_all('td'): for file in files:
if td.find_all('a'): torrent_files.append(file.text)
for link in td.find_all('a'):
if link.get('href')[-9:] != '#comments':
block.append(link.get('href'))
if link.text.rstrip():
block.append(link.text)
if td.text.rstrip(): torrent['title'] = re.sub('\n|\r|\t', '', content[0].find('h3', {
block.append(td.text.rstrip()) "class": "panel-title"}).text.replace("\n", ""))
torrent['category'] = data[0]
torrent['uploader'] = data[2]
torrent['uploader_profile'] = "https://nyaa.si/user/{}".format(data[2])
torrent['website'] = re.sub('\t', '', data[4])
torrent['size'] = data[6]
torrent['date'] = data[1]
torrent['seeders'] = data[3]
torrent['leechers'] = data[5]
torrent['completed'] = data[7]
torrent['hash'] = data[8]
torrent['description'] = re.sub('\t', '', content[1].find('div', {
'id': 'torrent-description'}).text)
torrent['files'] = torrent_files
return torrent
def parse_sukebei(self, table_rows, limit):
if limit == 0:
limit = len(table_rows)
torrents = []
for row in table_rows[:limit]:
block = []
for td in row.find_all('td'):
for link in td.find_all('a'):
if link.get('href')[-9:] != '#comments':
block.append(link.get('href'))
block.append(link.text.rstrip())
if td.text.rstrip():
block.append(td.text.rstrip())
try: try:
torrent = { torrent = {
'id': block[1].replace("/view/", ""), 'id': block[1].replace("/view/", ""),
'category': Utils.nyaa_categories(self, block[0]), 'category': sukebei_categories(self, block[0]),
'url': "http://nyaa.si{}".format(block[1]), 'url': "http://sukebei.nyaa.si{}".format(block[1]),
'name': block[2], 'name': block[2],
'download_url': "http://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],
@@ -106,139 +177,66 @@ class Utils:
'completed_downloads': block[10], 'completed_downloads': block[10],
} }
torrents.append(torrent) torrents.append(torrent)
except IndexError as ie:
pass
return torrents return torrents
def parse_single(self, content): def sukebei_categories(self, b):
torrent = {} c = b.replace('/?c=', '')
data = [] cats = c.split('_')
torrent_files = []
for row in content[0].find_all('div', {'class': 'row'}): cat = cats[0]
for div in row.find_all('div', {'class': 'col-md-5'}): subcat = cats[1]
data.append(div.text.replace("\n", ""))
files = content[2].find('div', categories = {
{'class', 'torrent-file-list'}).find_all('li') "1": {
"name": "Art",
for file in files: "subcats": {
torrent_files.append(file.text) "1": "Anime",
"2": "Doujinshi",
torrent['title'] = re.sub('\n|\r|\t', '', content[0].find('h3', { "3": "Games",
"class": "panel-title"}).text.replace("\n", "")) "4": "Manga",
torrent['category'] = data[0] "5": "Pictures",
torrent['uploader'] = data[2] }
torrent['uploader_profile'] = "https://nyaa.si/user/{}".format(data[2]) },
torrent['website'] = re.sub('\t', '', data[4]) "2": {
torrent['size'] = data[6] "name": "Real Life",
torrent['date'] = data[1] "subcats": {
torrent['seeders'] = data[3] "1": "Photobooks & Pictures",
torrent['leechers'] = data[5] "2": "Videos"
torrent['completed'] = data[7]
torrent['hash'] = data[8]
torrent['description'] = re.sub('\t', '', content[1].find('div', {
'id': 'torrent-description'}).text)
torrent['files'] = torrent_files
return torrent
def parse_sukebei(self, table_rows, limit):
if limit == 0:
limit = len(table_rows)
torrents = []
for row in table_rows[:limit]:
block = []
for td in row.find_all('td'):
for link in td.find_all('a'):
if link.get('href')[-9:] != '#comments':
block.append(link.get('href'))
block.append(link.text.rstrip())
if td.text.rstrip():
block.append(td.text.rstrip())
try:
torrent = {
'id': block[1].replace("/view/", ""),
'category': Utils.sukebei_categories(self, block[0]),
'url': "http://sukebei.nyaa.si{}".format(block[1]),
'name': block[2],
'download_url': "http://sukebei.nyaa.si{}".format(
block[4]),
'magnet': block[5],
'size': block[6],
'date': block[7],
'seeders': block[8],
'leechers': block[9],
'completed_downloads': block[10],
}
torrents.append(torrent)
return torrents
def sukebei_categories(self, b):
c = b.replace('/?c=', '')
cats = c.split('_')
cat = cats[0]
subcat = cats[1]
categories = {
"1": {
"name": "Art",
"subcats": {
"1": "Anime",
"2": "Doujinshi",
"3": "Games",
"4": "Manga",
"5": "Pictures",
}
},
"2": {
"name": "Real Life",
"subcats": {
"1": "Photobooks & Pictures",
"2": "Videos"
}
} }
} }
}
try: try:
category_name = "{} - {}".format( category_name = "{} - {}".format(
categories[cat]['name'], categories[cat]['subcats'][subcat]) categories[cat]['name'], categories[cat]['subcats'][subcat])
except Exception: 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", available_params = ["category", "page", "limit", "userID", "fromID",
"status", "maxage", "toDate", "fromDate", "status", "maxage", "toDate", "fromDate",
"dateType", "minSize", "maxSize", "sizeType", "dateType", "minSize", "maxSize", "sizeType",
"sort", "order", "lang"] "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 if (param != "category" and param != "status" and
param != "lang"): 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