<s> import httplib2 <EOL> import dateutil . parser <EOL> import dateutil . tz <EOL> from django . utils import simplejson <EOL> from django . utils . encoding import force_unicode <EOL> from django . conf import settings <EOL> from jellyroll . providers . utils . anyetree import etree <EOL> DEFAULT_HTTP_HEADERS = { <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> def getxml ( url , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> xml = fetch_resource ( url , ** kwargs ) <EOL> return etree . fromstring ( xml ) <EOL> def getjson ( url , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> json = fetch_resource ( url , ** kwargs ) <EOL> return simplejson . loads ( json ) <EOL> def fetch_resource ( url , method = "<STR_LIT:GET>" , body = None , username = None , password = None , headers = None ) : <EOL> h = httplib2 . Http ( timeout = <NUM_LIT:15> ) <EOL> h . force_exception_to_status_code = True <EOL> if username is not None or password is not None : <EOL> h . add_credentials ( username , password ) <EOL> if headers is None : <EOL> headers = DEFAULT_HTTP_HEADERS . copy ( ) <EOL> response , content = h . request ( url , method , body , headers ) <EOL> return content <EOL> def parsedate ( s ) : <EOL> """<STR_LIT>""" <EOL> dt = dateutil . parser . parse ( s ) <EOL> if dt . tzinfo : <EOL> dt = dt . astimezone ( dateutil . tz . tzlocal ( ) ) . replace ( tzinfo = None ) <EOL> return dt <EOL> def safeint ( s ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return int ( force_unicode ( s ) ) <EOL> except ( ValueError , TypeError ) : <EOL> return <NUM_LIT:0> <EOL> JELLYROLL_ADJUST_DATETIME = False <EOL> if hasattr ( settings , '<STR_LIT>' ) : <EOL> JELLYROLL_ADJUST_DATETIME = settings . JELLYROLL_ADJUST_DATETIME <EOL> if JELLYROLL_ADJUST_DATETIME : <EOL> try : <EOL> import pytz <EOL> except ImportError : <EOL> import logging <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> log . error ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> import datetime <EOL> import time <EOL> UTC = pytz . timezone ( '<STR_LIT>' ) <EOL> LOCAL = pytz . timezone ( settings . TIME_ZONE ) <EOL> def utc_to_local_datetime ( dt ) : <EOL> """<STR_LIT>""" <EOL> return dt . astimezone ( LOCAL ) <EOL> def utc_to_local_timestamp ( ts , orig_tz = UTC ) : <EOL> """<STR_LIT>""" <EOL> timestamp = datetime . datetime . fromtimestamp ( ts , tz = orig_tz ) <EOL> return timestamp . astimezone ( LOCAL ) <EOL> def utc_to_local_timestruct ( ts , orig_tz = UTC ) : <EOL> """<STR_LIT>""" <EOL> return utc_to_local_timestamp ( time . mktime ( ts ) , orig_tz ) </s>
