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

  1. import os, argparse, json, re
  2. from datetime import date
  3. from app import ipfs_client, db, Paper
  4. def check_length(x, limit=30, allow_null=False):
  5. return (x and len(x) <= limit) or (allow_null and not x)
  6. if __name__ == "__main__":
  7. # parse arguments
  8. parser = argparse.ArgumentParser(description='add multiple learning resources simultaneously')
  9. parser.add_argument('path',
  10. type = str,
  11. nargs = 1,
  12. help = 'the root path to the resources')
  13. args = parser.parse_args()
  14. path = args.path[0]
  15. if not os.path.isdir(path):
  16. raise Exception(f'{path} is not a directory, which should have been the root path to the resources.')
  17. # load resources
  18. for course in os.listdir(path):
  19. course_path = os.path.join(path, course)
  20. if os.path.isdir(course_path):
  21. for d in os.listdir(course_path):
  22. dir_path = os.path.join(course_path, d)
  23. if os.path.isdir(dir_path):
  24. print(f'item {dir_path}')
  25. fm = None
  26. for r in os.listdir(dir_path):
  27. res_path = os.path.join(dir_path, r)
  28. if r == 'format.json':
  29. with open(res_path, 'r') as f:
  30. fm = json.load(f)
  31. jsonExistence = True
  32. if fm is None:
  33. raise Exception(f'there\'s no format.json in {dir_path}.')
  34. if len(os.listdir(dir_path)) == 1:
  35. raise Exception(f'there\'s no normal file in {dir_path}.')
  36. if fm['course'] != course:
  37. raise Exception('the course name in json {} is not equal to the directory name {}'.format(fm['course'], course))
  38. # check length
  39. if not check_length(fm['author']):
  40. raise Exception('the length of the name of author is too long: {}'.format(fm['author']))
  41. if not check_length(fm['teacher']):
  42. raise Exception('the length of the name of teacher is too long: {}'.format(fm['teacher']))
  43. if not check_length(fm['note'], 200, True):
  44. raise Exception('the legnth of the name of note is too long: {}'.format(fm['note']))
  45. # add directory and immediate files in the directory except format.json
  46. res = ipfs_client.add(dir_path,
  47. recursive = True,
  48. pattern = re.compile(r'^(?!format.json$)'))
  49. file_hash = ''
  50. for r in res:
  51. if r.get('Name') == d:
  52. file_hash = r.get('Hash')
  53. if file_hash == '':
  54. raise Exception('the directory is not put into IPFS, something wrong happened.')
  55. if not Paper.query.filter_by(file_hash = file_hash).count():
  56. paper = Paper(
  57. course = fm['course'],
  58. teacher = fm['teacher'],
  59. year = fm['year'],
  60. notes = fm['note'],
  61. anon = fm['author'] == '匿名',
  62. author = fm['author'],
  63. create_date = date.today(),
  64. file_hash = file_hash
  65. )
  66. db.session.add(paper)
  67. db.session.commit()
  68. print(f'item {dir_path} is added with hash {file_hash}')
  69. else:
  70. print(f'{dir_path} has been added with hash {file_hash} before')