debrouwere / facebook-insights Goto Github PK
View Code? Open in Web Editor NEWPython wrapper and command-line utility to interact with Insights data in the Facebook Graph API.
License: ISC License
Python wrapper and command-line utility to interact with Insights data in the Facebook Graph API.
License: ISC License
It's possible to specify arbitrary insight metrics for a post, and it seems to work fine. However, it doesn't return normal metrics on a post.
As far as I can see, the raw response when returning posts only gives id
, message
and created_time
leaving fields such as likes
empty.
Perhaps something has changed in the API causing this?
page.posts.latest(10).get()
page.insights.daily(['page_impressions', 'page_fan_adds']).range(months=1).get()
while the commands run without error but doesn't return any value. It gives empty list.
Please help!!!!
Hi!
I'm trying authenticate with token. But it gives me KeyError('username',) error. I'm pretty sure about token himself, but i couldnt find a solution.
any suggestions?
Thanks
Hi,
I am trying to authenticate by sending the client id and secret. the authentication browser opens and shows successful and then this error occurs
>>> # this will launch a web browser to authenticate ... pages = fi.authenticate( ... client_id='1113595855373558', ... client_secret='91f0254c1534966e8cf9ddecc1c36d1b', ... ) Authentication flow completed. You may close the browser tab. Traceback (most recent call last): File "<stdin>", line 4, in <module> File "C:\Users\nabeelah.bijapur\Documents\Anaconda3\lib\site-packages\facebookinsights\__init__.py", line 35, in authenticate return [graph.Page(token) for token in tokens] File "C:\Users\nabeelah.bijapur\Documents\Anaconda3\lib\site-packages\facebookinsights\__init__.py", line 35, in <listcomp> return [graph.Page(token) for token in tokens] File "C:\Users\nabeelah.bijapur\Documents\Anaconda3\lib\site-packages\facebookinsights\graph.py", line 366, in __init__ self.username = data['username'] KeyError: 'username'
The same thing occurs when I pass the access_token.
Can you point out the issue.
Regards,
getting the following error:
File "/usr/local/lib/python2.7/dist-packages/facebookinsights/graph.py", line 366, in init
self.username = data['username']
maybe use 'name' instead?
Using python3.4 a very simple page insights query explodes:
fb_page_conn.insights.daily(['page_impressions', 'page_fan_adds']).get()
Traceback (most recent call last):
File "bin/run_fb.py", line 52, in <module>
function(fb_mysql_conn, sid_list, customer_type)
File "bin/run_fb.py", line 22, in insights_api_fetch_all_helper
fb_insights_mysql.get_page_info(fb_mysql_conn, stream_id, customer_class)
File "/Users/tcarpenter/Development/Eclipse/research-pipeline/sumall_research/integration_wrappers/fb_insights_mysql.py", line 147, in get_page_info
print(t.get())
File "/Users/tcarpenter/Development/Eclipse/research-pipeline/rpvenv/lib/python3.4/site-packages/facebookinsights/graph.py", line 208, in get
results = self.get_rows()
File "/Users/tcarpenter/Development/Eclipse/research-pipeline/rpvenv/lib/python3.4/site-packages/facebookinsights/graph.py", line 182, in get_rows
results = self.get_raw()
File "/Users/tcarpenter/Development/Eclipse/research-pipeline/rpvenv/lib/python3.4/site-packages/facebookinsights/graph.py", line 166, in get_raw
metrics, **self.params)
File "/Users/tcarpenter/Development/Eclipse/research-pipeline/rpvenv/lib/python3.4/site-packages/facebookinsights/utils/api.py", line 58, in all
url = self._resolve_endpoint(segments + [relative_url], params)
File "/Users/tcarpenter/Development/Eclipse/research-pipeline/rpvenv/lib/python3.4/site-packages/facebookinsights/utils/api.py", line 32, in _resolve_endpoint
for key in options.keys():
RuntimeError: dictionary changed size during iteration
I traced it to the line and the code reads:
def _resolve_endpoint(self, endpoint, options={}):
endpoint = self._segmentize_endpoint(endpoint)
url = "/".join(self.base + endpoint)
# remove facepy options, retain everything
# that needs to end up in the querystring
blacklist = ['path', 'page', 'retry', 'data', 'method', 'relative_url']
for key in options.keys():
if key in blacklist:
del options[key]
Googling the error I found that deleting keys while iterating a dictionary fails:
http://www.quora.com/Working-in-Python-how-can-I-delete-items-while-iterating-over-a-dictionary
After fixing that, I realized there was another error:
File "bin/run_fb.py", line 52, in <module>
function(fb_mysql_conn, sid_list, customer_type)
File "bin/run_fb.py", line 22, in insights_api_fetch_all_helper
fb_insights_mysql.get_page_info(fb_mysql_conn, stream_id, customer_class)
File "/Users/tcarpenter/Development/Eclipse/research-pipeline/sumall_research/integration_wrappers/fb_insights_mysql.py", line 147, in get_page_info
print(t.get())
File "/Users/tcarpenter/Development/Eclipse/research-pipeline/rpvenv/lib/python3.4/site-packages/facebookinsights/graph.py", line 208, in get
results = self.get_rows()
File "/Users/tcarpenter/Development/Eclipse/research-pipeline/rpvenv/lib/python3.4/site-packages/facebookinsights/graph.py", line 182, in get_rows
results = self.get_raw()
File "/Users/tcarpenter/Development/Eclipse/research-pipeline/rpvenv/lib/python3.4/site-packages/facebookinsights/graph.py", line 166, in get_raw
metrics, **self.params)
File "/Users/tcarpenter/Development/Eclipse/research-pipeline/rpvenv/lib/python3.4/site-packages/facebookinsights/utils/api.py", line 58, in all
url = self._resolve_endpoint(segments + [relative_url], params)
File "/Users/tcarpenter/Development/Eclipse/research-pipeline/rpvenv/lib/python3.4/site-packages/facebookinsights/utils/api.py", line 37, in _resolve_endpoint
qs = urllib.urlencode(options)
AttributeError: 'module' object has no attribute 'urlencode'
After again googling, I found that this code is not python3 compatible:
http://stackoverflow.com/questions/28906859/module-has-no-attribute-urlencode
I have fixed both errors and have pushed a PR.
When trying to authenticate with
pages = fi.authenticate(
client_id='',
client_secret='',
)
I get the following error
raise KeyError(PROCESS_TOKEN_ERROR.format(key=bad_key, raw=r.content))
KeyError: 'Decoder failed to handle access_token with data as returned by provider. A different decoder may be needed. Provider returned: {"access_token":"xxx","token_type":"bearer","expires_in":5181147}'
I'm not able to figure out how to fix it.
When doing e.g. page.insights.lifetime('page_fans').get()
, the Graph API doesn't return a single lifetime metric but instead a range of older values. So it looks like behind the scenes, this metric isn't really a lifetime total as documented. Instead, page.insights.lifetime('page_fans').range(days=1).get()
returns the proper latest page fans count.
Wondering how/if we should deal with this โ is this something this module should wrap/correct/handle, or is it weird-but-not-wrong behavior that we should not try to hide from users?
Play around more with the raw API to see when/where this happens.
On metric page_fans_gender_age query in graph.py line 198 (value = row['value']) there is crash that started recently.
I added printouts of the rows so you can see the one that causes the crash.
I have no idea why the api started returning this line out of the blue?!
Test Name: test_get_bulk
Test Outcome: Passed
Result StandardOutput:
{u'end_time': u'2017-04-15T07:00:00+0000', u'value': {u'U.65+': 329, u'M.25-34': 37468, u'F.25-34': 66894, u'U.13-17': 33, u'M.55-64': 30880, u'M.45-54': 41544, u'U.25-34': 416, u'F.35-44': 96250, u'M.35-44': 38188, u'F.55-64': 93154, u'F.18-24': 47329, u'M.18-24': 21841, u'U.18-24': 294, u'F.65+': 52786, u'M.65+': 16236, u'U.45-54': 590, u'F.45-54': 123482, u'U.55-64': 465, u'M.13-17': 3595, u'F.13-17': 6769, u'U.35-44': 509}}
{u'end_time': u'2017-04-16T07:00:00+0000', u'value': {u'U.65+': 329, u'M.25-34': 37472, u'F.25-34': 66890, u'U.13-17': 33, u'M.55-64': 30876, u'M.45-54': 41548, u'U.25-34': 415, u'F.35-44': 96250, u'M.35-44': 38192, u'F.55-64': 93171, u'F.18-24': 47327, u'M.18-24': 21837, u'U.18-24': 294, u'F.65+': 52799, u'M.65+': 16245, u'U.45-54': 590, u'F.45-54': 123470, u'U.55-64': 465, u'M.13-17': 3588, u'F.13-17': 6762, u'U.35-44': 509}}
{u'end_time': u'2017-04-17T07:00:00+0000', u'value': {u'U.65+': 329, u'M.25-34': 37477, u'F.25-34': 66885, u'U.13-17': 33, u'M.55-64': 30878, u'M.45-54': 41547, u'U.25-34': 414, u'F.35-44': 96226, u'M.35-44': 38197, u'F.55-64': 93189, u'F.18-24': 47318, u'M.18-24': 21843, u'U.18-24': 294, u'F.65+': 52811, u'M.65+': 16255, u'U.45-54': 589, u'F.45-54': 123466, u'U.55-64': 466, u'M.13-17': 3580, u'F.13-17': 6748, u'U.35-44': 509}}
[Row(end_time=datetime.datetime(2017, 4, 16, 7, 0, tzinfo=tzutc()), page_fans_gender_age={u'U.65+': 329, u'M.25-34': 37472, u'F.25-34': 66890, u'U.13-17': 33, u'M.55-64': 30876, u'M.45-54': 41548, u'U.25-34': 415, u'F.35-44': 96250, u'M.35-44': 38192, u'F.55-64': 93171, u'F.18-24': 47327, u'M.18-24': 21837, u'U.18-24': 294, u'F.65+': 52799, u'M.65+': 16245, u'U.45-54': 590, u'F.45-54': 123470, u'U.55-64': 465, u'M.13-17': 3588, u'F.13-17': 6762, u'U.35-44': 509})]
KeyError Traceback (most recent call last)
in ()
----> 1 latest = page.posts.latest(20).get()
C:\ProgramData\Anaconda2\lib\site-packages\facebookinsights-0.3.4-py2.7.egg\facebookinsights\graph.pyc in get(self)
81 for page in pages:
82 for post in page['data']:
---> 83 post = Post(self.edge, post)
84
85 # For date ranges, we can't rely on pagination
C:\ProgramData\Anaconda2\lib\site-packages\facebookinsights-0.3.4-py2.7.egg\facebookinsights\graph.pyc in init(self, page, raw)
300 self.graph = page.graph.partial(raw['id'])
301 self.id = raw['id']
--> 302 self.type = raw['type']
303 self.created_time = utils.date.parse(raw['created_time'])
304 self.updated_time = utils.date.parse(raw['updated_time'])
KeyError: 'type'
Any ideas? Did something change with in the Graph API
Hi, I ran into the following error when executing:
page.insights.daily(['page_impressions', 'page_fan_adds']).range(months=1).get()
...
File ".../site-packages/facebookinsights/utils/api.py", line 32, in _resolve_endpoint
qs = url.encode({key: value for key, value in options.items() if key not in blacklist})
TypeError: encode() argument 1 must be string, not dict`
I resolved this by adding in import urllib
at the top of the api.py file and changing that line to urllib.urlencode(...).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.