Created
February 20, 2020 20:55
-
-
Save jakekara/d75b2458d8c36c2343dd5f5b3bf3ba09 to your computer and use it in GitHub Desktop.
Generate input files for aria2c download manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Generate input files for aria2c. | |
For examples and documentation: https://aria2.github.io/manual/en/html/aria2c.html#input-file | |
""" | |
import re | |
class Aria2cInputEntry: | |
def __init__(self): | |
self.options = {} | |
self.uris = [] | |
@staticmethod | |
def make_line(line: str): | |
ret = line | |
if not line.endswith("\n"): | |
ret = f"{line}\n" | |
return ret | |
@staticmethod | |
def make_first_line(uris: list): | |
""" | |
The first line in an entry is a tab-separated list of source URIs | |
""" | |
return Aria2cInputEntry.make_line("\t".join(uris)) | |
@staticmethod | |
def join_lines(lines: list): | |
return "".join([Aria2cInputEntry.make_line(line) for line in lines]) | |
@staticmethod | |
def make_options(options: dict, indent=2): | |
lines = [] | |
for k in options.keys(): | |
lines.append((" " * indent) + f"{k}={options[k]}") | |
return Aria2cInputEntry.join_lines(lines) | |
@staticmethod | |
def make_entry(uris: list, options: dict): | |
return Aria2cInputEntry.join_lines([ | |
Aria2cInputEntry.make_first_line(uris), | |
Aria2cInputEntry.make_options(options) | |
]) | |
def __str__(self): | |
return Aria2cInputEntry.make_entry(self.uris, self.options) | |
def add_uri(self, uri:str): | |
self.uris.append(uri) | |
def add_uris(self, uris:list): | |
for uri in uris: | |
self.add_uri(uri) | |
def set_option(self, key:str, value:str): | |
self.options[key] = value | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ar2c_input_tools import Aria2cInputEntry | |
# Example of static method use | |
print(Aria2cInputEntry.make_entry(['http://example.com', 'https://example.org'], {"dir":DST_DIR})) | |
""" | |
outputs: | |
http://example.com https://example.org | |
dir=./sample-downloads | |
""" | |
# Example of building an entry | |
entry = Aria2cInputEntry() | |
entry.add_uri("http://example.com") | |
entry.add_uri("http://example.org") | |
entry.set_option("dir", DST_DIR) | |
print(entry) | |
""" | |
outputs: | |
http://example.com http://example.org | |
dir=./sample-downloads | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment