Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import csv
- import json
- file = open('cats_subcats.json', 'r', encoding='UTF-8')
- categories = json.loads(file.read())
- file.close()
- dirs = list(filter(os.path.isdir, os.listdir(os.getcwd())))
- if(not os.path.isdir('shares/')): os.mkdir('shares/')
- for category in categories:
- catpost = '/category_'+str(category['id'])+'.csv'
- shares = []
- for dirt in dirs:
- if(os.path.isfile(dirt+catpost)):
- file = open(dirt+catpost, 'r', encoding='UTF-8')
- shares.extend(list(csv.DictReader(file, fieldnames=('subcat_id', 'subcat_name', 'share_id', 'share_hash', 'share_name', 'share_size'), delimiter=';', quotechar='"')))
- file.close()
- for subcat in category['subcats']:
- subshares = {}
- for share in shares[:]:
- if(int(share['subcat_id']) == subcat['id']):
- subshares[share['share_id']] = {
- 'hash': share['share_hash'],
- 'name': share['share_name'],
- 'size': int(share['share_size'])
- }
- shares.remove(share)
- output = []
- for key in subshares: output.append({
- 'id': int(key),
- 'hash': subshares[key]['hash'],
- 'name': subshares[key]['name'],
- 'size': subshares[key]['size']
- })
- outname = 'shares/cat_'+str(category['id'])+'_subcat_'+str(subcat['id'])+'.json'
- if(os.path.isfile(outname)): os.remove(outname)
- outfile = open(outname, 'w', encoding='UTF-8')
- outfile.write(json.dumps(output, indent=4, sort_keys=True))
- outfile.close()
Advertisement
Add Comment
Please, Sign In to add comment