Merge pull request #3 from rv2900/master
add read aria2 files recursively from folder, add useage to readme
This commit is contained in:
commit
c8587a5995
@ -56,9 +56,14 @@ output
|
||||
magnet:?xt=urn:btih:959E2ECEB954313D3869EFF7924CA7CD8DE739
|
||||
```
|
||||
|
||||
with multiple files
|
||||
with multiple files, folder
|
||||
```bash
|
||||
python aria2_to_magnet.py *.aria2
|
||||
python3 aria2_to_magnet.py dahufa.aria2 dahufa.aria2 ./folder
|
||||
```
|
||||
|
||||
reading aria2 files from folder recursively
|
||||
```bash
|
||||
python3 aria2_to_magnet.py ./ -r
|
||||
```
|
||||
|
||||
## python2
|
||||
|
@ -2,15 +2,20 @@
|
||||
import argparse
|
||||
import os
|
||||
import glob
|
||||
|
||||
"""
|
||||
Usage:
|
||||
command line
|
||||
python aria2_to_magnet.py -e .aria2 .
|
||||
windows: python aria2_to_magnet.py [path]|[-r --recursive]
|
||||
bash: python3 aria2_to_magnet.py [path]|[-r --recursive]
|
||||
example: python aria2_to_magnet.py ./
|
||||
"""
|
||||
|
||||
|
||||
def main():
|
||||
file_parser(parse_aria_control_file)
|
||||
|
||||
|
||||
def file_parser(handler_func):
|
||||
# codes from gist
|
||||
# https://gist.github.com/89465127/5273149
|
||||
parser = argparse.ArgumentParser(
|
||||
@ -19,10 +24,18 @@ def main():
|
||||
parser.add_argument('path',
|
||||
nargs='+',
|
||||
help='Path of a file or a folder of files.')
|
||||
# no need extension filter for aria2 file
|
||||
parser.add_argument('-e',
|
||||
'--extension',
|
||||
default='',
|
||||
default='.aria2',
|
||||
help='File extension to filter by.')
|
||||
|
||||
parser.add_argument('-r',
|
||||
'--recursive',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help='Search through subfolders')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# Parse paths
|
||||
@ -30,13 +43,18 @@ def main():
|
||||
files = set()
|
||||
for path in full_paths:
|
||||
if os.path.isfile(path):
|
||||
files.add(path)
|
||||
fileName, fileExt = os.path.splitext(path)
|
||||
if args.extension == '' or args.extension == fileExt:
|
||||
files.add(path)
|
||||
else:
|
||||
files |= set(glob.glob(path + '/*' + args.extension))
|
||||
if (args.recursive):
|
||||
full_paths += glob.glob(path + '/*')
|
||||
else:
|
||||
files |= set(glob.glob(path + '/*' + args.extension))
|
||||
|
||||
for f in files:
|
||||
magnet = parse_aria_control_file(f)
|
||||
print(f'file name: {os.path.basename(f)}, {magnet}')
|
||||
handler_func(f)
|
||||
|
||||
|
||||
# ================================================================
|
||||
# 0 1 2 3
|
||||
@ -67,24 +85,19 @@ def main():
|
||||
# more detail
|
||||
# https://aria2.github.io/manual/en/html/technical-notes.html
|
||||
# ================================================================
|
||||
|
||||
|
||||
def parse_aria_control_file(file_name):
|
||||
with open(file_name, "rb") as f:
|
||||
try:
|
||||
|
||||
f.seek(0) # Go to beginning, read VER
|
||||
version = f.read(2)
|
||||
|
||||
i = int.from_bytes(version, 'big')
|
||||
# print("version is " + str(i))
|
||||
try:
|
||||
with open(file_name, "rb") as f:
|
||||
# f.seek(0) # Go to beginning, read VER
|
||||
# version = f.read(2)
|
||||
# i = int.from_bytes(version, 'big')
|
||||
# print(f'version is {i}')
|
||||
|
||||
# skip EXT, find info hash_binary length
|
||||
f.seek(6)
|
||||
|
||||
length = f.read(4)
|
||||
hash_length = int.from_bytes(length, 'big')
|
||||
# print "hash length is " + str(hash_length)
|
||||
# print(f"hash length is {hash_length}")
|
||||
|
||||
# read next hash_length
|
||||
f.seek(10) # Go to info hash
|
||||
@ -92,17 +105,12 @@ def parse_aria_control_file(file_name):
|
||||
info_hash = hash_binary.hex().upper()
|
||||
|
||||
magnet_link = "magnet:?xt=urn:btih:" + info_hash
|
||||
return magnet_link
|
||||
except:
|
||||
pass
|
||||
finally:
|
||||
f.close()
|
||||
print(f'file name: {os.path.basename(file_name)}, {magnet_link}')
|
||||
except FileNotFoundError:
|
||||
print(f'file not found. {file_name}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# file_list = args.file
|
||||
# for file_name in file_list:
|
||||
# parse_aria_control_file(file_name)
|
||||
# version is 1
|
||||
# hash length is 20
|
||||
# magnet:?xt=urn:btih:959E2ECEB954313D38690EFF7924CA7CD80DE739
|
||||
|
Loading…
Reference in New Issue
Block a user