How to analyze FQL responses to Facebook pages by category and / or location? (Python)

Long listener, first time caller.

I get a good result from my Open Open Graph API request:

fbtest = graph.request("/fql", {"q": "SELECT name, page_id, categories, location FROM page WHERE page_id IN (SELECT page_id FROM page_fan WHERE uid=me())"}) 

And although I took a great Zed course on Learning Python Hard Way , I am still green and need help on these fronts:

  • I would like to ONLY request pages that correspond to certain categories, for example, “Local business”, but since “categories” is a list (and it is not indexed in the FB table), I have a tough time figuring this out; I guess this is not possible.

  • So, I get a complete explosion of ALL pages that a person likes, and I need to figure it out later. Here I am having problems:

How can I manipulate the results (filtering by category and / or location, which are both lists) and send them in a readable format to the fbtest.html file?

I'm currently just displaying fbtest output in HTML;

 self.render("test.html", fbtest=fbtest) 

which is pretty ugly:

fbtest: {u'data ': [{u'page_id': 8495417058L, u'name ': u'Mat Zo', u'categories': [], u'location ': {u'street': u '' , u'zip ': u' '}}, {u'page_id': 9980651805L, u'name ': u'deadmau5', u'categories': [], u'location ': {u'street': u '', u'zip ': u' '}}, {u'page_id': 6209079710L, u'name ': u'Ultra Records', u'categories ': [], u'location': {u'street ': u' ', u'zip': u ''}}, {u'page_id ': 12609724042L, u'name': u'Oceanlab ', u'categories': [], u'location': {u 'street': u '', u'zip ': u' '}} etc.

And as soon as I try to manipulate the list, I can send a single result (for example, page name = "thesocialbusiness"), but not the number of results I'm looking for. My vision is to have a beautiful snapshot of thumbnail pages that are classified and sorted by location.

Thank you and happy holidays,

-James

+4
source share
1 answer

If I understand correctly, you just want to filter the final result here? If you haven't done it yet, go ahead and convert the json string to a python object

 import json resp = json.loads(fql_resp) data = resp['data'] 

loads indicates the load line you will receive from the service. From here you have several options. One option is to use the built-in filter if you know in front of you which categories you want. Take for example the following dataset

 objs = [{'_id': 1, 'categories': ['a', 'b', 'c']}, {'_id': 2, 'categories': ['b']}, {'_id': 3, 'categories': ['c']}] 

You can filter results that contain only category b , for example

 def f(obj): return 'b' in obj['categories'] filtered_objs = filter(f, objs) print(filtered_objs) # outputs: # [{'_id': 1, 'categories': ['a', 'b', 'c']}, # {'_id': 2, 'categories': ['b']}] 

If you want the reuse function to filter for different categories, you can do something like

 def filter_objs(objs, category): result = [] for obj in objs: if category in obj['categories']: result.append(category) return result filtered_objs = filter_objs(objs, 'b') 

And finally, you could just use list comprehension, whether built in as needed or in the filter_objs function.

 filtered_objs = [obj for obj in objs if 'b' in obj['categories']] 

Thus, there are several ways to filter the result, but the first step is to use json.loads

+1
source

Source: https://habr.com/ru/post/1387822/


All Articles