PageRenderTime 56ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/main.py

https://gitlab.com/smidaharoun/devoirTunisiePython
Python | 197 lines | 183 code | 14 blank | 0 comment | 5 complexity | 0df67b530c757602bc1629d21c1493d9 MD5 | raw file
  1. import re
  2. import urllib2
  3. from bs4 import BeautifulSoup
  4. from flask import Flask, jsonify
  5. from flask import request
  6. app = Flask(__name__)
  7. main = "https://www.devoir.tn/"
  8. page = urllib2.urlopen(main)
  9. soup = BeautifulSoup(page, 'html.parser')
  10. soup.prettify()
  11. @app.route('/getMenus', methods=['GET'])
  12. def get_menu():
  13. right_table = soup.find_all('li', class_='sub-menu')
  14. menus = []
  15. accueil = soup.find("ul", class_="main-menu").find("li").find("a").contents[1].strip()
  16. menus.append({"id": 0, "name": accueil, "items ": []})
  17. id_item = 1
  18. for row in right_table:
  19. items = row.find_all("a")
  20. title = items[0].contents[1].strip()
  21. del items[0]
  22. subtitles = []
  23. for item in items:
  24. subtitles.append(item.text)
  25. hashmap = {"id": id_item, "name": title, "items ": subtitles}
  26. id_item += 1
  27. menus.append(hashmap)
  28. resp = jsonify(menus)
  29. resp.status_code = 200
  30. return resp
  31. @app.route('/getAccueil', methods=['GET'])
  32. def get_accueil():
  33. table = soup.find_all("div", {'class': re.compile(r'card pt-inner br-1.*')})
  34. items = []
  35. id_item = 0
  36. for row in table:
  37. header = row.find("div", {'class': re.compile(r'pti-header bgm.*')})
  38. number_document = header.find("p").text
  39. name_document = header.find("div").text
  40. type_document = header.find("small").text
  41. footer = row.find("div", {'class': re.compile(r'pti-footer.*')})
  42. link = footer.find("a").get("href")
  43. hashmap = {"id": id_item, "name": name_document, "value": number_document, "type": type_document,
  44. "link": link}
  45. id_item += 1
  46. items.append(hashmap)
  47. resp = jsonify(items)
  48. resp.status_code = 200
  49. return resp
  50. @app.route('/getLevel', methods=['POST'])
  51. def get_level():
  52. request_link = request.form["link"]
  53. url_level = main + request_link
  54. page_level = urllib2.urlopen(url_level)
  55. soup_level = BeautifulSoup(page_level, 'html.parser')
  56. soup_level.prettify()
  57. table = soup_level.find_all("div", {'class': re.compile(r'card br-1 bgb-amber.*')})
  58. items = []
  59. id_item = 0
  60. for row in table:
  61. header = row.find("div", class_="card-header bgm-amber text-left ch-dark")
  62. name_document = header.find("h2").contents[0]
  63. link = header.find("a")
  64. sub_items = row.find_all("a", class_="list-group-item media")
  65. list_items = []
  66. inner_id = 0
  67. for item in sub_items:
  68. item_link = item.get("href")
  69. item_name = item.find("div", class_="lgi-heading").text
  70. list_items.append({"id": inner_id, "name": item_name, "link": item_link.replace("./", "")})
  71. inner_id += 1
  72. hashmap = {"id": id_item, "name": name_document, "link": link.get("href").replace("./", ""), "subjects": list_items}
  73. id_item += 1
  74. items.append(hashmap)
  75. resp = jsonify(items)
  76. resp.status_code = 200
  77. return resp
  78. @app.route('/getYear', methods=['POST'])
  79. def get_year():
  80. request_link = request.form['link']
  81. url_level = main + request_link
  82. page_level = urllib2.urlopen(url_level)
  83. soup_level = BeautifulSoup(page_level, 'html.parser')
  84. soup_level.prettify()
  85. table = soup_level.find_all("div", {'class': re.compile(r'card br-1 bgb-amber.*')})
  86. items = []
  87. id_item = 0
  88. for row in table:
  89. header = row.find("div", class_="card-header bgm-amber text-left ch-dark")
  90. name_document = header.find("h2").contents[0]
  91. link = header.find("a")
  92. sub_items = row.find_all("a", class_="list-group-item media")
  93. list_items = []
  94. inner_id = 0
  95. for item in sub_items:
  96. item_link = item.get("href")
  97. item_name = item.find("div", class_="lgi-heading").text
  98. list_items.append({"id": inner_id, "name": item_name, "link": item_link.replace("../", "")})
  99. inner_id += 1
  100. hashmap = {"id": id_item, "name": name_document, "link": link.get("href").replace("../", ""), "items": list_items}
  101. id_item += 1
  102. items.append(hashmap)
  103. resp = jsonify(items)
  104. resp.status_code = 200
  105. return resp
  106. @app.route('/getSubject', methods=['POST'])
  107. def get_subject():
  108. request_link = request.form['link']
  109. url_level = main + request_link
  110. page_level = urllib2.urlopen(url_level)
  111. soup_level = BeautifulSoup(page_level, 'html.parser')
  112. soup_level.prettify()
  113. table = soup_level.find_all("div", {'class': re.compile(r'card br-1 bgb-amber.*')})
  114. items = []
  115. id_item = 0
  116. for row in table:
  117. header = row.find("div", class_="card-header bgm-amber text-left ch-dark")
  118. name_document = header.find("h2").contents[0]
  119. link = header.find("a")
  120. sub_items = row.find_all("a", class_="list-group-item media")
  121. list_items = []
  122. inner_id = 0
  123. for item in sub_items:
  124. item_link = item.get("href")
  125. item_name = item.find("div", class_="lgi-heading").text
  126. list_items.append({"id": inner_id, "name": item_name, "link": item_link.replace("../", "")})
  127. inner_id += 1
  128. hashmap = {"id": id_item, "name": name_document, "link": link.get("href").replace("../", ""), "items": list_items}
  129. id_item += 1
  130. items.append(hashmap)
  131. resp = jsonify(items)
  132. resp.status_code = 200
  133. return resp
  134. @app.route('/getDocument', methods=['POST'])
  135. def get_document():
  136. request_link = request.form['link']
  137. url_level = main + request_link
  138. page_level = urllib2.urlopen(url_level)
  139. soup_level = BeautifulSoup(page_level, 'html.parser')
  140. soup_level.prettify()
  141. table = soup_level.find_all("div", {'class': re.compile(r'card br-1 bgb.*')})
  142. items = []
  143. id_item = 0
  144. for row in table:
  145. header = row.find("div", {'class': re.compile(r'card-header.*')})
  146. button = header.find("ul", class_="dropdown-menu")
  147. if button is not None:
  148. li = button.find("li")
  149. list_link_document = li.find_all("a")
  150. list_link_docs = []
  151. for link_document in list_link_document:
  152. list_link_docs.append(link_document.get("href").replace("../", ""))
  153. name_tag = header.find("div", class_="media-body m-t-5")
  154. name_document = name_tag.find("h2").contents[0]
  155. date_document = name_tag.find("small").text
  156. body = row.find("div", class_="card-body br-t-1 bgb-blue")
  157. img = body.find("img")
  158. picture_document = img.get("src").replace("../", "")
  159. hashmap = {"id": id_item, "name": name_document.strip(), "date": date_document.strip(),
  160. "picture": picture_document, "link": list_link_docs}
  161. id_item += 1
  162. items.append(hashmap)
  163. resp = jsonify(items)
  164. resp.status_code = 200
  165. return resp
  166. if __name__ == '__main__':
  167. app.run()