1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
build/
|
build/
|
||||||
dist/
|
dist/
|
||||||
nyaapy.egg-info
|
nyaapy.egg-info
|
||||||
|
.vscode
|
||||||
@@ -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
|
||||||
@@ -1,47 +1,33 @@
|
|||||||
import requests
|
import requests
|
||||||
import xmltodict
|
from bs4 import BeautifulSoup
|
||||||
import json
|
from NyaaPy.utils import Utils as utils
|
||||||
import collections
|
|
||||||
|
|
||||||
class Nyaa():
|
class Nyaa():
|
||||||
def search(keyword, page):
|
'''
|
||||||
if not page:
|
Return a list of dicts with the results of the query.
|
||||||
url = "https://nyaa.si/?page=rss&c=1_0&f=0&q={}".format(keyword)
|
'''
|
||||||
|
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:
|
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)
|
soup = BeautifulSoup(r.text, 'html.parser')
|
||||||
response = xmltodict.parse(request.text)
|
rows = soup.select('table tr')
|
||||||
|
|
||||||
results = []
|
results = {}
|
||||||
|
|
||||||
try:
|
if rows:
|
||||||
if type(response['rss']['channel']['item']) is collections.OrderedDict:
|
results = utils.parse_nyaa(rows, limit=None)
|
||||||
results.append(response['rss']['channel']['item'])
|
|
||||||
else:
|
|
||||||
results = response['rss']['channel']['item']
|
|
||||||
|
|
||||||
except KeyError as ex:
|
|
||||||
results = []
|
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
class NyaaPantsu():
|
'''
|
||||||
def search(keyword):
|
Returns an array of dicts with the n last updates of Nyaa.si
|
||||||
nyaapantsu_baseurl = "https://nyaa.pantsu.cat/feed?c=_&s=0&max=99999&userID=0&q="
|
'''
|
||||||
|
def news(number_of_results):
|
||||||
|
r = requests.get("http://nyaa.si/")
|
||||||
|
soup = BeautifulSoup(r.text, 'html.parser')
|
||||||
|
rows = soup.select('table tr')
|
||||||
|
|
||||||
request = requests.get(nyaa_baseurl + keyword)
|
return utils.parse_nyaa(rows, limit=number_of_results)
|
||||||
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
|
|
||||||
42
NyaaPy/nyaapantsu.py
Normal file
42
NyaaPy/nyaapantsu.py
Normal 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
107
NyaaPy/utils.py
Normal 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
145
README.md
@@ -2,15 +2,132 @@
|
|||||||
|
|
||||||
Unofficial Python module to search into Nyaa.si and nyaa.pantsu.cat.
|
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.
|
Install it using pip.
|
||||||
|
|
||||||
pip install nyaapy
|
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
|
### 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.
|
4. Always use the code into `src` folder, never the package.
|
||||||
|
|
||||||
### Example code
|
## License
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
MIT license.
|
MIT license.
|
||||||
|
|||||||
@@ -1,5 +1,2 @@
|
|||||||
certifi==2017.7.27.1
|
requests==2.18.1
|
||||||
chardet==3.0.4
|
beautifulsoup4==4.6.0
|
||||||
idna==2.5
|
|
||||||
requests==2.18.3
|
|
||||||
urllib3==1.22
|
|
||||||
2
setup.py
2
setup.py
@@ -1,7 +1,7 @@
|
|||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
setup(name='nyaapy',
|
setup(name='nyaapy',
|
||||||
version='0.3.1',
|
version='0.4.1',
|
||||||
url='https://github.com/juanjosalvador/nyaapy',
|
url='https://github.com/juanjosalvador/nyaapy',
|
||||||
download_url = 'https://github.com/juanjosalvador/nyaapy/archive/0.1.tar.gz',
|
download_url = 'https://github.com/juanjosalvador/nyaapy/archive/0.1.tar.gz',
|
||||||
license='MIT',
|
license='MIT',
|
||||||
|
|||||||
54
src/nyaa.py
54
src/nyaa.py
@@ -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
|
|
||||||
@@ -1,20 +1,36 @@
|
|||||||
from NyaaPy.nyaa import Nyaa
|
from NyaaPy import Nyaa, NyaaPantsu
|
||||||
from NyaaPy.nyaa import NyaaPantsu
|
|
||||||
import json
|
|
||||||
|
|
||||||
# Nyaa.si results
|
# 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 nyaa in nyaa_query:
|
||||||
for result in nyaa_query:
|
print(nyaa)
|
||||||
print(result['title'])
|
|
||||||
else:
|
def nyaa_news():
|
||||||
print('Nothing here!')
|
news = Nyaa.news(number_of_results=5)
|
||||||
|
for n in news:
|
||||||
|
print(n)
|
||||||
|
|
||||||
# Nyaa.pantsu.cat results
|
# Nyaa.pantsu.cat results
|
||||||
pantsu_query = NyaaPantsu.search('new game!!')
|
def pantsu_search():
|
||||||
if len(pantsu_query) > 0:
|
pantsu_query = NyaaPantsu.search('new game!!')
|
||||||
for result in pantsu_query:
|
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'])
|
print(result['title'])
|
||||||
else:
|
|
||||||
print('Nothing here!')
|
# Uncomment whatever you want to test
|
||||||
|
|
||||||
|
#nyaa_search()
|
||||||
|
#pantsu_search()
|
||||||
|
nyaa_news()
|
||||||
|
#pantsu_news()
|
||||||
Reference in New Issue
Block a user