You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

79 lines
3.8 KiB

import os, argparse, json, re
from datetime import date
from app import ipfs_client, db, Paper
def check_length(x, limit=30, allow_null=False):
return (x and len(x) <= limit) or (allow_null and not x)
if __name__ == "__main__":
# parse arguments
parser = argparse.ArgumentParser(description='add multiple learning resources simultaneously')
parser.add_argument('path',
type = str,
nargs = 1,
help = 'the root path to the resources')
args = parser.parse_args()
path = args.path[0]
if not os.path.isdir(path):
raise Exception(f'{path} is not a directory, which should have been the root path to the resources.')
# load resources
for course in os.listdir(path):
course_path = os.path.join(path, course)
if os.path.isdir(course_path):
for d in os.listdir(course_path):
dir_path = os.path.join(course_path, d)
if os.path.isdir(dir_path):
print(f'item {dir_path}')
fm = None
for r in os.listdir(dir_path):
res_path = os.path.join(dir_path, r)
if r == 'format.json':
with open(res_path, 'r') as f:
fm = json.load(f)
jsonExistence = True
if fm is None:
raise Exception(f'there\'s no format.json in {dir_path}.')
if len(os.listdir(dir_path)) == 1:
raise Exception(f'there\'s no normal file in {dir_path}.')
if fm['course'] != course:
raise Exception('the course name in json {} is not equal to the directory name {}'.format(fm['course'], course))
# check length
if not check_length(fm['author']):
raise Exception('the length of the name of author is too long: {}'.format(fm['author']))
if not check_length(fm['teacher']):
raise Exception('the length of the name of teacher is too long: {}'.format(fm['teacher']))
if not check_length(fm['note'], 200, True):
raise Exception('the legnth of the name of note is too long: {}'.format(fm['note']))
# add directory and immediate files in the directory except format.json
res = ipfs_client.add(dir_path,
recursive = True,
pattern = re.compile(r'^(?!format.json$)'))
file_hash = ''
for r in res:
if r.get('Name') == d:
file_hash = r.get('Hash')
if file_hash == '':
raise Exception('the directory is not put into IPFS, something wrong happened.')
if not Paper.query.filter_by(file_hash = file_hash).count():
paper = Paper(
course = fm['course'],
teacher = fm['teacher'],
year = fm['year'],
notes = fm['note'],
anon = fm['author'] == '匿名',
author = fm['author'],
create_date = date.today(),
file_hash = file_hash
)
db.session.add(paper)
db.session.commit()
print(f'item {dir_path} is added with hash {file_hash}')
else:
print(f'{dir_path} has been added with hash {file_hash} before')