Created
May 2, 2016 17:43
-
-
Save ricalanis/fd785e702605166835b4f471048fdb6f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import requests\n", | |
"import os" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"page_access_token = os.environ[\"page_access_token\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def base_url(facebook_id,object_type, token, comment_adder = \"\"):\n", | |
" if object_type == \"comments\": comment_adder = \"&fields=comments,id,created_time,message,from\"\n", | |
" url_base = 'https://graph.facebook.com/v2.5/'+facebook_id+'/'+object_type+'/?limit=100&access_token='+ token + comment_adder\n", | |
" print(url_base)\n", | |
" return url_base\n", | |
"\n", | |
"\n", | |
"def facebook_data(facebook_id, object_type, token):\n", | |
" #when object type = \"posts\", gets posts, when \"comments\", gets comments.\n", | |
" url_base = base_url(facebook_id,object_type, token)\n", | |
" next_page, data = next_page_data(url_base,url_base)\n", | |
" posts = []\n", | |
" while next_page is not None:\n", | |
" posts = posts + data\n", | |
" next_page,data = next_page_data(next_page,url_base)\n", | |
" return posts\n", | |
"\n", | |
"\n", | |
"def next_url(r, url_base):\n", | |
" try:\n", | |
" next_cursor = r.json()['paging']['cursors']['after']\n", | |
" next_page = url_base + \"&after=\"+ next_cursor\n", | |
" data = r.json()[\"data\"]\n", | |
" except:\n", | |
" next_page = None\n", | |
" data = None\n", | |
" return next_page,data\n", | |
"\n", | |
"\n", | |
"def next_page_data(pagination_token,url_base):\n", | |
" r = requests.get(pagination_token)\n", | |
" try:\n", | |
" next_page = r.json()[\"paging\"][\"next\"]\n", | |
" data = r.json()[\"data\"]\n", | |
" except:\n", | |
" next_page, data = next_url(r, url_base)\n", | |
" return next_page, data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"post_id_pautado= \"1037553166317439_1052734731465949\"\n", | |
"object_type = \"comments\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"https://graph.facebook.com/v2.5/1037553166317439_1052734731465949/comments/?limit=100&access_token=EAACEdEose0cBAL4RvtXpODYq2E9dnoyF3NuTgVMdXxEoI23soTZBwUhBm9zTtk5GZAXZBdMbECAhETHAfWneTaWybeb1uyF6K1OmegEcixDhgZCxQNQlWPkGBk7mLWC0WTe2yyvzaQVVpdC8cpafaJGn0SSMCpeCTjxpUVMofwZDZD&fields=comments,id,created_time,message,from\n" | |
] | |
} | |
], | |
"source": [ | |
"comments_pautado = facebook_data(post_id_pautado,object_type,page_access_token)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1084" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"len(comments_pautado)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'created_time': '2016-04-19T22:04:46+0000',\n", | |
" 'from': {'id': '820827144629829', 'name': 'Arturo Mendoza'},\n", | |
" 'id': '1052734731465949_1052772968128792',\n", | |
" 'message': 'A mi me da la impresión qué en primer lugar va Morena.'}" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"comments_pautado[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"user_list = [comment[\"from\"][\"id\"] for comment in comments_pautado]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def get_age_range(user_id):\n", | |
" construct_url = 'https://graph.facebook.com/v2.5/'+user_id+'?fields=age_range&access_token=' + page_access_token\n", | |
" r = requests.get(construct_url)\n", | |
" try:\n", | |
" age_range = r.json()[\"age_range\"]\n", | |
" except:\n", | |
" age_range = \"NA\"\n", | |
" return age_range" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"user_age_range = []\n", | |
"for user in user_list:\n", | |
" user_age_range.append(get_age_range(user))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"i = 0\n", | |
"for user_age in user_age_range:\n", | |
" if user_age is \"NA\":\n", | |
" i = i +1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1084" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"i" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.4.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment