Skip to content

Instantly share code, notes, and snippets.

@EgZvor
Created April 7, 2015 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EgZvor/6611c2c05d1aaa397005 to your computer and use it in GitHub Desktop.
Save EgZvor/6611c2c05d1aaa397005 to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import requests\n",
"import ast\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"r = requests.post('https://api.vk.com/method/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('https://api.vk.com/method/friends.get?user_id={0}'.format(friend_list[i]))\n",
" friend_list_1 = ast.literal_eval(r.text)['response']\n",
" r = requests.post('https://api.vk.com/method/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]), ' '.join(names_dict[second]))) for (first, second) in G.edges()]\n",
"# print('\\n'.join(string_list_of_edges[:10]))\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