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

Benjamin Mako Hill || Want to submit a patch?