0

I'm getting the following error when executing the following script:

Error Type: <type 'exceptions.TypeError'>
Error Contents: 'NoneType' object is not iterable
Traceback (most recent call last):
File "addon.py", line 75, in <module>
plugin.run()
File "xbmcswift2/plugin.py", line 332, in run
items = self._dispatch(self.request.path)
File "/plugin.py", line 306, in _dispatch
listitems = view_func(**items)
File "/addon.py", line 42, in all_episodes
items = thisiscriminal.compile_playable_podcast(playable_podcast)
File "/lib/thisiscriminal.py", line 121, in compile_playable_podcast
for podcast in playable_podcast:
TypeError: 'NoneType' object is not iterable

The code in question is as follows, any advice would be greatly appreciated as I have no idea what I'm doing wrong:

def get_playable_podcast(soup):
    """
    @param: parsed html page            
    """
    r    = urllib.urlopen('https://thisiscriminal.com/wp-json/criminal/v1/episodes?posts=10000&page=1')
    data = json.loads(r.read().decode('utf-8'))

    for post in data['posts']:

          print post['title']
          print post['episodeNumber']
          print post['audioSource']
          print post['image']['medium']

          subjects = []

          item = {
                'title': post['title'],
                'audioSource': post['audioSource'],
                'episodeNumber': post['episodeNumber'],
                'medium': post['image']['medium']
          }

          subjects.append(item)

          print subjects

def compile_playable_podcast(playable_podcast):
    """
    @para: list containing dict of key/values pairs for playable podcasts
    """
    items = []

    for podcast in playable_podcast:
        items.append({
            post['title']: podcast['title']['episodeNumber'],
            post['audioSource']: podcast['audioSource'],
            post['image']['medium']: podcast['medium'],
            'is_playable': True,})

    return items
Nelewout
  • 6,281
  • 3
  • 29
  • 39
leopheard
  • 101
  • 7
  • It looks like `playable_podcast` is not of the type you expect, but you're not showing the code that creates this data. – Worthwelle Jul 16 '19 at 16:33
  • Please read this guide on how to produce a [minimum reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – vekerdyb Jul 20 '19 at 10:19
  • Possible duplicate of [TypeError: 'NoneType' object is not iterable in Python](https://stackoverflow.com/questions/3887381/typeerror-nonetype-object-is-not-iterable-in-python) – Caramiriel Jul 20 '19 at 10:34

1 Answers1

1

I assume your script does something alike to the following,

podcast = get_playable_podcast(soup)
compiled = compile_playable_podcast(podcast)

The problem is that get_playable_podcast has no return statement. In such a case, Python defaults to returning None - which you then pass into compile_playable_podcast. Since None is not iterable, compile_playable_podcast rightfully raises a TypeError.

Now, the solution is of course to return the podcast list you're building in get_playable_podcast, like so,

def get_playable_podcast(soup):
    """
    @param: parsed html page            
    """
    r    = urllib.urlopen('https://thisiscriminal.com/wp-json/criminal/v1/episodes?posts=10000&page=1')
    data = json.loads(r.read().decode('utf-8'))

    subjects = []

    for post in data['posts']:

          print post['title']
          print post['episodeNumber']
          print post['audioSource']
          print post['image']['medium']

          item = {
                'title': post['title'],
                'audioSource': post['audioSource'],
                'episodeNumber': post['episodeNumber'],
                'medium': post['image']['medium']
          }

          subjects.append(item)

          print subjects

    return subjects

Beside this, it may be worthwhile to carefully check your script for unused parameters and/or duplicate code.

Nelewout
  • 6,281
  • 3
  • 29
  • 39