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.

176 lines
5.3 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. """
  14. Creates a request for a user.
  15. Parameter:
  16. url(string): URL to send the request to
  17. Returns:
  18. string: request
  19. """
  20. url = self.DOMAIN + url
  21. return req.get(url, auth=self.USER)
  22. def get_uid(self):
  23. """
  24. Get the user id of the user specified in the object.
  25. Returns:
  26. string: user id
  27. """
  28. rsp = self.auth_req('/api.php/user/')
  29. user_id = rsp.json()['user_id']
  30. return user_id
  31. def get_curr_semester(self):
  32. """
  33. Get the current semester of the studip instance specified in the object.
  34. Returns:
  35. string: id for current semester
  36. """
  37. rsp = self.auth_req('/api.php/semesters/')
  38. curr_time = int(str(int(time.time())))
  39. semesters = rsp.json()['collection']
  40. for sem_uri in semesters:
  41. semester = semesters[sem_uri]
  42. sem_begin = semester['begin']
  43. sem_end = semester['end']
  44. if sem_begin < curr_time < sem_end:
  45. return sem_uri
  46. return 0
  47. def get_ordered_semesters(self):
  48. """
  49. Get the a list of semesters of studip instance specified in the object.
  50. Returns:
  51. list(string): all semesters of the user
  52. """
  53. rsp = self.auth_req('/api.php/semesters/')
  54. semesters = rsp.json()['collection']
  55. order_sems = []
  56. for sem_uri in semesters:
  57. order_sems.append(sem_uri)
  58. return order_sems
  59. def get_curr_courses(self, user_id, semester):
  60. """
  61. Get the a list of semesters of studip instance specified in the object.
  62. Returns:
  63. string: id of the current semester
  64. """
  65. rsp = self.auth_req('/api.php/user/' + user_id + '/courses')
  66. ord_sems = self.get_ordered_semesters()
  67. courses = rsp.json()['collection']
  68. course_list = {}
  69. for course_uri in courses:
  70. course = courses[course_uri]
  71. start_sem = course['start_semester']
  72. if start_sem != None:
  73. start_ind = ord_sems.index(start_sem)
  74. else:
  75. start_ind = 100
  76. end_sem = course['end_semester']
  77. if end_sem != None:
  78. end_ind = ord_sems.index(end_sem)
  79. else:
  80. end_ind = 100
  81. curr_ind = ord_sems.index(semester)
  82. if start_ind <= curr_ind <= end_ind:
  83. course_title = course['title']
  84. course_id = course['course_id']
  85. course_list[course_id] = course_title
  86. return course_list
  87. def get_top_folder(self, course):
  88. """
  89. Retrieves the top folder id of a given course.
  90. Parameters:
  91. course (string): course to get the top folder of
  92. Returns:
  93. string: id of the top folder
  94. """
  95. rsp = self.auth_req('/api.php/course/' + course + '/top_folder')
  96. top_folder = rsp.json()
  97. tf_id = top_folder['id']
  98. return(tf_id)
  99. def get_docs(self, folder):
  100. """
  101. Get all the documents of a given folder.
  102. Parameters:
  103. folder(string): id of the folder to get documents of
  104. Returns:
  105. list(string): ids of the documents
  106. """
  107. rsp = self.auth_req('/api.php/folder/' + folder)
  108. docs = rsp.json()['file_refs']
  109. res_docs = []
  110. for doc in docs:
  111. doc_id = doc['id']
  112. res_docs.append(doc_id)
  113. return(res_docs)
  114. def download(self, doc):
  115. """
  116. Download a document.
  117. Parameters:
  118. doc (string): id of the document to download
  119. """
  120. rsp1 = self.auth_req('/api.php/file/' + doc)
  121. doc_name = rsp1.json()['name']
  122. doc_chdate = rsp1.json()['chdate']
  123. last_dl = self.db.get_last_file_dl(doc)
  124. if last_dl == None or last_dl < doc_chdate:
  125. rsp2 = self.auth_req('/api.php/file/' + doc + '/download')
  126. #total_size = int(rsp2.headers.get('content-length', 0))
  127. log.info('downloading ' + doc_name)
  128. #progbar = tqdm(total=total_size, unit='iB', unit_scale=True)
  129. with open(doc_name, 'wb') as doc_file:
  130. for chunk in rsp2.iter_content(self.CHUNK_SIZE):
  131. #progbar.update(len(chunk))
  132. doc_file.write(chunk)
  133. self.db.set_last_file_dl(str(doc), str(int(time.time())))
  134. def get_subdirs(self, folder):
  135. """
  136. Get all the subdirectories of a given folder.
  137. Parameters:
  138. folder(string): id of the folder to get subdirectories of
  139. Returns:
  140. list(string): ids of the subdirectories
  141. """
  142. rsp = self.auth_req('/api.php/folder/' + folder)
  143. subdirs = rsp.json()['subfolders']
  144. docs = rsp.json()['file_refs']
  145. res_subdirs = {}
  146. for subdir in subdirs:
  147. sub_id = subdir['id']
  148. sub_name = subdir['name']
  149. res_subdirs[sub_id] = sub_name
  150. return res_subdirs