Created
April 7, 2015 18:39
-
-
Save EgZvor/9ca4ee3165dea3b8aec4 to your computer and use it in GitHub Desktop.
This file contains 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": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import requests\n", | |
"import ast\n", | |
"import networkx as nx\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"\n", | |
"vk_url = 'https://api.vk.com/method/'\n", | |
"r = requests.post(vk_url + 'friends.get?user_id=28918137')\n", | |
"friend_list = ast.literal_eval(r.text)['response']\n", | |
"list_of_edges = []\n", | |
"names_dict = {}\n", | |
"names_dict[28918137] = ('Егор', 'Зворыкин')\n", | |
"for i in range(0, len(friend_list)):\n", | |
" r = requests.post(vk_url + 'friends.get?user_id={0}'.format(friend_list[i]))\n", | |
" friend_list_1 = ast.literal_eval(r.text)['response']\n", | |
" r = requests.post(vk_url + 'users.get?user_id={0}'.format(friend_list[i]))\n", | |
" user_dict = ast.literal_eval(r.text)['response'][0]\n", | |
" names_dict[friend_list[i]] = (user_dict['first_name'], user_dict['last_name'])\n", | |
" for j in range(0, len(friend_list)):\n", | |
" if friend_list[j] in friend_list_1 and i != j:\n", | |
" list_of_edges.append((friend_list[i], friend_list[j]))\n", | |
"for i in range(0, len(friend_list)):\n", | |
" list_of_edges.append((28918137, friend_list[i]))\n", | |
"G = nx.Graph()\n", | |
"G.add_nodes_from(friend_list)\n", | |
"G.add_node(28918137)\n", | |
"for e in list_of_edges:\n", | |
" G.add_edge(*e)\n", | |
"\n", | |
"# pos = nx.spring_layout(G)\n", | |
"# nx.draw_networkx_nodes(G, pos)\n", | |
"# nx.draw_networkx_edges(G, pos)\n", | |
"# plt.show()\n", | |
"string_list_of_edges = [('\"{0}\",\"{1}\"'.format(' '.join(names_dict[first]),\n", | |
" ' '.join(names_dict[second])))\n", | |
" for (first, second) in G.edges()]\n", | |
"f = open('D:\\Graph_with_names.csv', 'w')\n", | |
"f.write('\\n')\n", | |
"f.write('\\n'.join(string_list_of_edges))\n", | |
"f.close()" | |
] | |
} | |
], | |
"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