Seite 1 von 1

Twitch API auslesen / Code sortieren

Verfasst: Samstag 4. Oktober 2014, 00:29
von lucanello
Guten Abend :)

Ich würde gerne die Twitch.tv API auslesen, was auch bisher ganz gut klappt. Allerdings muss ich den Code irgendwie sortieren. Wie funktioniert das?
Das Programm sieht so aus:

Code: Alles auswählen

import urllib2

x = 'https://api.twitch.tv/kraken/channels/' + raw_input('What Channel? ')
print x
url = x
contents = urllib2.urlopen(url)

print contents.read()
Und die Ausgabe ist z.B.:

Code: Alles auswählen

{"mature":true,"abuse_reported":null,"status":"follow twitch.tv/wolskyy","display_name":"Nadeshot","game":"Call of Duty: Ghosts","delay":0,"language":"en","_id":21130533,"name":"nadeshot","created_at":"2011-03-14T19:59:28Z","updated_at":"2014-10-03T22:54:09Z","logo":"http://static-cdn.jtvnw.net/jtv_user_pictures/nadeshot-profile_image-d68adbe2f9afccf9-300x300.png","banner":"http://static-cdn.jtvnw.net/jtv_user_pictures/nadeshot-channel_header_image-9d87030d73d97092-640x125.png","video_banner":"http://static-cdn.jtvnw.net/jtv_user_pictures/nadeshot-channel_offline_image-a0dde14d2291c4bb-640x360.png","background":null,"profile_banner":null,"profile_banner_background_color":null,"partner":true,"url":"http://www.twitch.tv/nadeshot","views":34961868,"followers":358743,"_links":{"self":"https://api.twitch.tv/kraken/channels/nadeshot","follows":"https://api.twitch.tv/kraken/channels/nadeshot/follows","commercial":"https://api.twitch.tv/kraken/channels/nadeshot/commercial","stream_key":"https://api.twitch.tv/kraken/channels/nadeshot/stream_key","chat":"https://api.twitch.tv/kraken/chat/nadeshot","features":"https://api.twitch.tv/kraken/channels/nadeshot/features","subscriptions":"https://api.twitch.tv/kraken/channels/nadeshot/subscriptions","editors":"https://api.twitch.tv/kraken/channels/nadeshot/editors","teams":"https://api.twitch.tv/kraken/channels/nadeshot/teams","videos":"https://api.twitch.tv/kraken/channels/nadeshot/videos"}}
Ich bin mir nicht sicher was für ein Format das ist, ich denke aber JSON.
Mit JSON kenn ich mich null aus und habe mich damit noch nie befasst, kann mir da jemand behilflich sein? :)

Danke für jede Hilfe!

MfG Luca

Re: Twitch API auslesen / Code sortieren

Verfasst: Samstag 4. Oktober 2014, 00:46
von cofi
Nun, JSON hat eine recht kurze Dokumentation und im Grunde sind es nur eine Hand voll Funktionen: https://docs.python.org/2/library/json.html

Code: Alles auswählen

In [1]: import urllib2

In [2]: s = urllib2.urlopen("https://api.twitch.tv/kraken/channels/wolskyy")

In [3]: import json

In [4]: d = json.load(s)

In [5]: d
Out[5]: 
{u'_id': 30911525,
 u'_links': {u'chat': u'https://api.twitch.tv/kraken/chat/wolskyy',
  u'commercial': u'https://api.twitch.tv/kraken/channels/wolskyy/commercial',
  u'editors': u'https://api.twitch.tv/kraken/channels/wolskyy/editors',
  u'features': u'https://api.twitch.tv/kraken/channels/wolskyy/features',
  u'follows': u'https://api.twitch.tv/kraken/channels/wolskyy/follows',
  u'self': u'https://api.twitch.tv/kraken/channels/wolskyy',
  u'stream_key': u'https://api.twitch.tv/kraken/channels/wolskyy/stream_key',
  u'subscriptions': u'https://api.twitch.tv/kraken/channels/wolskyy/subscriptions',
  u'teams': u'https://api.twitch.tv/kraken/channels/wolskyy/teams',
  u'videos': u'https://api.twitch.tv/kraken/channels/wolskyy/videos'},
 u'abuse_reported': None,
 u'background': None,
 u'banner': None,
 u'created_at': u'2012-05-30T06:55:33Z',
 u'delay': 0,
 u'display_name': u'WOLSKYY',
 u'followers': 69,
 u'game': u'Minecraft',
 u'language': u'en',
 u'logo': u'http://static-cdn.jtvnw.net/jtv_user_pictures/wolskyy-profile_image-79330c9540643cf1-300x300.jpeg',
 u'mature': False,
 u'name': u'wolskyy',
 u'partner': False,
 u'profile_banner': None,
 u'profile_banner_background_color': None,
 u'status': u'2v2 Wagers with W0LSKY',
 u'updated_at': u'2014-10-03T22:50:17Z',
 u'url': u'http://www.twitch.tv/wolskyy',
 u'video_banner': None,
 u'views': 2866}
Heraus kommt ein Dictionary und damit sollte man Erfahrung haben.