This program downloads all files of a Stud.IP users current semester.
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.

111 lines
3.7 KiB

  1. import time
  2. import logging as log
  3. from tqdm import tqdm
  4. import requests as req
  5. from requests.auth import HTTPBasicAuth
  6. class Studip:
  7. def __init__(self, chunk_size, domain, user, db):
  8. self.CHUNK_SIZE = chunk_size
  9. self.DOMAIN = domain
  10. self.USER = user
  11. self.db = db
  12. def auth_req(self, url):
  13. url = self.DOMAIN + url
  14. return req.get(url, auth=self.USER)
  15. def get_uid(self):
  16. rsp = self.auth_req('/api.php/user/')
  17. user_id = rsp.json()['user_id']
  18. return user_id
  19. def get_curr_semester(self):
  20. rsp = self.auth_req('/api.php/semesters/')
  21. curr_time = int(str(int(time.time())))
  22. semesters = rsp.json()['collection']
  23. for sem_uri in semesters:
  24. semester = semesters[sem_uri]
  25. sem_begin = semester['begin']
  26. sem_end = semester['end']
  27. if sem_begin < curr_time < sem_end:
  28. return sem_uri
  29. return 0
  30. def get_ordered_semesters(self):
  31. rsp = self.auth_req('/api.php/semesters/')
  32. semesters = rsp.json()['collection']
  33. order_sems = []
  34. for sem_uri in semesters:
  35. order_sems.append(sem_uri)
  36. return order_sems
  37. def get_curr_courses(self, user_id, semester):
  38. rsp = self.auth_req('/api.php/user/' + user_id + '/courses')
  39. ord_sems = self.get_ordered_semesters()
  40. courses = rsp.json()['collection']
  41. i = 0
  42. course_list = {}
  43. for course_uri in courses:
  44. course = courses[course_uri]
  45. start_sem = course['start_semester']
  46. if start_sem != None:
  47. start_ind = ord_sems.index(start_sem)
  48. else:
  49. start_ind = 100
  50. end_sem = course['end_semester']
  51. if end_sem != None:
  52. end_ind = ord_sems.index(end_sem)
  53. else:
  54. end_ind = 100
  55. curr_ind = ord_sems.index(semester)
  56. if start_ind <= curr_ind <= end_ind:
  57. course_title = course['title']
  58. course_id = course['course_id']
  59. course_list[course_id] = course_title
  60. return course_list
  61. def get_top_folder(self, course):
  62. rsp = self.auth_req('/api.php/course/' + course + '/top_folder')
  63. top_folder = rsp.json()
  64. tf_id = top_folder['id']
  65. return(tf_id)
  66. def get_docs(self, folder):
  67. rsp = self.auth_req('/api.php/folder/' + folder)
  68. docs = rsp.json()['file_refs']
  69. res_docs = []
  70. for doc in docs:
  71. doc_id = doc['id']
  72. res_docs.append(doc_id)
  73. return(res_docs)
  74. def download(self, doc):
  75. rsp1 = self.auth_req('/api.php/file/' + doc)
  76. doc_name = rsp1.json()['name']
  77. doc_chdate = rsp1.json()['chdate']
  78. last_dl = self.db.get_last_file_dl(doc)
  79. if last_dl == None or last_dl < doc_chdate:
  80. rsp2 = self.auth_req('/api.php/file/' + doc + '/download')
  81. total_size = int(rsp2.headers.get('content-length', 0))
  82. print('downloading ' + doc_name)
  83. progbar = tqdm(total=total_size, unit='iB', unit_scale=True)
  84. with open(doc_name, 'wb') as doc_file:
  85. for chunk in rsp2.iter_content(self.CHUNK_SIZE):
  86. progbar.update(len(chunk))
  87. doc_file.write(chunk)
  88. self.db.set_last_file_dl(str(doc), str(int(time.time())))
  89. def get_subdirs(self, folder):
  90. rsp = self.auth_req('/api.php/folder/' + folder)
  91. subdirs = rsp.json()['subfolders']
  92. docs = rsp.json()['file_refs']
  93. res_subdirs = {}
  94. for subdir in subdirs:
  95. sub_id = subdir['id']
  96. sub_name = subdir['name']
  97. res_subdirs[sub_id] = sub_name
  98. return res_subdirs