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

Benjamin Mako Hill || Want to submit a patch?