Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This is a python scraper to read torrent files for metadata. #358

Merged
merged 2 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions scrapers/torrent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os
import sys
import json
import torrent_parser as tp
''' This script parses all torrent files in the specified directory for embedded metadata.
The title can either be a filename or the filename of the .torrent file

This requires python3.
This uses the torrent_parser library to parse torrent files from: https://github.com/7sDream/torrent_parser
This library is under the MIT Licence.

'''

path='./torrents/'

def query(title):
# print(f"Test",file=sys.stderr)
for root,dirs,files in os.walk(path):
for name in files:
if '.torrent' in name:
query_torrent(title,os.path.join(root,name))

def query_torrent(title,path,found=False):
data=tp.parse_torrent_file(path)
# does the torrent contain more than one file and check if the file name we want is in the list
if not found and 'files' in data['info']:
for d in data['info']['files']:
for f in d['path']:
if title in f:
found=True
elif title in data['info']['name']:
found=True
if found:
res={'title':title}
if 'metadata' in data:
if 'title' in data['metadata']:
res['title']=data['metadata']['title']
if 'cover url' in data['metadata']:
res['image']=data['metadata']['cover url']
if 'description' in data['metadata']:
res['details']=data['metadata']['description']
if 'taglist' in data['metadata']:
res['tags']=[{"name":x} for x in data['metadata']['taglist']]

print(json.dumps(res))
exit(0)
def lookup_torrent(title):
for root,dirs,files in os.walk(path):
if title in files:
query_torrent(title,os.path.join(root,title),found=True)

if sys.argv[1] == "query":
fragment = json.loads(sys.stdin.read())
title=fragment['title']
if '.torrent' in title:
lookup_torrent(title)
else:
query(title)
print(json.dumps(fragment))
10 changes: 10 additions & 0 deletions scrapers/torrent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: "Torrent"
sceneByFragment:
action: script
script:
- python
# use python3 instead if needed
- torrent.py
- query

# Last Updated February 4, 2021
Loading