Skip to content

Instantly share code, notes, and snippets.

@rushter
Last active February 13, 2017 11:01
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 rushter/8eac13834cc0660a4ed02efcf2e1da11 to your computer and use it in GitHub Desktop.
Save rushter/8eac13834cc0660a4ed02efcf2e1da11 to your computer and use it in GitHub Desktop.
import vk
from datetime import datetime, date, timedelta
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
U_ID = 11111111 # used id
bulk_limit = 50
def date_to_age(age):
return (date.today() - datetime.strptime(age, "%d.%m.%Y").date()) / timedelta(days=365)
def get_age(u_id):
session = vk.Session()
api = vk.API(session)
friends = api.friends.get(user_id=u_id)
bdates = []
for i in range(0, len(friends), bulk_limit):
f_sliced = friends[i:i + bulk_limit]
users = api.users.get(user_ids=f_sliced, fields='bdate')
for u in users:
if 'bdate' in u and u['bdate'].count('.') == 2:
bdates.append(date_to_age(u['bdate']))
return bdates
ages = get_age(U_ID)
print(ages)
print('Mean: %s, std: %s' % (np.mean(ages), np.std(ages)))
print('Median: %s, var: %s' % (np.median(ages), np.var(ages)))
sns.distplot(ages)
# fig, ax = plt.subplots()
# counts, bins, patches = ax.hist(ages, bins=40)
# ax.set_xticks(bins)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment