Merge pull request #11 from JuanjoSalvador/version-0.4

Version 0.4
This commit is contained in:
Juan José Salvador Piedra
2017-10-13 23:06:36 +02:00
committed by GitHub
10 changed files with 336 additions and 136 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
build/
dist/
nyaapy.egg-info
.vscode

View File

@@ -0,0 +1,10 @@
# Info about the module
__version__ = '0.4.1'
__author__ = 'Juanjo Salvador'
__email__ = 'juanjosalvador@netc.eu'
__url__ = 'http://juanjosalvador.me'
__copyright__ = '2017 Juanjo Salvador'
__license__ = 'MIT license'
from NyaaPy.nyaa import Nyaa
from NyaaPy.nyaapantsu import NyaaPantsu

View File

@@ -1,47 +1,33 @@
import requests
import xmltodict
import json
import collections
from bs4 import BeautifulSoup
from NyaaPy.utils import Utils as utils
class Nyaa():
def search(keyword, page):
if not page:
url = "https://nyaa.si/?page=rss&c=1_0&f=0&q={}".format(keyword)
'''
Return a list of dicts with the results of the query.
'''
def search(keyword, category, subcategory, filters, page):
if page > 0:
r = requests.get("http://nyaa.si/?f={}&c={}_{}&q={}&p={}".format(filters, category, subcategory, keyword, page))
else:
url = "https://nyaa.si/?page=rss&c=1_0&f=0&q={}&p={}".format(keyword, page)
r = requests.get("http://nyaa.si/?f={}&c={}_{}&q={}".format(filters, category, subcategory, keyword))
request = requests.get(url)
response = xmltodict.parse(request.text)
soup = BeautifulSoup(r.text, 'html.parser')
rows = soup.select('table tr')
results = []
results = {}
try:
if type(response['rss']['channel']['item']) is collections.OrderedDict:
results.append(response['rss']['channel']['item'])
else:
results = response['rss']['channel']['item']
except KeyError as ex:
results = []
if rows:
results = utils.parse_nyaa(rows, limit=None)
return results
'''
Returns an array of dicts with the n last updates of Nyaa.si
'''
def news(number_of_results):
r = requests.get("http://nyaa.si/")
soup = BeautifulSoup(r.text, 'html.parser')
rows = soup.select('table tr')
class NyaaPantsu():
def search(keyword):
nyaapantsu_baseurl = "https://nyaa.pantsu.cat/feed?c=_&s=0&max=99999&userID=0&q="
request = requests.get(nyaa_baseurl + keyword)
response = xmltodict.parse(request.text)
results = []
try:
if type(response['rss']['channel']['item']) is collections.OrderedDict:
results.append(response['rss']['channel']['item'])
else:
results = response['rss']['channel']['item']
except KeyError as ex:
results = []
return results
return utils.parse_nyaa(rows, limit=number_of_results)

42
NyaaPy/nyaapantsu.py Normal file
View File

@@ -0,0 +1,42 @@
import requests
from bs4 import BeautifulSoup
from NyaaPy.utils import Utils as utils
class NyaaPantsu():
'''
Make a query to nyaa.pantsu.cat using keyword as keyword.
Returns an array of OrderedDict with every result of the query.
Returns an empty array if no results.
'''
def search(keyword):
nyaapantsu_baseurl = "https://nyaa.pantsu.cat/feed?c=_&s=0&max=99999&userID=0&q="
request = requests.get(nyaa_baseurl + keyword)
response = xmltodict.parse(request.text)
results = []
try:
if type(response['rss']['channel']['item']) is collections.OrderedDict:
results.append(response['rss']['channel']['item'])
else:
results = response['rss']['channel']['item']
except KeyError as ex:
results = []
return results
'''
Returns an array of OrderedDict with the n last updates of nyaa.pantsu.cat
'''
def news(n):
nyaa_baseurl = "https://nyaa.pantsu.cat/feed"
request = requests.get(nyaa_baseurl)
response = xmltodict.parse(request.text)
results = response['rss']['channel']['item']
return results[:n]

107
NyaaPy/utils.py Normal file
View File

@@ -0,0 +1,107 @@
'''
Module utils
'''
class Utils():
def get_categories(b):
c = b.replace('/?c=', '')
cats = c.split('_')
cat = cats[0]
subcat = cats[1]
categories = {
"1": {
"name": "Anime",
"subcats": {
"1": "Anime Music Video",
"2": "English-translated",
"3": "Non-English-translated",
"4": "Raw"
}
},
"2": {
"name": "Audio",
"subcats": {
"1": "Lossless",
"2": "Lossy"
}
},
"3": {
"name": "Literature",
"subcats": {
"1": "English-translated",
"2": "Non-English-translated",
"3": "Raw"
}
},
"4": {
"name": "Live Action",
"subcats": {
"1": "English-translated",
"2": "Idol/Promotional Video",
"3": "Non-English-translated",
"4": "Raw"
}
},
"5": {
"name": "Pictures",
"subcats": {
"1": "Graphics",
"2": "Photos"
}
},
"6": {
"name": "Software",
"subcats": {
"1": "Applications",
"2": "Games"
}
}
}
try:
category_name = "{} - {}".format(categories[cat]['name'], categories[cat]['subcats'][subcat])
except:
pass
return category_name
def parse_nyaa(table_rows, limit):
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:
torrent = {
'category': Utils.get_categories(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
return torrents

145
README.md
View File

@@ -2,15 +2,132 @@
Unofficial Python module to search into Nyaa.si and nyaa.pantsu.cat.
Based on [Kylart's Nyaapi](https://github.com/Kylart/Nyaapi).
Supports Python 3+
* [Installation](#installation)
* [Example](#example)
* [Methods](#methods)
* [search()](#search)
* [news()](#news)
* [Categories and subcategories](#categories-and-subcategories)
* [Contributions and development](#contributons-and-development)
* [License](#license)
### Installation and ussage
## Installation
Install it using pip.
pip install nyaapy
## Example
```python
from NyaaPy import Nyaa, NyaaPantsu
nyaa_query = Nyaa.search(keyword='koe no katachi 1080', category=1, subcategory=0, filters=0, page=0)
nyaa_news = Nyaa.news(5)
if len(nyaa_query) > 0:
for result in nyaa_query:
print(result['title'])
else:
print('Nothing here!')
for new in nyaa_news:
print(new['title])
```
## Methods
### search()
Returns a list of dicts with the search results.
Parameters:
* **keyword**: String. Keyword for the search query.
* **category**: Integer.
* **subcategory**: Integer.
* **filters**: Integer.
* **page**: Integer.
`page` must be between 0 and 1000.
#### Dict returned for Nyaa.si
```python
'category': "Anime - English-translated",
'url': "https://nyaa.si/view/968600",
'name': "[HorribleSubs] Shoukoku no Altair - 14 [720p].mkv",
'download_url': "https://nyaa.si/download/968600.torrent",
'magnet': <magnet torrent URI>
'size': "317.2 MiB",
'date': "2017-10-13 20:16",
'seeders': "538",
'leechers': "286",
'completed_downloads': "852"
```
### news()
Parameters:
* **number_of_results**: Integer
`number_of_results` must be between 1 and 75.
## Categories and subcategories
List of available categories and subcategories:
1. Anime.
1.1 - Anime Music Video
1.2 - English-translated
1.3 - Non-English-translated
1.4 - Raw
2. Audio.
2.1 - Lossless
2.2 - Lossy
3. Literature.
3.1 - English-translated
3.2 - Non-English-translated
3.3 - Raw
4. Live Action.
4.1 - English-translated
4.2 - Idol/Promotional Video
4.3 - Non-English-translated
4.4 - Raw
5. Pictures.
5.1 - Graphics
5.2 - Photos
6. Software.
6.1 - Applications
6.2 - Games
### Contributions and development
@@ -35,28 +152,6 @@ At this moment there isn't an official Nyaa.si API, so we only can make requests
4. Always use the code into `src` folder, never the package.
### Example code
from NyaaPy.nyaa import Nyaa
from NyaaPy.nyaa import NyaaPantsu
# Nyaa.si results
nyaa_query = Nyaa.search('illo que pasa')
if len(nyaa_query) > 0:
for result in nyaa_query:
print(result['title'])
else:
print('Nothing here!')
# Nyaa.pantsu.cat results
pantsu_query = NyaaPantsu.search('new game')
if len(pantsu_query) > 0:
for result in pantsu_query:
print(result['title'])
else:
print('Nothing here!')
### License
## License
MIT license.

View File

@@ -1,5 +1,2 @@
certifi==2017.7.27.1
chardet==3.0.4
idna==2.5
requests==2.18.3
urllib3==1.22
requests==2.18.1
beautifulsoup4==4.6.0

View File

@@ -1,7 +1,7 @@
from setuptools import setup, find_packages
setup(name='nyaapy',
version='0.3.1',
version='0.4.1',
url='https://github.com/juanjosalvador/nyaapy',
download_url = 'https://github.com/juanjosalvador/nyaapy/archive/0.1.tar.gz',
license='MIT',

View File

@@ -1,54 +0,0 @@
import requests
import xmltodict
import json
import collections
class Nyaa:
'''
Makes a search query to nyaa.si with the given keyword that returns a
RSS file converted into a dictionary that we can use.
'''
def search(keyword):
nyaa_baseurl = "https://nyaa.si/?page=rss&c=1_0&f=0&q="
request = requests.get(nyaa_baseurl + keyword)
response = xmltodict.parse(request.text)
results = []
try:
if type(response['rss']['channel']['item']) is collections.OrderedDict:
results.append(response['rss']['channel']['item'])
else:
results = response['rss']['channel']['item']
except KeyError as ex:
results = []
return results
class NyaaPantsu:
'''
Makes a search query to nyaa.pantsu.cat with the given keyword that returns a
RSS file converted into a dictionary that we can use.
'''
def search(keyword):
nyaa_baseurl = "https://nyaa.pantsu.cat/feed?c=_&s=0&max=99999&userID=0&q="
request = requests.get(nyaa_baseurl + keyword)
response = xmltodict.parse(request.text)
results = []
try:
if type(response['rss']['channel']['item']) is collections.OrderedDict:
results.append(response['rss']['channel']['item'])
else:
results = response['rss']['channel']['item']
except KeyError as ex:
results = []
return results

View File

@@ -1,20 +1,36 @@
from NyaaPy.nyaa import Nyaa
from NyaaPy.nyaa import NyaaPantsu
import json
from NyaaPy import Nyaa, NyaaPantsu
# Nyaa.si results
nyaa_query = Nyaa.search('koe no katachi 1080')
def nyaa_search():
nyaa_query = Nyaa.search(keyword='koe no katachi 1080', category=1, subcategory=0, filters=0, page=0)
if len(nyaa_query) > 0:
for result in nyaa_query:
print(result['title'])
else:
print('Nothing here!')
for nyaa in nyaa_query:
print(nyaa)
def nyaa_news():
news = Nyaa.news(number_of_results=5)
for n in news:
print(n)
# Nyaa.pantsu.cat results
pantsu_query = NyaaPantsu.search('new game!!')
if len(pantsu_query) > 0:
for result in pantsu_query:
def pantsu_search():
pantsu_query = NyaaPantsu.search('new game!!')
if len(pantsu_query) > 0:
for result in pantsu_query:
print(result['title'])
else:
print('Nothing here!')
def pantsu_news():
news = NyaaPantsu.news(5)
for result in news:
print(result['title'])
else:
print('Nothing here!')
# Uncomment whatever you want to test
#nyaa_search()
#pantsu_search()
nyaa_news()
#pantsu_news()