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.

177 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. i = 0
  69. course_list = {}
  70. for course_uri in courses:
  71. course = courses[course_uri]
  72. start_sem = course['start_semester']
  73. if start_sem != None:
  74. start_ind = ord_sems.index(start_sem)
  75. else:
  76. start_ind = 100
  77. end_sem = course['end_semester']
  78. if end_sem != None:
  79. end_ind = ord_sems.index(end_sem)
  80. else:
  81. end_ind = 100
  82. curr_ind = ord_sems.index(semester)
  83. if start_ind <= curr_ind <= end_ind:
  84. course_title = course['title']
  85. course_id = course['course_id']
  86. course_list[course_id] = course_title
  87. return course_list
  88. def get_top_folder(self, course):
  89. """
  90. Retrieves the top folder id of a given course.
  91. Parameters:
  92. course (string): course to get the top folder of
  93. Returns:
  94. string: id of the top folder
  95. """
  96. rsp = self.auth_req('/api.php/course/' + course + '/top_folder')
  97. top_folder = rsp.json()
  98. tf_id = top_folder['id']
  99. return(tf_id)
  100. def get_docs(self, folder):
  101. """
  102. Get all the documents of a given folder.
  103. Parameters:
  104. folder(string): id of the folder to get documents of
  105. Returns:
  106. list(string): ids of the documents
  107. """
  108. rsp = self.auth_req('/api.php/folder/' + folder)
  109. docs = rsp.json()['file_refs']
  110. res_docs = []
  111. for doc in docs:
  112. doc_id = doc['id']
  113. res_docs.append(doc_id)
  114. return(res_docs)
  115. def download(self, doc):
  116. """
  117. Download a document.
  118. Parameters:
  119. doc (string): id of the document to download
  120. """
  121. rsp1 = self.auth_req('/api.php/file/' + doc)
  122. doc_name = rsp1.json()['name']
  123. doc_chdate = rsp1.json()['chdate']
  124. last_dl = self.db.get_last_file_dl(doc)
  125. if last_dl == None or last_dl < doc_chdate:
  126. rsp2 = self.auth_req('/api.php/file/' + doc + '/download')
  127. total_size = int(rsp2.headers.get('content-length', 0))
  128. print('downloading ' + doc_name)
  129. progbar = tqdm(total=total_size, unit='iB', unit_scale=True)
  130. with open(doc_name, 'wb') as doc_file:
  131. for chunk in rsp2.iter_content(self.CHUNK_SIZE):
  132. progbar.update(len(chunk))
  133. doc_file.write(chunk)
  134. self.db.set_last_file_dl(str(doc), str(int(time.time())))
  135. def get_subdirs(self, folder):
  136. """
  137. Get all the subdirectories of a given folder.
  138. Parameters:
  139. folder(string): id of the folder to get subdirectories of
  140. Returns:
  141. list(string): ids of the subdirectories
  142. """
  143. rsp = self.auth_req('/api.php/folder/' + folder)
  144. subdirs = rsp.json()['subfolders']
  145. docs = rsp.json()['file_refs']
  146. res_subdirs = {}
  147. for subdir in subdirs:
  148. sub_id = subdir['id']
  149. sub_name = subdir['name']
  150. res_subdirs[sub_id] = sub_name
  151. return res_subdirs