獲取Jawbone UP中的個人數據(三)Python實現


Author : iascchen(at)gmail(dot)com

Date : 2013-06-29

新浪微博 : @問天鼓


利用Python實現了 Jawbone UP 的 API。對應API詳細說明參見 獲取Jawbone UP中的個人數據(二)非官方API

代碼地址

https://github.com/iascchen/VisHealth/

使用 devices/jawboneup.py 即可。

依賴包

requests

源代碼說明

jawboneurl.py 爲 Jawbone UP 服務請求 URL 入口生成工具。

jawboneup.py 爲Jawbone UP 非官方 API 的 Python 實現代碼。它的 main 方法是使用這些API的示例。

  1. Jawbone UP API 被封裝在 class DeviceJawboneUp 中
  2. 獲取Jawbone 的數據之前,首先登錄。登錄成功之後,能夠獲得用於維持會話的 Token。這個Token將被存放爲 DeviceJawboneUp 實例中的 nudgeHeaders 變量中,每次向服務器的請求,都需要將 nudgeHeaders 的加載在 HTTP Header 中提供。
  3. 登錄之後,user 的 Xid, 身份認賬 token 這兩個參數,也會被存儲於 DeviceJawboneUp 實例中的 auth_info 變量中。
  4. 如果函數調用之後,各函數會返回所收到的JSON文件。如果調用失敗,會拋出異常,或打出 Error Code 。

代碼示例

爲了方便理解,下面對原來main中的代碼進行了最簡化改寫,去掉了一些不必要的爲了輸出和驗證的代碼。


Part 1. 登陸

這兩種登陸方法都可用,任選其一即可,Jawbone UP 2 的應用中使用的是 users/login 。

函數說明:

get_users_login(self , email , password )
get_auth_info(self , email , password )

參考代碼:

account = { "email" : "your@email" , "passwd" : "yourpassword" }

device = DeviceJawboneUp ()

# API users/login
device.get_users_login(account["email"], account["passwd"])
print device.auth_info

# API user/signin/login
device.get_auth_info(account["email"], account["passwd"])
print device.auth_info

Part 2. 用戶行爲概況

函數說明:

# 返回 datestr 日期那天的數據。
# userXid , 取值可以有兩種形式: 
#   @me ,用於訪問自己的信息; 
#   可以使用 login 返回的 "user"."xid" 訪問自己的信息。或者可以利用從朋友查找中獲取的 User XID值,查詢他人的信息(推測,未實驗)。
# datestr 格式爲yyyymmdd. 缺省 datestr = None,返回今日數據。

get_users_score(self , userXid = "@me", datestr = None) 

# 返回 datestr 日期之前的用戶事件,最多返回 limit 條。
# datestr,用戶事件的截止日期。格式爲yyyymmdd. 缺省 datestr = None,返回截止日期爲今日.
# limit,整數,缺省爲20。用於限制返回多少條結果  

get_users_social(self , userXid = "@me", datestr = None , limit = 20)

# 獲得從當前日期向前的用戶事件,最多返回 limit 條。

get_users_feed(self , userXid = "@me", limit = 10)

# 獲得從某用戶事件的詳情 。

get_feeditems(self , evtXid )

# users/%userXid%/events 是個非常有用的函數。利用這個函數,能夠獲取當前用戶的所有 Event, 包括已經被刪除的用戶事件。同時,這個 API 還有 types 參數,能夠限制只返回特定類別 Event。
# startDate, 開始日期,格式爲yyyymmdd
# types, 選擇哪種類型的事件。取值爲 : 1 workout, 2 meal, 3 sleep, 4 move, 5 mood, 7 body。需要顯示多個類型,可以將類型取值用逗號連接。不設置則包括所有類型。此處取值爲逆向工程得出,不一定全面。
# limit, 最多返回多少條記錄,缺省爲20。
# listDeleted, 取值爲 True, False。缺省爲 False。

get_users_events(self  ,userXid = "@me" , startDate = None, types = None , limit = 20 , listDeleted = True )

參考代碼:

account = { "email" : "your@email" , "passwd" : "yourpassword" }

startDate = "20130609"
endDate =  "20130612"

device = DeviceJawboneUp ()

# login
device.get_users_login(account["email"], account["passwd"])

# users/%userXid%/score
score = device.get_users_score( datestr = startDate )
score = device.get_users_score( )

# users/%userXid%/social
social = device.get_users_social( datestr = startDate)
social = device.get_users_social( )

feeds = social["data"]["feed"]
for feed in feeds:
    try:
        print feed["type"], feed["title"]
        ret = device.get_event( feed["type"] , feed["xid"])
    except:
        pass

# users/%userXid%/feed
userfeeds = device.get_users_feed( limit = 5 )

feeds = userfeeds["data"]["feed"]
for feed in feeds:
    try:
        print feed["type"], feed["title"]
        feeditem = device.get_feeditems(  feed["activity_xid"] )
    except:
        pass

# users/%userXid%/events 
events = device.get_users_events( startDate = startDate, listDeleted = True , limit = 10 )
device.displayEvents( events)

tys = ["1" , "3"]
eventsintypes = device.get_users_events( limit = 10 , types = ','.join(tys) , listDeleted = True )
device.displayEvents( eventsintypes )

# check event type
for t in range(1, 10) :
    ret = device.get_users_events( types = t , limit = 10 , listDeleted = True )
    items = ret["data"]["items"]
    if( (items != None) & (len(items) > 0) ):
        print t , items[0]["type"]

Part 3. Sleeps

函數說明:

# 獲得 [ startTime , endTime ] 期間之內的睡眠情況。
# userXid , 取值可以有兩種形式: 
#   @me ,用於訪問自己的信息; 
#   可以使用 login 返回的 "user"."xid" 訪問自己的信息。或者可以利用從朋友查找中獲取的 User XID值,查詢他人的信息(推測,未實驗)。
# startTime, 爲 long 型時間,即從1970年以來的秒數
# endTime, 爲 long 型時間,即從1970年以來的秒數
# limit, 最多返回結果條數限制

get_users_sleeps(self  , startTime , endTime , userXid = "@me", limit = 100)

# 某次睡眠信息情況。
# evtXid,例如可以從 user/%userXid%/social 結果中得到的的 "data"."feed"."xid" 獲得 %evtXid%

get_sleeps(self , evtXid )

# 某次睡眠的按時間狀態詳情。

get_sleeps_snapshot(self , evtXid )

參考代碼:

account = { "email" : "your@email" , "passwd" : "yourpassword" }

startDate = "20130609"
sDate = time.strptime(startDate, "%Y%m%d")
start = long( time.mktime(sDate) )

endDate = "20130612"
eDate = time.strptime(endDate, "%Y%m%d")
end = long( time.mktime(eDate) )

device = DeviceJawboneUp ()

# login
device.get_users_login(account["email"], account["passwd"])

# users/%userXid%/events in type 3 is sleep 
usersleeps = device.get_users_events( types = "3" , limit = 10 , listDeleted = False )

# user/%userXid%/sleeps
usersleeps = device.get_users_sleeps( startTime = start , endTime = end , limit = 100)

# display sleeps
items = usersleeps["data"]["items"]
for item in items:
    xid = item["xid"]
    print item["type"], " " , xid

    sleep = device.get_sleeps( evtXid = xid )
    sleepsnap = device.get_sleeps_snapshot( evtXid = xid )

Part 4. Moves

函數說明:

# 某次運動信息情況。
# evtXid,例如可以從 user/%userXid%/social 結果中得到的的 "data"."feed"."xid" 獲得 %evtXid%

get_moves(self , evtXid )

# 某次運動的按時間狀態詳情。
# bucket,運動數據彙總時間顆粒度,以秒爲單位,最小取值爲 60 

get_moves_snapshot(self , evtXid , bucket = 100 )

參考代碼:

account = { "email" : "your@email" , "passwd" : "yourpassword" }

startDate = "20130609"
sDate = time.strptime(startDate, "%Y%m%d")
start = long( time.mktime(sDate) )

device = DeviceJawboneUp ()

# login
device.get_users_login(account["email"], account["passwd"])

# users/%userXid%/events in type 4 is moves 
moves = device.get_users_events( types = "4" , limit = 10 , listDeleted = False )

# display moves
items = moves["data"]["items"]
for item in items:
    xid = item["xid"]
    print item["type"], " " , xid

    move = device.get_moves( evtXid = xid )
    movesnap = device.get_moves_snapshot( evtXid = xid )

Part 5. Workouts

函數說明:

# 獲得 [ startTime , endTime ] 期間之內的鍛鍊情況。
# userXid , 取值可以有兩種形式: 
#   @me ,用於訪問自己的信息; 
#   可以使用 login 返回的 "user"."xid" 訪問自己的信息。或者可以利用從朋友查找中獲取的 User XID值,查詢他人的信息(推測,未實驗)。
# startTime, 爲 long 型時間,即從1970年以來的秒數
# endTime, 爲 long 型時間,即從1970年以來的秒數
# limit, 最多返回結果條數限制

get_users_workouts(self  , startTime , endTime , userXid = "@me", limit = 100)

# 某次鍛鍊信息情況。
# evtXid,例如可以從 user/%userXid%/social 結果中得到的的 "data"."feed"."xid" 獲得 %evtXid%

get_workouts(self , evtXid )

# 某次鍛鍊的按時間狀態詳情。並非所有的 workouts/%evtXid%/snapshot 都會返回詳細的數據,一些鍛鍊方式,如:步行、跑步等,使用 snapshot 能夠返回細節運動數據;而另一些鍛鍊方式,如:瑜伽,則沒有細節運動數據。
# bucket,運動數據彙總時間顆粒度,以秒爲單位,最小取值爲 60 

get_workouts_snapshot(self , evtXid , bucket = 100 )

參考代碼:

account = { "email" : "your@email" , "passwd" : "yourpassword" }

startDate = "20130609"
sDate = time.strptime(startDate, "%Y%m%d")
start = long( time.mktime(sDate) )

device = DeviceJawboneUp ()

# login
device.get_users_login(account["email"], account["passwd"])

# users/%userXid%/events in type 1 is workouts 
workouts = device.get_users_events( types = "1" , limit = 10 , listDeleted = False )

# user/%userXid%/workouts
workouts = device.get_users_workouts( startTime = start , endTime = None , limit = 10)

# display workouts
items = workouts["data"]["items"]
for item in items:
    xid = item["xid"]
    print item["type"], " " , xid

    workout = device.get_workouts( evtXid = xid )
    workoutsnap = device.get_workouts_snapshot( evtXid = xid , bucket = 600 )

Part 6. Meals & Mood

函數說明:

# 獲得用戶最近一次Moods情況。
# userXid , 取值可以有兩種形式: 
#   @me ,用於訪問自己的信息; 
#   可以使用 login 返回的 "user"."xid" 訪問自己的信息。或者可以利用從朋友查找中獲取的 User XID值,查詢他人的信息(推測,未實驗)。

get_users_moods(self  ,userXid = "@me")

# evtXid,例如可以從 user/%userXid%/social 結果中得到的的 "data"."feed"."xid" 獲得 %evtXid%

# 某次飲食信息情況。
get_meals(self , evtXid )
# 某次Mood信息情況。
get_moods(self , evtXid )

參考代碼:

account = { "email" : "your@email" , "passwd" : "yourpassword" }

device = DeviceJawboneUp ()

# login
device.get_users_login(account["email"], account["passwd"])

# Meal
meals = device.get_users_events( types = "2" , limit = 10 , listDeleted = True )

items = meals["data"]["items"]
for item in items:
    xid = item["xid"]
    print item["type"], " " , xid

    meal = device.get_meals( evtXid = xid )

# Moods
# user/%userXid%/moods
currentmood = device.get_users_moods( device.auth_info["xid"] )    
xid = currentmood["data"]["xid"]
mood = device.get_moods( evtXid = xid )

# user/%userXid%/events in type 5 is mood
moods = device.get_users_events( types = "5" , limit = 10 , listDeleted = True )

items = moods["data"]["items"]
for item in items:
    xid = item["xid"]
    print item["type"], " " , xid

    mood = device.get_moods( evtXid = xid )

# Body, 
# user/%userXid%/events in type 7 is body , such as weight data from other App named Withings
body = device.get_users_events( types = "7" , limit = 10 , listDeleted = True )
items = body["data"]["items"]
for item in items:
    print item["type"], " " , item["xid"]

Part 7. Other User Proiles

函數說明:

# userXid , 取值可以有兩種形式: 
#   @me ,用於訪問自己的信息; 
#   可以使用 login 返回的 "user"."xid" 訪問自己的信息。或者可以利用從朋友查找中獲取的 User XID值,查詢他人的信息(推測,未實驗)。

get_users_acknowledgement(self , userXid = "@me")

get_users_aliases(self , userXid = "@me")

get_users_friends(self , userXid = "@me")

get_users_goals(self , userXid = "@me")

get_users_profile(self , userXid = "@me")

get_users_photo(self , userXid = "@me")

get_users_settings(self , userXid = "@me")

get_users_timezone(self , userXid = "@me")

參考代碼:

account = { "email" : "your@email" , "passwd" : "yourpassword" }

device = DeviceJawboneUp ()

# login
device.get_users_login(account["email"], account["passwd"])

# display privicy setting
acknowledgement = device.get_users_acknowledgement(  )
acknowledgement = device.get_users_acknowledgement( device.auth_info["xid"] )

# alias
aliases = device.get_users_aliases(  )
aliases = device.get_users_aliases( device.auth_info["xid"] )

# friends
friends = device.get_users_friends( )
friends = device.get_users_friends( device.auth_info["xid"] )

# what's you had done ?
goals = device.get_users_goals( )
goals = device.get_users_goals( device.auth_info["xid"] )

# user profiles
profile = device.get_users_profile( )
profile = device.get_users_profile( device.auth_info["xid"] )

photo = device.get_users_photo( )
photo = device.get_users_photo( device.auth_info["xid"] )

# setting for up hardware
setting = device.get_users_settings( )
setting = device.get_users_settings( device.auth_info["xid"] )

timezone = device.get_users_timezone( )
setting = device.get_users_settings( device.auth_info["xid"] )

Part 8. Trends & Band

函數說明:

# 返回用戶運動的歷史統計。
# endDate , 統計截止日期,格式爲 yyyymmdd
# bucketSize, 取值爲 : d 日 , w 周, m 月
# inRange ,  取值似乎只能爲 d 按天
# rangeDuration,返回的最大的區間,與 range 有關。

get_users_trends(self, userXid = "@me" , endDate = None , bucketSize = "d", inRange = "d" , rangeDuration = 730 ):

# 時間顆粒度爲分鐘,顯示用戶上傳的詳細運動和能量消耗數據。
# startTime, 爲 long 型時間,即從1970年以來的秒數
# endTime, 爲 long 型時間,即從1970年以來的秒數

get_users_band(self , startTime , endTime , userXid = "@me"):

參考代碼:

account = { "email" : "your@email" , "passwd" : "yourpassword" }

startDate = "20130609"
sDate = time.strptime(startDate, "%Y%m%d")
start = long( time.mktime(sDate) )

endDate = "20130612"
eDate = time.strptime(endDate, "%Y%m%d")
end = long( time.mktime(eDate) )

device = DeviceJawboneUp ()

# login
device.get_users_login(account["email"], account["passwd"])

# trends

monthtrends = device.get_users_trends( endDate =  None , inRange = "d" , bucketSize = "m"  )
weektrends = device.get_users_trends( endDate =  None , inRange = "d" , bucketSize = "w"  )
daytrends = device.get_users_trends( endDate =  None , inRange = "d" , bucketSize = "d"  )
daytrends = device.get_users_trends( endDate =  endDate , inRange = "d" , bucketSize = "d"  )

# Band
band = device.get_users_band( startTime = start , endTime = end )

其他

其他還有一些社交類、飲食類 API 與用戶行爲數據相關性不大,不再進行分析。

祝各位玩的開心!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章