utils.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Tweepy
  2. # Copyright 2010 Joshua Roesslein
  3. # See LICENSE for details.
  4. from __future__ import print_function
  5. from datetime import datetime
  6. import six
  7. from six.moves.urllib.parse import quote
  8. from email.utils import parsedate
  9. def parse_datetime(string):
  10. return datetime(*(parsedate(string)[:6]))
  11. def parse_html_value(html):
  12. return html[html.find('>')+1:html.rfind('<')]
  13. def parse_a_href(atag):
  14. start = atag.find('"') + 1
  15. end = atag.find('"', start)
  16. return atag[start:end]
  17. def convert_to_utf8_str(arg):
  18. # written by Michael Norton (http://docondev.blogspot.com/)
  19. if isinstance(arg, six.text_type):
  20. arg = arg.encode('utf-8')
  21. elif not isinstance(arg, bytes):
  22. arg = six.text_type(arg).encode('utf-8')
  23. return arg
  24. def import_simplejson():
  25. try:
  26. import simplejson as json
  27. except ImportError:
  28. try:
  29. import json # Python 2.6+
  30. except ImportError:
  31. try:
  32. # Google App Engine
  33. from django.utils import simplejson as json
  34. except ImportError:
  35. raise ImportError("Can't load a json library")
  36. return json
  37. def list_to_csv(item_list):
  38. if item_list:
  39. return ','.join([str(i) for i in item_list])