add yts_torrents project (#116)

* add yts_torrents project
This commit is contained in:
Mayank Nader 2019-10-31 10:07:55 +05:30 committed by Ayush Bhardwaj
parent e1b32bf801
commit c04764c6d7
5 changed files with 233 additions and 1 deletions

View File

@ -76,7 +76,7 @@ So far, the following projects have been integrated to this repo:
|[PyRecorder](./PyRecorder)|[Rocky Jain](https://github.com/jainrocky)|
|[Pretty CSV](https://github.com/Frizz925/Awesome-Python-Scripts/tree/pretty-csv/Pretty-CSV)|[Frizz925](https://github.com/Frizz925)|
|[File Organizer](https://github.com/Frizz925/Awesome-Python-Scripts/tree/File-Organizer)|[Ayush Bhardwaj](https://github.com/hastagAB)|
|[YTS Torrents](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/yts_torrents)|[Mayank Nader](https://github.com/makkoncept)|
## How to use :

48
yts_torrents/README.md Normal file
View File

@ -0,0 +1,48 @@
# yts_torrents
Download all torrents from yts.am (yify movies). Uses yify api.
## Usage
- Create a virtualenv:
```
python3 -m venv venv
```
- Activate it on Linux:
```
. venv/bin/activate
```
- Or on Windows cmd:
```
venv\Scripts\activate.bat
```
- Install requirements
```
pip install -r requirements
```
run `python yts_am_api.py` to make json files containing torrent links of the movies. Then run `python linkdownload.py`
to parse the created json files and download the torrents.
## Priority
The torrents will be downloaded according to the following priority:
1080p bluray> 1080p web> 720p bluray> 720p web
## Disclaimer
Downloading copyright movies may be illegal in your country. This tool is for educational purposes only and was created only to experiment with [yify api](https://yts.am/api)
## Original Repository
Check out the original repository at https://github.com/makkoncept/yts_torrents.
This project was used to create [movie_torrents](https://github.com/makkoncept/movie_torrents)[repository of 10k+ movie torrents].

View File

@ -0,0 +1,86 @@
import requests
import os
import json
import time
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
os.makedirs('torrents', exist_ok=True)
check_path = os.path.join('torrents')
cache = os.listdir(check_path)
def requests_retry_session(
retries=3,
backoff_factor=0.3,
status_forcelist=(500, 502, 504),
session=None,
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
def download_torrent(bin_content, movie_name, type):
torrent_name = movie_name + type + '.torrent'
if '/' in torrent_name:
torrent_name = torrent_name.split('/')[0]
if torrent_name in cache:
print("{} already downloaded".format(torrent_name))
path = os.path.join('torrents', torrent_name)
with open(path, 'wb') as f:
f.write(bin_content)
# write the torrents json file names here.
torrent_list = ['torrents1.json', 'torrents2.json', 'torrents3.json', 'torrents5.json',
'torrents6.json', 'torrents7.json', 'torrents8.json', 'torrents9.json',
'torrents10.json', 'torrents11.json', 'torrents12.json', 'torrents13.json']
for torrent_json_file in torrent_list:
if not os.path.isfile(torrent_json_file):
print("{} does not exist. Run yts_am_api.py script "
"to create json files with torrent links to download".format(
torrent_json_file))
continue
print(torrent_json_file)
with open(torrent_json_file, 'r') as f:
content = json.load(f)
movies = list(content.keys())
print("no. of movies: {}".format(len(movies)))
for movie in movies:
torrents = content[movie]
bluray_1080 = torrents.get('1080_bluray')
bluray_720 = torrents.get('720_bluray')
web_1080 = torrents.get('1080_web')
web_720 = torrents.get('720_web')
movie = movie.encode('utf-8').decode('utf-8')
print('movie', movie)
time.sleep(0.01)
if bluray_1080 is not None:
response = requests_retry_session().get(bluray_1080, timeout=3)
download_torrent(response.content, movie, 'bluray_1080p')
continue
elif web_1080 is not None:
response = requests_retry_session().get(web_1080, timeout=3)
download_torrent(response.content, movie, 'web_1080p')
continue
elif bluray_720 is not None:
response = requests_retry_session().get(bluray_720, timeout=3)
download_torrent(response.content, movie, 'bluray_720p')
continue
elif web_720 is not None:
response = requests_retry_session().get(web_720, timeout=3)
download_torrent(response.content, movie, 'web_720p')
continue
else:
print('not any torrent')

View File

@ -0,0 +1,5 @@
certifi==2019.9.11
chardet==3.0.4
idna==2.8
requests==2.22.0
urllib3==1.25.6

View File

@ -0,0 +1,93 @@
import requests
import json
import time
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
def requests_retry_session(
retries=3,
backoff_factor=0.3,
status_forcelist=(500, 502, 504),
session=None,
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
def create_json_file(json_file_number):
name = 'torrents' + str(json_file_number) + '.json'
with open(name, 'w') as f:
content = {}
json.dump(content, f)
def add_torrent_to_json_file(json_file_number):
name = 'torrents' + str(json_file_number) + '.json'
with open(name, 'r') as f:
content = json.load(f)
content[title_long] = {'720_bluray': torrent_720_bluray, '1080_bluray': torrent_1080_bluray,
'720_web': torrent_720_web, '1080_web': torrent_1080_web}
with open(name, 'w') as f:
json.dump(content, f)
json_file_number = 1
create_json_file(json_file_number)
url = "https://yts.am/api/v2/list_movies.json?limit=50&page="
count = 0
movie_count = 0
for page in range(1, 424):
count += 1
api_url = url + str(page)
time.sleep(1)
print(api_url)
response = requests_retry_session().get(api_url, timeout=3).json()
time.sleep(2)
data = response.get('data')
movies = data.get('movies')
if movies is None:
print("No more torrents on this page")
exit()
for movie in movies:
movie_count += 1
title_long = movie.get('title_long')
print(title_long)
print('movie_count', movie_count)
torrents = movie.get('torrents')
if torrents is None:
print("no torrent for this movie")
continue
torrent_720_web = None
torrent_1080_web = None
torrent_720_bluray = None
torrent_1080_bluray = None
for torrent in torrents:
if torrent.get('quality') == "720p":
if torrent.get('type') == "web":
torrent_720_web = torrent.get('url')
elif torrent.get('type') == "bluray":
torrent_720_bluray = torrent.get('url')
elif torrent.get('quality') == "1080p":
if torrent.get('type') == "web":
torrent_1080_web = torrent.get('url')
elif torrent.get('type') == "bluray":
torrent_1080_bluray = torrent.get('url')
if count < 20:
add_torrent_to_json_file(json_file_number)
else:
count = 1
json_file_number += 1
create_json_file(json_file_number)
add_torrent_to_json_file(json_file_number)