v1.8.0 (#105)
## Change log - Youtube support added - Custom filenames feature added - Custom folder structure feature added - Unsaving downloaded posts option added - Remove duplicate posts on different subreddits option added - Skipping given domains option added - Keeping track of already downloaded posts on a separate file option added (See --dowloaded-posts in README) - No audio on v.redd.it videos bug fixed (see README for details about ffmpeg) - --default-directory option is added - --default-options is added - --use-local-config option is added - Bug fixes
This commit is contained in:
58
src/jsonHelper.py
Normal file
58
src/jsonHelper.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import json
|
||||
from os import path, remove
|
||||
|
||||
from src.errors import InvalidJSONFile
|
||||
|
||||
class JsonFile:
|
||||
""" Write and read JSON files
|
||||
|
||||
Use add(self,toBeAdded) to add to files
|
||||
|
||||
Use delete(self,*deletedKeys) to delete keys
|
||||
"""
|
||||
|
||||
FILEDIR = ""
|
||||
|
||||
def __init__(self,FILEDIR):
|
||||
self.FILEDIR = FILEDIR
|
||||
if not path.exists(self.FILEDIR):
|
||||
self.__writeToFile({},create=True)
|
||||
|
||||
def read(self):
|
||||
try:
|
||||
with open(self.FILEDIR, 'r') as f:
|
||||
return json.load(f)
|
||||
except json.decoder.JSONDecodeError:
|
||||
raise InvalidJSONFile(f"{self.FILEDIR} cannot be read")
|
||||
|
||||
def add(self,toBeAdded,sub=None):
|
||||
"""Takes a dictionary and merges it with json file.
|
||||
It uses new key's value if a key already exists.
|
||||
Returns the new content as a dictionary.
|
||||
"""
|
||||
|
||||
data = self.read()
|
||||
if sub: data[sub] = {**data[sub], **toBeAdded}
|
||||
else: data = {**data, **toBeAdded}
|
||||
self.__writeToFile(data)
|
||||
return self.read()
|
||||
|
||||
def delete(self,*deleteKeys):
|
||||
"""Delete given keys from JSON file.
|
||||
Returns the new content as a dictionary.
|
||||
"""
|
||||
|
||||
data = self.read()
|
||||
for deleteKey in deleteKeys:
|
||||
if deleteKey in data:
|
||||
del data[deleteKey]
|
||||
found = True
|
||||
if not found:
|
||||
return False
|
||||
self.__writeToFile(data)
|
||||
|
||||
def __writeToFile(self,content,create=False):
|
||||
if not create:
|
||||
remove(self.FILEDIR)
|
||||
with open(self.FILEDIR, 'w') as f:
|
||||
json.dump(content, f, indent=4)
|
||||
Reference in New Issue
Block a user