1
0

uber.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding: utf-8 -*-
  2. import BeautifulSoup
  3. import datetime
  4. import dingguo
  5. import email
  6. import ioex
  7. import re
  8. def parse_order_confirmation_mail(mail):
  9. assert isinstance(mail, email.message.Message)
  10. html = mail.get_payload()[0].get_payload(decode = True)
  11. """ document in html2 has the same structure as the one in html.
  12. only difference is that hyperlink urls in html2 have been
  13. replaced by 'email.uber.com/wf/click?upn=.*' urls.
  14. """
  15. html2 = mail.get_payload()[1].get_payload()[0].get_payload(decode = True)
  16. route_map = mail.get_payload()[1].get_payload()[1].get_payload(decode = True)
  17. doc = BeautifulSoup.BeautifulSoup(
  18. html,
  19. convertEntities = BeautifulSoup.BeautifulSoup.HTML_ENTITIES,
  20. )
  21. # strptime
  22. with ioex.setlocale('en_US.UTF-8'):
  23. trip_id = re.search(
  24. ur'[\da-f\-]{36}',
  25. doc.find(text = 'Visit the trip page').parent['href'],
  26. ).group(0)
  27. order = dingguo.Order(
  28. u'uber',
  29. trip_id,
  30. datetime.datetime.strptime(
  31. doc.find(attrs = {'class': 'date'}).text,
  32. '%B %d, %Y',
  33. ).date(),
  34. )
  35. departure_time_tag = doc.find(attrs = {'class': 'from time'})
  36. departure_time = datetime.datetime.strptime(
  37. departure_time_tag.text,
  38. '%I:%M%p',
  39. ).time()
  40. arrival_time_tag = doc.find(attrs = {'class': 'to time'})
  41. arrival_time = datetime.datetime.strptime(
  42. arrival_time_tag.text,
  43. '%I:%M%p',
  44. ).time()
  45. distance = dingguo.Distance(
  46. float(doc.find(text = 'kilometers').parent.parent.find(attrs = {'class': 'data'}).text),
  47. u'km',
  48. )
  49. fare = doc.find(attrs = {'class': 'header-price'}).find(attrs = {'class': 'header-fare text-pad'}).text
  50. order.items.append(dingguo.TaxiRide(
  51. name = doc.find(text = 'CAR').parent.parent.find(attrs = {'class': 'data'}).text + ' Ride',
  52. price_brutto = dingguo.Sum(float(fare[1:]), fare[0]),
  53. arrival_time = datetime.datetime.combine(order.order_date, arrival_time),
  54. departure_time = datetime.datetime.combine(order.order_date, departure_time),
  55. departure_point = departure_time_tag.parent.find(attrs = {'class': 'address'}).text,
  56. destination_point = arrival_time_tag.parent.find(attrs = {'class': 'address'}).text,
  57. distance = distance,
  58. driver = doc.find(attrs = {'class': 'driver-info'}).text[len('You rode with '):],
  59. route_map = route_map,
  60. ))
  61. return [order]