<s> import xml . etree . ElementTree as ET <EOL> import datetime <EOL> import urllib , urllib2 <EOL> import logging <EOL> _log = logging . getLogger ( "<STR_LIT>" ) <EOL> class Client ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user_id , sid , uid , uis , url_base = "<STR_LIT>" ) : <EOL> self . user_id = user_id <EOL> self . sid = sid <EOL> self . uid = uid <EOL> self . uis = uis <EOL> self . url_base = url_base <EOL> self . _request_cookie = "<STR_LIT>" % ( sid , uid , uis ) <EOL> def intraday_calories_burned ( self , date ) : <EOL> """<STR_LIT>""" <EOL> return self . _graphdata_intraday_request ( "<STR_LIT>" , date ) <EOL> def intraday_active_score ( self , date ) : <EOL> """<STR_LIT>""" <EOL> return self . _graphdata_intraday_request ( "<STR_LIT>" , date ) <EOL> def intraday_steps ( self , date ) : <EOL> """<STR_LIT>""" <EOL> return self . _graphdata_intraday_request ( "<STR_LIT>" , date ) <EOL> def intraday_sleep ( self , date , sleep_id = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _graphdata_intraday_sleep_request ( "<STR_LIT>" , date , sleep_id = sleep_id ) <EOL> def _request ( self , path , parameters ) : <EOL> parameters = dict ( [ ( k , v ) for k , v in parameters . items ( ) if v ] ) <EOL> query_str = urllib . urlencode ( parameters ) <EOL> request = urllib2 . Request ( "<STR_LIT>" % ( self . url_base , path , query_str ) , headers = { "<STR_LIT>" : self . _request_cookie } ) <EOL> _log . debug ( "<STR_LIT>" , request . get_full_url ( ) ) <EOL> data = None <EOL> try : <EOL> response = urllib2 . urlopen ( request ) <EOL> data = response . read ( ) <EOL> response . close ( ) <EOL> except urllib2 . HTTPError as httperror : <EOL> data = httperror . read ( ) <EOL> httperror . close ( ) <EOL> return ET . fromstring ( data . strip ( ) ) <EOL> def _graphdata_intraday_xml_request ( self , graph_type , date , data_version = <NUM_LIT> , ** kwargs ) : <EOL> params = dict ( <EOL> userId = self . user_id , <EOL> type = graph_type , <EOL> version = "<STR_LIT>" , <EOL> dataVersion = data_version , <EOL> chart_Type = "<STR_LIT>" , <EOL> period = "<STR_LIT>" , <EOL> dateTo = str ( date ) <EOL> ) <EOL> if kwargs : <EOL> params . update ( kwargs ) <EOL> return self . _request ( "<STR_LIT>" , params ) <EOL> def _graphdata_intraday_request ( self , graph_type , date ) : <EOL> xml = self . _graphdata_intraday_xml_request ( graph_type , date ) <EOL> base_time = datetime . datetime . combine ( date , datetime . time ( ) ) <EOL> timestamps = [ base_time + datetime . timedelta ( minutes = m ) for m in xrange ( <NUM_LIT:0> , <NUM_LIT> * <NUM_LIT:5> , <NUM_LIT:5> ) ] <EOL> values = [ int ( float ( v . text ) ) for v in xml . findall ( "<STR_LIT>" ) ] <EOL> return zip ( timestamps , values ) <EOL> def _graphdata_intraday_sleep_request ( self , graph_type , date , sleep_id = None ) : <EOL> xml = self . _graphdata_intraday_xml_request ( graph_type , date , data_version = <NUM_LIT> , arg = sleep_id ) <EOL> elements = xml . findall ( "<STR_LIT>" ) <EOL> timestamps = [ datetime . datetime . strptime ( e . attrib [ '<STR_LIT:description>' ] . split ( '<STR_LIT:U+0020>' ) [ - <NUM_LIT:1> ] , "<STR_LIT>" ) for e in elements ] <EOL> last_stamp = None <EOL> datetimes = [ ] <EOL> base_date = date <EOL> for timestamp in timestamps : <EOL> if last_stamp and last_stamp > timestamp : <EOL> base_date -= datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> last_stamp = timestamp <EOL> last_stamp = None <EOL> for timestamp in timestamps : <EOL> if last_stamp and last_stamp > timestamp : <EOL> base_date += datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> datetimes . append ( datetime . datetime . combine ( base_date , timestamp . time ( ) ) ) <EOL> last_stamp = timestamp <EOL> values = [ int ( float ( v . text ) ) for v in xml . findall ( "<STR_LIT>" ) ] <EOL> return zip ( datetimes , values ) </s>
