user2748540
user2748540

Reputation: 209

Python Facebook 'NoneType object error'

I am new to programing and python. I wrote this script below and I am getting a "TypeError: 'NoneType' object is not iterable"

import csv
import json
import urllib
import sys
import time
import re

class FacebookSearch:
    def __init__(self,
        query   = '"https://graph.facebook.com/search.{mode}?{query}&{access_token}'
    ):
        access_token = 'XXXXX|XXXXX'

    def search(self, q, mode='json', **queryargs):
        queryargs['q'] = q
        query = urllib.urlencode(queryargs)


def write_csv(fname, rows, header=None, append=False, **kwargs):
    filemode = 'ab' if append else 'wb'
    with open(fname, filemode) as outf:
        out_csv = csv.writer(outf, **kwargs)
        if header:
            out_csv.writerow(header)
        out_csv.writerows(rows)

def main():
    ts = FacebookSearch()
    response, data = ts.search('appliance', result_type='recent')  #I am getting the error on this line
    js = json.loads(data)



    messages = ([msg['created_time'],  msg['id']] for msg in js.get('data', []))

    write_csv('fb_washerdryer.csv', messages, append=True)

if __name__ == '__main__':
    main()

I am not sure why I am getting the "TypeError: 'NoneType' object is not iterable" on the line where I am defining my search term for the graph API.

Upvotes: 1

Views: 228

Answers (1)

Bleeding Fingers
Bleeding Fingers

Reputation: 7129

Add return query at the end of FacebookSearch's search method. You are actually asking it to unpack None which search is current returning. When a function has no explicitly defined return it defaults to return None.

>>> response, data = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable

Upvotes: 2

Related Questions