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