Question

Currently i am trying to create a python script that will run my sickbeard api code by requesting the special url and grabbing the information. It is outputting the below code which is correct:

{
    "data": {
        "missed": [
            {
                "airdate": "2014-03-27", 
                "airs": "Friday 02:05", 
                "ep_name": "Episode 24", 
                "ep_plot": "", 
                "episode": 24, 
                "network": "MBS", 
                "paused": 0, 
                "quality": "SD", 
                "season": 1, 
                "show_name": "Kill la Kill", 
                "show_status": "Continuing", 
                "tvdbid": 272074, 
                "weekday": 4
            }, 
            {
                "airdate": "2014-03-27", 
                "airs": "Thursday 10:00 PM", 
                "ep_name": "Tyga", 
                "ep_plot": "", 
                "episode": 18, 
                "network": "MTV", 
                "paused": 0, 
                "quality": "SD", 
                "season": 4, 
                "show_name": "Ridiculousness", 
                "show_status": "Continuing", 
                "tvdbid": 250793, 
                "weekday": 4
            }, 
            {
                "airdate": "2014-03-27", 
                "airs": "Thursday 10:00 PM", 
                "ep_name": "Guy Fieri", 
                "ep_plot": "", 
                "episode": 19, 
                "network": "MTV", 
                "paused": 0, 
                "quality": "SD", 
                "season": 4, 
                "show_name": "Ridiculousness", 
                "show_status": "Continuing", 
                "tvdbid": 250793, 
                "weekday": 4
            }
        ]
    }, 
    "message": "", 
    "result": "success"
}

Now i want it to only display the line with the keyword "show_name". for example i want it to just display "kill la kill", ridiculousness and ridiculousness. I will be using this so if i say the command "what shows are on today" it will run the python script and output the shows that are on today. Whenever i run the code it just runs and displays nothing.

from urllib2 import Request, urlopen, URLError

request = Request('http://192.168.1.***:8081/api/*****/?cmd=future&sort=date&type=missed')

try:
    response = urlopen(request)
    tv_shows = response.read()
    for single_line in tv_shows:
        if 'show_name' in single_line:
            print single_line

except URLError, e:
    print ('Error') 

Im very new to python programming so any help is much appreciated.

Was it helpful?

Solution

A few points. First, to work with that data, you need to convert it to dictionary (it is much easier to work with dictionary than with strings) :

import json

...

tv_shows = json.loads(response.read())

Second, to access the list you are interested in, use tv_shows['data']['missed'].

So, you code may look like:

import json
from urllib2 import Request, urlopen, URLError

request = Request('http://192.168.1.***:8081/api/*****/?cmd=future&sort=date&type=missed')

try:
    response = urlopen(request)
    tv_shows = json.loads(response.read())
    for show_data in tv_shows['data']['missed']:
        if 'show_name' in show_data:
            print show_data['show_name']

except URLError, e:
    print ('Error')

This may be useful to get you aknowledged with dictionaries - http://www.tutorialspoint.com/python/python_dictionary.htm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top