Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. from bottle import get, post, run, route, request, response, hook, abort, put
  2. import spiceypy as spy
  3. import random
  4. import json
  5. import string
  6. import os
  7. from pymongo import MongoClient
  8. from bson import BSON
  9. from bson import json_util
  10. from bson.objectid import ObjectId
  11. import smtplib
  12. from email.mime.text import MIMEText
  13. import subprocess
  14.  
  15. connection = MongoClient('localhost', 27017)
  16. db = connection['documents']
  17. posts = db.posts
  18.  
  19.  
  20. @route('/<:re:.*>', method='OPTIONS')
  21. def enable_cors_generic_route():
  22. """
  23. This route takes priority over all others. So any request with an OPTIONS
  24. method will be handled by this function.
  25.  
  26. See: https://github.com/bottlepy/bottle/issues/402
  27.  
  28. NOTE: This means we won't 404 any invalid path that is an OPTIONS request.
  29. """
  30. add_cors_headers()
  31.  
  32.  
  33. @hook('before_request')
  34. def enable_cors_after_request_hook():
  35. """
  36. This executes after every route. We use it to attach CORS headers when
  37. applicable.
  38. """
  39. add_cors_headers()
  40.  
  41.  
  42. def add_cors_headers():
  43. if True: # You don't have to gate this
  44. response.headers['Access-Control-Allow-Origin'] = '*'
  45. response.headers['Access-Control-Allow-Methods'] = \
  46. 'GET, POST, PUT, OPTIONS'
  47. response.headers['Access-Control-Allow-Headers'] = \
  48. 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
  49.  
  50.  
  51. @route('/spiceoutput')
  52. class Spice():
  53. def __init__(self, lon, lat, alt, et, planet):
  54. self.lon = float(lon)
  55. self.lat = float(lat)
  56. self.alt = float(alt)
  57. self.et = et
  58. self.planet = planet
  59.  
  60. def spkFiles(self):
  61. '''
  62. Loads the kernel that are needed for dertermining the calculations
  63.  
  64. param: NONE
  65. return: none
  66. '''
  67.  
  68. # Must put the path to these files
  69. SPK = ["spice/de405.bsp"
  70. , "spice/pck00008.tpc.txt"
  71. , "spice/naif0012.txt"]
  72. spy.furnsh(SPK)
  73.  
  74. def spherToCart(self, lon, lat, alt, velocity, planet):
  75. '''
  76.  
  77. :param lon:
  78. :param lat:
  79. :param alt:
  80. :param velocity:
  81. :param planet:
  82. :return:
  83. '''
  84.  
  85. self.spkFiles()
  86.  
  87. input_state = [lon, lat, alt, velocity[0], velocity[1], velocity[2]]
  88. input_coord_sys = 'PLANETOGRAPHIC'
  89. output_corrd_sys = 'RECTANGULAR'
  90.  
  91. temp = spy.xfmsta(input_state, input_coord_sys, output_corrd_sys, planet)
  92.  
  93. list = temp[0], temp[1], temp[2]
  94.  
  95. return list
  96.  
  97. def sunData(self):
  98. '''
  99.  
  100. :param lon:
  101. :param lat:
  102. :param alt:
  103. :param et:
  104. :param planet:
  105. :return:
  106. '''
  107. frame = "J2000"
  108. abcorr = "None"
  109. target = "Sun"
  110. refloc = 'TARGET'
  111.  
  112. # get the files required
  113. self.spkFiles()
  114.  
  115. # gets correct time format
  116. et0 = self.et
  117. et1 = et0.replace('T', ' ')
  118. et2 = et1.replace('Z', '')
  119. et3 = spy.str2et(et2)
  120.  
  121. # print(et3)
  122.  
  123. # get the velocity of planet
  124. templist = spy.spkezr(self.planet, et3, frame, abcorr, target)
  125. velocity = templist[0][3], templist[0][4], templist[0][5]
  126.  
  127. # converts into x,y,z from lon,lat,alt
  128. xyz = self.spherToCart(self.lon, self.lat, self.alt, velocity, self.planet)
  129.  
  130. temp0 = spy.spkcpo(target, et3, frame, refloc, abcorr, xyz, self.planet, frame)
  131. sunPos = temp0[0][0], temp0[0][1], temp0[0][2]
  132. return json.dumps(sunPos)
  133.  
  134.  
  135. @route('/SunData')
  136. def SunData():
  137. lon = request.params.lon
  138. lat = request.params.lat
  139. alt = request.params.alt
  140. et = request.params.et
  141. planet = request.params.planet
  142.  
  143. print(alt)
  144.  
  145. lon = str(lon)
  146. lat = str(lat)
  147. alt = str(alt)
  148. lon = lon.replace('p', '.')
  149. lat = lat.replace('p', '.')
  150. alt = alt.replace('p', '.')
  151.  
  152. sun = Spice(lon, lat, alt, et, planet)
  153.  
  154. # URL I tested it on
  155. # http://localhost:8281/SunData?lon=29p001&lat=26p2121&alt=52p999&et=2012-08-04T10:00:00Z&planet=Mars
  156. dataString = sun.sunData()
  157. return dataString
  158.  
  159.  
  160. def base_str():
  161. return string.ascii_lowercase + string.ascii_uppercase + string.digits
  162.  
  163.  
  164. def key_gen(KEY_LEN=20):
  165. keylist = [random.choice(base_str()) for i in range(KEY_LEN)]
  166. return "".join(keylist)
  167.  
  168.  
  169. @put('/getData')
  170. def getData():
  171. json_text = request.json
  172. check_availablity(key_gen(20), email)
  173.  
  174.  
  175. @get('/temp')
  176. def doTemp():
  177. check_availablity(key_gen(20), "fake@gmail.com")
  178.  
  179.  
  180. def check_availablity(randomid="", email=""):
  181. entity = posts.find_one({'vid': randomid})
  182. print(entity)
  183. if not entity:
  184. # TODO: EDIT PATH ON LINUX SYSTEM
  185. mypath = 'C:\\jobs\\' + str(randomid)
  186. if not os.path.isdir(mypath):
  187. os.makedirs(mypath)
  188. print(mypath)
  189. try:
  190. abspath = "C:\\output\\" + randomid
  191. posts.insert_one({'vid': randomid, 'email': email, 'path': mypath, 'output': abspath})
  192. # sub = subprocess.Popen([alt], shell=True)
  193. except RuntimeError:
  194. pass
  195. else:
  196. check_availablity(key_gen(20), email)
  197. print(posts.find_one({'email': "fake@gmail.com"}))
  198.  
  199.  
  200. # SEND THE MOVIE NAME HERE
  201. @post('/completed')
  202. def sendEmail():
  203. postdata = request.body.read()
  204. print(postdata)
  205. entity = json.loads(posts.find_one({'vid': postdata}))
  206. if not entity:
  207. abort(404,"No such video")
  208. else:
  209. temp = json.dumps(entity, sort_keys=True, indent=4, default=json_util.default)
  210. temp1 = json.loads(temp)
  211. to = temp1["email"]
  212. text = temp1["output"]
  213. sendMail('temp@gmail.com', to, 'Your Movie from MarsTrek has completed!', text, 'smtp.gmail.com')
  214.  
  215.  
  216. def sendMail(FROM, TO, SUBJECT, TEXT, SERVER):
  217. import smtplib
  218. """this is some test documentation in the function"""
  219. message = """\
  220. From: %s
  221. To: %s
  222. Subject: %s
  223. %s
  224. """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
  225. # Send the mail
  226. server = smtplib.SMTP(SERVER)
  227. "New part"
  228. server.starttls()
  229. server.login('username', 'password')
  230. server.sendmail(FROM, TO, message)
  231. server.quit()
  232.  
  233.  
  234. run(host='localhost', port=8281, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement