mirror of
https://gitlab.com/niansa/commsyfuse.git
synced 2025-03-06 20:48:31 +01:00
Added script as example on how to parse the JSON result in Python
This commit is contained in:
parent
c690040e8e
commit
c11de2bd75
2 changed files with 40 additions and 2 deletions
38
downloadall.py
Executable file
38
downloadall.py
Executable file
|
@ -0,0 +1,38 @@
|
|||
#! /usr/bin/env python3
|
||||
import subprocess
|
||||
import os, sys
|
||||
import requests, json
|
||||
|
||||
# Define a little write() helper
|
||||
def fwrite(path:str, data, mode="w"):
|
||||
with open(path, mode=mode) as f:
|
||||
f.write(data)
|
||||
|
||||
# Check argv
|
||||
if len(sys.argv) < 3:
|
||||
os.write(2, f"Usage: {argv[0]} <destpath> <SID> <room>\n")
|
||||
sys.exit(1);
|
||||
destpath = sys.argv[1]
|
||||
|
||||
# Try accessing/writing to given path
|
||||
os.listdir(destpath)
|
||||
fwrite(destpath + "/rwtest", "It works!")
|
||||
os.remove(destpath + "/rwtest")
|
||||
|
||||
# Run program
|
||||
proc = subprocess.Popen([os.path.dirname(os.path.abspath(__file__))+'/bin/CommSyFuse', 'json'] + sys.argv[2:], stdout=subprocess.PIPE)
|
||||
|
||||
# Generate folder/file structure
|
||||
os.chdir(destpath)
|
||||
for entry in json.load(proc.stdout):
|
||||
# Create and switch to new directory
|
||||
os.mkdir(entry["name"])
|
||||
os.chdir(entry["name"])
|
||||
# Write description
|
||||
fwrite("info.txt", entry["meta"] + "\n\n" + entry["description"])
|
||||
# Download files
|
||||
for file in entry["files"]:
|
||||
print("Downloading:", file["url"])
|
||||
fwrite(file["name"], requests.get(file["url"], cookies=dict(SID=sys.argv[2])).content, mode="wb")
|
||||
# Switch back
|
||||
os.chdir("..")
|
4
main.cpp
4
main.cpp
|
@ -381,12 +381,12 @@ int mode_tmpl(mode_json) {
|
|||
jsonroot[it]["name"] = postsmap_numname[it];
|
||||
jsonroot[it]["meta"] = postsmap_nummeta[it];
|
||||
jsonroot[it]["description"] = postsmap_numdesc[it];
|
||||
jsonroot[it]["url"] = postsmap_numurl[it];
|
||||
jsonroot[it]["url"] = server_url + postsmap_numurl[it];
|
||||
jsonroot[it]["files"] = json::array();
|
||||
for (unsigned long it2 = 0; it2 != postsmap_numfileurls[it].size(); it2++) {
|
||||
jsonroot[it]["files"][it2] = json::object();
|
||||
jsonroot[it]["files"][it2]["name"] = postsmap_numfilenames[it][it2];
|
||||
jsonroot[it]["files"][it2]["url"] = postsmap_numfileurls[it][it2];
|
||||
jsonroot[it]["files"][it2]["url"] = server_url + postsmap_numfileurls[it][it2];
|
||||
}
|
||||
}
|
||||
// Serialise to cout...
|
||||
|
|
Loading…
Add table
Reference in a new issue