index.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import io
  5. from PIL import Image
  6. from HTMLParser import HTMLParser
  7. import sqlite3 as lite
  8. import tornado.httpserver
  9. import tornado.ioloop
  10. import tornado.options
  11. import tornado.web
  12. from tornado.options import define, options
  13. define("port", default=8000, help="run on the given port", type=int)
  14. PATH=os.path.dirname(os.path.abspath(__file__))
  15. class MyHTMLParser(HTMLParser):
  16. def __init__(self):
  17. self.reset()
  18. self.ttype = "p"
  19. self.fed = []
  20. def handle_starttag(self, tag, attrs):
  21. self.ttype = str(tag)
  22. def handle_endtag(self, tag):
  23. self.ttype = "p"
  24. def handle_data(self, data):
  25. self.fed.append({"type": self.ttype,
  26. "text": data.strip()})
  27. def get_data(self):
  28. return self.fed
  29. class Application(tornado.web.Application):
  30. def __init__(self):
  31. handlers = [
  32. (r"/", MainHandler),
  33. (r"/img", ImageHandler),
  34. (r"/download", GenFileStreamerHandler),
  35. ]
  36. settings = dict(
  37. template_path=os.path.join(os.path.dirname(__file__), "templates"),
  38. static_path=os.path.join(os.path.dirname(__file__), "static"),
  39. debug=True,
  40. )
  41. tornado.web.Application.__init__(self, handlers, **settings)
  42. class MainHandler(tornado.web.RequestHandler):
  43. #def __init__(self):
  44. # con = lite.connect('fossil.db')
  45. #with con:
  46. # cur = con.cursor()
  47. def get(self):
  48. con = lite.connect('fossil.db')
  49. with con:
  50. cur = con.cursor()
  51. cur.execute("SELECT * FROM Fossil ORDER BY id")
  52. data = cur.fetchall()
  53. data_list = [ [x[0], x[1]] for x in data]
  54. current_index = 0
  55. parser = MyHTMLParser()
  56. print data[current_index][2]
  57. parser.feed(data[current_index][2])
  58. desc = parser.get_data()
  59. data_size = ("%.2f" % (data[current_index][3] / 1000000.0))
  60. self.render(
  61. "index.html",
  62. title = """Preservation of three-dimensional anatomy in phosphatized
  63. fossil arthropods enriches evolutionary inference""",
  64. author = """Achim H Schwermann, Tomy dos Santos Rolo, Michael S Caterino,
  65. Günter Bechly, Heiko Schmied, Tilo Baumbach, Thomas van de Kamp""",
  66. institute = """Steinmann Institute for Geology, Mineralogy and Paleontology,
  67. University of Bonn, Bonn, Germany; ANKA/Institute for Photon Science and
  68. Synchrotron Radiation, Karlsruhe Institute of Technology, Eggenstein-Leopoldshafen,
  69. Germany; Department of Agricultural and Environmental Sciences, Clemson University,
  70. Clemson, United States; State Museum of Natural History Stuttgart, Stuttgart, Germany;
  71. Institute of Crop Science and Resource Conservation, University of Bonn, Bonn, Germany;
  72. Laboratory for Applications of Synchrotron Radiation, Karlsruhe Institute of Technology,
  73. Karlsruhe, Germany""",
  74. data_list = data_list,
  75. desc = desc,
  76. data_size = data_size
  77. )
  78. class GenFileStreamerHandler(tornado.web.RequestHandler):
  79. CHUNK_SIZE = 512000
  80. DATA_PATH = "/home/ntj/fossils_for_nicholas"
  81. @tornado.web.asynchronous
  82. @tornado.gen.engine
  83. def get(self):
  84. cdid = self.get_arguments("did")
  85. con = lite.connect('fossil.db')
  86. with con:
  87. cur = con.cursor()
  88. cur.execute("SELECT name FROM Fossil WHERE name = ?", (cdid))
  89. data_key = cur.fetchall()
  90. if len(data_key) > 0:
  91. file_name = str(cdid[0]) + ".zip"
  92. self.path = self.DATA_PATH + str(cdid[0]) + "/" + file_name
  93. self.set_header('Content-Type',
  94. 'application/octet-stream')
  95. self.set_header('Content-Disposition',
  96. 'attachment; filename=' + file_name)
  97. self.set_header("Content-Length",
  98. os.path.getsize(self.path))
  99. self.flush()
  100. fd = open(self.path, "rb")
  101. data = fd.read(self.CHUNK_SIZE)
  102. while data:
  103. self.write(data)
  104. yield tornado.gen.Task(self.flush)
  105. data = fd.read(self.CHUNK_SIZE)
  106. fd.close()
  107. self.finish()
  108. class ImageHandler(tornado.web.RequestHandler):
  109. CHUNK_SIZE = 512000
  110. @tornado.web.asynchronous
  111. @tornado.gen.engine
  112. def get(self):
  113. #print self.get_arguments("did")
  114. self.path = PATH + '/static/data/1.png'
  115. self.set_header('Content-Type', 'image/png')
  116. self.set_header("Content-Length",
  117. os.path.getsize(self.path))
  118. self.flush()
  119. fd = open(self.path, "rb")
  120. data = fd.read(self.CHUNK_SIZE)
  121. while data:
  122. self.write(data)
  123. yield tornado.gen.Task(self.flush)
  124. data = fd.read(self.CHUNK_SIZE)
  125. fd.close()
  126. self.finish()
  127. if __name__ == "__main__":
  128. tornado.options.parse_command_line()
  129. http_server = tornado.httpserver.HTTPServer(Application())
  130. http_server.listen(options.port)
  131. tornado.ioloop.IOLoop.instance().start()