peppercarrot icon indicating copy to clipboard operation
peppercarrot copied to clipboard

Generation of episode.json : insecure, wrong.

Open Deevad opened this issue 7 years ago • 1 comments

This code responsible for the generation of episode.json : https://github.com/Deevad/peppercarrot/blob/master/peppercarrot-main-menu.sh#L237 could be safer refactored in a separated file in Python.

Deevad avatar Jun 01 '17 16:06 Deevad

Suggesting this python implementation that can be called as a script as the comments indicate:

#!/usr/bin/env python
# compatible with python2 and python3, no dependencies except python itself

import sys
from glob import glob
from os import path
import json

# take as the first argument the episodes directory that we will inspect
webcomics_dir = sys.argv[1]

# take as the second argument the output filename we will write the JSON to
out_filename = sys.argv[2]


# helper function that takes a glob pattern as path parts, and returns an array of normalized paths
def globber(*pattern_path):
    return [ path.normpath(p) for p in glob( path.join(*pattern_path) ) ]


# generate our data structure by walking the webcomics directory
episodes_data = [
    {
        'name':                 path.basename(episode_dir),
        'total_pages':          len( globber(episode_dir, 'lang', 'gfx_*_E*P*.png') ) - 1, # don't count the last page (credits)
        'translated_languages': [
            path.basename(lang_dir)
            for lang_dir
            in globber(episode_dir, 'lang', '*/')
        ]
    }
    for episode_dir
    in globber(webcomics_dir, 'ep*_*/')
]


# write the data structure to the specified output file, in JSON
with open(out_filename, 'w') as out_file:
    out_file.write( json.dumps(episodes_data, indent=4) )

calimeroteknik avatar Jun 01 '17 18:06 calimeroteknik