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.

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