Add a fetch command; this could probably use some refinement.
This commit is contained in:
parent
d1ddad97ef
commit
790f3af1a2
3 changed files with 44 additions and 1 deletions
|
@ -17,6 +17,8 @@ DEFAULT_IWAD=IWADS.joinpath("DOOM2.WAD")
|
|||
THUMB_WIDTH=1280
|
||||
THUMB_HEIGHT=720
|
||||
|
||||
MIRROR="https://youfailit.net/pub/idgames" # NYC
|
||||
|
||||
def DsdaPreamble(wad, mapstr):
|
||||
args = []
|
||||
pwadpath = PWADS.joinpath(wad)
|
||||
|
@ -60,6 +62,9 @@ def DemoOutPath(wad, mapstr):
|
|||
def DemoName(wad, mapstr):
|
||||
return "{}_map{}.lmp".format(wad, mapstr)
|
||||
|
||||
def PwadPath(wad):
|
||||
return Ensure(PWADS.joinpath(wad))
|
||||
|
||||
def BaseThumbPath(wad, mapstr):
|
||||
return Ensure(OUTPUT.joinpath(wad)).joinpath("{}_map{}_base.png".format(wad, mapstr))
|
||||
|
||||
|
@ -68,5 +73,5 @@ def VideoPath(wad, mapstr):
|
|||
|
||||
def Ensure(path):
|
||||
if not path.exists():
|
||||
ok.mkdir(path)
|
||||
os.mkdir(path)
|
||||
return path
|
||||
|
|
37
dcc/fetch.py
Normal file
37
dcc/fetch.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
from cliff.command import Command
|
||||
import dcc.config
|
||||
import io
|
||||
import json
|
||||
import pathlib
|
||||
import urllib.request
|
||||
import zipfile
|
||||
|
||||
class Fetch(Command):
|
||||
def get_parser(self, prog_name):
|
||||
parser = super().get_parser(prog_name)
|
||||
parser.add_argument("id_or_name")
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
idgames_id = parsed_args.id_or_name
|
||||
if not parsed_args.id_or_name.isdigit():
|
||||
idgames_id = self.search_idgames(parsed_args.id_or_name)
|
||||
|
||||
with urllib.request.urlopen("https://www.doomworld.com/idgames/api/api.php?action=get&id={}&out=json".format(idgames_id)) as response:
|
||||
reply = json.loads(response.read())
|
||||
rpath = "/".join([dcc.config.MIRROR, reply["content"]["dir"], reply["content"]["filename"]])
|
||||
wad = reply["content"]["filename"][0:-4]
|
||||
|
||||
with urllib.request.urlopen(rpath) as response:
|
||||
z = zipfile.ZipFile(io.BytesIO(response.read()))
|
||||
z.extractall(path=dcc.config.PwadPath(wad))
|
||||
|
||||
# TODO: explicit error handling. Let users choose when >1 result.
|
||||
def search_idgames(self, wad):
|
||||
with urllib.request.urlopen("https://www.doomworld.com/idgames/api/api.php?action=search&query={}&out=json".format(wad)) as response:
|
||||
reply = json.loads(response.read())
|
||||
files = reply["content"]["file"]
|
||||
if type(files) is dict: # One result.
|
||||
return files["id"]
|
||||
else: # More than one. Zero will raise an error.
|
||||
return files[0]["id"]
|
1
setup.py
1
setup.py
|
@ -29,6 +29,7 @@ setup(
|
|||
'ss = dcc.ss:SS',
|
||||
'check = dcc.check:Check',
|
||||
'extract = dcc.extract:Extract',
|
||||
'fetch = dcc.fetch:Fetch',
|
||||
],
|
||||
},
|
||||
zip_safe=False,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue