Advertisement
WupEly

Untitled

Mar 27th, 2023
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. from flask import Flask, Response
  2. import sqlite3
  3. from operator import itemgetter
  4.  
  5. app = Flask(__name__)
  6. app.should_ignore_error(True)
  7.  
  8.  
  9. @app.route("/schedule")
  10. def schcedule_route():
  11. return get_data()
  12.  
  13.  
  14. def get_data():
  15. try:
  16. with open("data.txt", "r") as read_file:
  17. file = read_file.read().split("\n")
  18. db_path = file[0]
  19. year = file[1]
  20. except Exception as e:
  21. return [str(e)]
  22. try:
  23. conn = sqlite3.connect(db_path)
  24. except Exception as e:
  25. return [str(e)]
  26. try:
  27. c = conn.cursor()
  28. except Exception as e:
  29. return [str(e)]
  30. try:
  31. c.execute("SELECT * FROM spaceships WHERE year = ?", (year,))
  32. except Exception as e:
  33. return [str(e)]
  34. try:
  35. result = []
  36. except Exception as e:
  37. return [str(e)]
  38. try:
  39. for el in c.fetchall():
  40. result.append({
  41. "id": el[0],
  42. "name": el[1],
  43. "load": el[2],
  44. "crew": el[3],
  45. "year": el[4]
  46. })
  47. except Exception as e:
  48. return [str(e)]
  49. try:
  50. result.sort(key=itemgetter("load", "name"))
  51. except Exception as e:
  52. return [str(e)]
  53. try:
  54. c.close()
  55. except Exception as e:
  56. return [str(e)]
  57. try:
  58. return result
  59. except Exception as e:
  60. return [str(e)]
  61.  
  62.  
  63. app.run()
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement