Reputation: 814
So i'm trying to fetch a list of channels that a user follows on Twitch,
I'm using urllib2 to fetch the data from:
http://api.twitch.tv/kraken/users/user/follows/channels?offset=0&on_site=1
however I only want a list of channels that the user follows, here is what the raw json data looks like, I only want the display_name from each channel e.g ['Syndicate','Gem28daz2012'], how could I do this?
{
"follows": [{
"created_at": "2016-01-15T22:43:35Z",
"_links": {
"self": "https://api.twitch.tv/kraken/users/user/follows/channels/syndicate"
},
"notifications": false,
"channel": {
"_links": {
"self": "https://api.twitch.tv/kraken/channels/syndicate",
"follows": "https://api.twitch.tv/kraken/channels/syndicate/follows",
"commercial": "https://api.twitch.tv/kraken/channels/syndicate/commercial",
"stream_key": "https://api.twitch.tv/kraken/channels/syndicate/stream_key",
"chat": "https://api.twitch.tv/kraken/chat/syndicate",
"features": "https://api.twitch.tv/kraken/channels/syndicate/features",
"subscriptions": "https://api.twitch.tv/kraken/channels/syndicate/subscriptions",
"editors": "https://api.twitch.tv/kraken/channels/syndicate/editors",
"videos": "https://api.twitch.tv/kraken/channels/syndicate/videos",
"teams": "https://api.twitch.tv/kraken/channels/syndicate/teams"
},
"background": null,
"banner": null,
"broadcaster_language": "en",
"display_name": "Syndicate",
"game": "Counter-Strike: Global Offensive",
"logo": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-profile_image-03bf1d6ad0025f86-300x300.png",
"mature": false,
"status": "Road To MLG! (CS:GO Competiive) ",
"partner": true,
"url": "http://www.twitch.tv/syndicate",
"video_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-channel_offline_image-a15fc19d9828b9c7-640x360.png",
"_id": 16764225,
"name": "syndicate",
"created_at": "2010-10-17T23:19:43Z",
"updated_at": "2016-01-22T22:17:33Z",
"delay": null,
"followers": 2353424,
"profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-profile_banner-5f912a83f7e9bafe-480.png",
"profile_banner_background_color": null,
"views": 38140282,
"language": "en"
}
}, {
"created_at": "2016-01-12T21:01:49Z",
"_links": {
"self": "https://api.twitch.tv/kraken/users/user/follows/channels/gem28daz2012"
},
"notifications": false,
"channel": {
"_links": {
"self": "http://api.twitch.tv/kraken/channels/gem28daz2012",
"follows": "http://api.twitch.tv/kraken/channels/gem28daz2012/follows",
"commercial": "http://api.twitch.tv/kraken/channels/gem28daz2012/commercial",
"stream_key": "http://api.twitch.tv/kraken/channels/gem28daz2012/stream_key",
"chat": "http://api.twitch.tv/kraken/chat/gem28daz2012",
"features": "http://api.twitch.tv/kraken/channels/gem28daz2012/features",
"subscriptions": "http://api.twitch.tv/kraken/channels/gem28daz2012/subscriptions",
"editors": "http://api.twitch.tv/kraken/channels/gem28daz2012/editors",
"videos": "http://api.twitch.tv/kraken/channels/gem28daz2012/videos",
"teams": "http://api.twitch.tv/kraken/channels/gem28daz2012/teams"
},
"background": null,
"banner": null,
"broadcaster_language": null,
"display_name": "Gem28daz2012",
"game": "Minecraft",
"logo": null,
"mature": null,
"status": "Playing minecraft",
"partner": false,
"url": "http://www.twitch.tv/gem28daz2012",
"video_banner": null,
"_id": 93338000,
"name": "gem28daz2012",
"created_at": "2015-06-12T19:51:58Z",
"updated_at": "2016-01-12T21:16:07Z",
"delay": null,
"followers": 8,
"profile_banner": null,
"profile_banner_background_color": null,
"views": 4,
"language": "en"
}
}],
"_total": 2,
"_links": {
"self": "https://api.twitch.tv/kraken/users/user/follows/channels?direction=DESC&limit=25&offset=0&sortby=created_at",
"next": "https://api.twitch.tv/kraken/users/user/follows/channels?direction=DESC&limit=25&offset=25&sortby=created_at"
}
Upvotes: 0
Views: 435
Reputation: 29926
To parse the http response body, you can use json.loads
. After that you can use a list comprehesion:
display_names = [f['channel']['display_name'] for f in data['follows']]
To put it in some context:
data_str = '''
{
"follows": [{
"created_at": "2016-01-15T22:43:35Z",
"_links": {
"self": "https://api.twitch.tv/kraken/users/user/follows/channels/syndicate"
},
"notifications": false,
"channel": {
"_links": {
"self": "https://api.twitch.tv/kraken/channels/syndicate",
"follows": "https://api.twitch.tv/kraken/channels/syndicate/follows",
"commercial": "https://api.twitch.tv/kraken/channels/syndicate/commercial",
"stream_key": "https://api.twitch.tv/kraken/channels/syndicate/stream_key",
"chat": "https://api.twitch.tv/kraken/chat/syndicate",
"features": "https://api.twitch.tv/kraken/channels/syndicate/features",
"subscriptions": "https://api.twitch.tv/kraken/channels/syndicate/subscriptions",
"editors": "https://api.twitch.tv/kraken/channels/syndicate/editors",
"videos": "https://api.twitch.tv/kraken/channels/syndicate/videos",
"teams": "https://api.twitch.tv/kraken/channels/syndicate/teams"
},
"background": null,
"banner": null,
"broadcaster_language": "en",
"display_name": "Syndicate",
"game": "Counter-Strike: Global Offensive",
"logo": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-profile_image-03bf1d6ad0025f86-300x300.png",
"mature": false,
"status": "Road To MLG! (CS:GO Competiive) ",
"partner": true,
"url": "http://www.twitch.tv/syndicate",
"video_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-channel_offline_image-a15fc19d9828b9c7-640x360.png",
"_id": 16764225,
"name": "syndicate",
"created_at": "2010-10-17T23:19:43Z",
"updated_at": "2016-01-22T22:17:33Z",
"delay": null,
"followers": 2353424,
"profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-profile_banner-5f912a83f7e9bafe-480.png",
"profile_banner_background_color": null,
"views": 38140282,
"language": "en"
}
}, {
"created_at": "2016-01-12T21:01:49Z",
"_links": {
"self": "https://api.twitch.tv/kraken/users/pandaswede/follows/channels/gem28daz2012"
},
"notifications": false,
"channel": {
"_links": {
"self": "http://api.twitch.tv/kraken/channels/gem28daz2012",
"follows": "http://api.twitch.tv/kraken/channels/gem28daz2012/follows",
"commercial": "http://api.twitch.tv/kraken/channels/gem28daz2012/commercial",
"stream_key": "http://api.twitch.tv/kraken/channels/gem28daz2012/stream_key",
"chat": "http://api.twitch.tv/kraken/chat/gem28daz2012",
"features": "http://api.twitch.tv/kraken/channels/gem28daz2012/features",
"subscriptions": "http://api.twitch.tv/kraken/channels/gem28daz2012/subscriptions",
"editors": "http://api.twitch.tv/kraken/channels/gem28daz2012/editors",
"videos": "http://api.twitch.tv/kraken/channels/gem28daz2012/videos",
"teams": "http://api.twitch.tv/kraken/channels/gem28daz2012/teams"
},
"background": null,
"banner": null,
"broadcaster_language": null,
"display_name": "Gem28daz2012",
"game": "Minecraft",
"logo": null,
"mature": null,
"status": "Playing minecraft",
"partner": false,
"url": "http://www.twitch.tv/gem28daz2012",
"video_banner": null,
"_id": 93338000,
"name": "gem28daz2012",
"created_at": "2015-06-12T19:51:58Z",
"updated_at": "2016-01-12T21:16:07Z",
"delay": null,
"followers": 8,
"profile_banner": null,
"profile_banner_background_color": null,
"views": 4,
"language": "en"
}
}],
"_total": 2,
"_links": {
"self": "https://api.twitch.tv/kraken/users/pandaswede/follows/channels?direction=DESC&limit=25&offset=0&sortby=created_at",
"next": "https://api.twitch.tv/kraken/users/pandaswede/follows/channels?direction=DESC&limit=25&offset=25&sortby=created_at"
}
}
'''
import json
data = json.loads(data_str)
display_names = [f['channel']['display_name'] for f in data['follows']]
print(display_names) # ['Syndicate', 'Gem28daz2012']
Upvotes: 3