-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
39 lines (29 loc) · 809 Bytes
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import json
import os.path
import requests
class RequestBase:
def __init__(self, url):
self.url = url
@property
def get(self):
return requests.get(url=self.url).json()
class JsonParser:
def __init__(self, path):
self.path = os.path.abspath(path)
@property
def exist(self):
return os.path.exists(self.path)
@property
def data(self):
if not self.exist:
return {}
with open(self.path, 'r', encoding='utf-8') as f:
return json.load(f)
def get(self, key, default=''):
try:
return self.data.get(key, default)
except:
return default
def set(self, data):
with open(self.path, 'w+', encoding='utf-8') as f:
json.dump(data, f, indent=4)