def get_network(users_ids, as_edgelist=True): if as_edgelist: edges = [] for user in users_ids: try: users_friends = get_friends(user, 'id')['response'] except KeyError: continue else: count = users_friends['count'] for i in range(count): this_friend_id = users_friends['items'][i]['id'] if this_friend_id in users_ids: edges.append((users_ids.index(user), users_ids.index(this_friend_id))) time.sleep(1) return edges else: matrix = [[0] * len(users_ids) for l in range(len(users_ids))] for user in users_ids: users_friends = get_friends(user, 'id') count = users_friends['response']['count'] for i in range(count): this_friend_id = users_friends['response']['items'][i]['id'] if this_friend_id in users_ids: this_index = users_ids.index(user) this_friend_index = users_ids.index(this_friend_id) matrix[this_index][this_friend_index] = 1 matrix[this_friend_index][this_index] = 1 return matrix
def get_network( users_ids: list, as_edgelist=True) -> Union[List[List[int]], List[Tuple[int, int]]]: user_num = 0 if as_edgelist: edge_list = [] for user in users_ids: try: friend_list = get_friends(user)['response'] except KeyError: continue else: for friend in friend_list['items']: if friend['id'] in users_ids: edge_list.append((users_ids.index(user), users_ids.index(friend['id']))) user_num += 1 time.sleep(1) return edge_list else: matrix = [[0] * len(users_ids) for _ in range(len(users_ids))] for user in users_ids: try: friend_list = get_friends(user)['response'] except KeyError: continue else: for friend in friend_list['items']: if friend['id'] in users_ids: matrix[users_ids.index(user)][users_ids.index( friend)] = 1 user_num += 1 time.sleep(1) return matrix
def get_network(all_ids, as_edgelist=True): """ Building a friend graph for an arbitrary list of users """ sort_ids = [] for ids in all_ids: try: id_friends = get_friends(ids, 'first_name')['response']['items'] except KeyError: continue for friend in id_friends: if friend['id'] in all_ids: sort_ids.append(ids) break vertices = list(range(len(sort_ids))) edges = set() for i in range(len(sort_ids)): try: id_friends = get_friends(sort_ids[i], 'first_name')['response']['items'] except KeyError: continue for user in id_friends: if user['id'] in sort_ids: j = sort_ids.index(user['id']) edges.add((i, j)) edges = list(edges) if as_edgelist: return vertices, edges else: matrix = [[0 for _ in vertices] for _ in vertices] for edge in edges: matrix[edge[0]][edge[1]] = 1 return matrix
def get_network(user_id, as_edgelist=True, backoff_factor=0.3): response = get_friends(user_id, 'id')['response']['items'] uid_list = [] network = [] sec_uid_list = [] for l in range(len(response)): uid = response[l]['id'] name = response[l]['first_name'] + ' ' + response[l]['last_name'] uid_list.append((uid, name)) for i in range(len(uid_list)): n = 1 try: sec_friends = get_friends(uid_list[i][0], 'id')['response']['items'] except KeyError: print('Wait...') delay = backoff_factor * (2**n) time.sleep(delay) continue for j in range(len(sec_friends)): sec_uid = sec_friends[j]['id'] for k in range(len(uid_list)): if uid_list[k][0] == sec_uid: network.append((i, k)) for i in range(len(uid_list)): print(f'{i}: {uid_list[i][1]}') return network
def get_network(user_id): response = get_friends(user_id) friends_list = response.get('response').get('items') vertices = [i for i in range(len(friends_list))] edges = get_network(friends_list) surnames = get_friends(user_id, 'last_name')['response']['items'] vertices = [i['last_name'] for i in surnames] g = igraph.Graph(vertex_attrs={ "shape": "circle", "label": vertices, "size": 2 }, edges=edges, directed=False) n = len(vertices) visual_style = { "vertex_label_dist": 5, "vertex_size": 2, "edge_color": "gray", "layout": g.layout_fruchterman_reingold(maxiter=100000, area=n**2, repulserad=n**2) } g.simplify(multiple=True, loops=True) clusters = g.community_multilevel() pal = igraph.drawing.colors.ClusterColoringPalette(len(clusters)) g.vs['color'] = pal.get_many(clusters.membership) igraph.plot(g, "friendNetwork.pdf", **visual_style)
def get_network(user_id): user_friends = get_friends(user_id, '') connections = [] for i, friend in enumerate(user_friends): friends = get_friends(friend, '') time.sleep(0.3) for _, friend in enumerate(friends): for k, another_friend in enumerate(user_friends): if friend == another_friend: connections.append((i, k)) return connections
def get_network(user_id, as_edgelist=True): user_friends = get_friends(user_id, '') links = [] for i, friend in enumerate(user_friends): friends = [] friends = get_friends(friend, '') time.sleep(0.34) for _, friend in enumerate(friends): for k, another_friend in enumerate(user_friends): if friend == another_friend: links.append((i, k)) return links
def get_network(user_id: int, as_edgelist=True) -> list: user_friends = get_friends(user_id, '') links = [] for i, friend in enumerate(user_friends): f_friends = [] try: f_friends = get_friends(friend, '') except BaseException: pass time.sleep(0.34) for j, f_friend in enumerate(f_friends): for k, another_friend in enumerate(user_friends): if f_friend == another_friend: links.append((i, k)) return links
def age_predict(user_id: int) -> Optional[float]: """ Наивный прогноз возраста по возрасту друзей Возраст считается как медиана среди возраста всех друзей пользователя :param user_id: идентификатор пользователя :return: медианный возраст пользователя """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" response = get_friends(user_id, 'bdate') ages = [] friends = [User(**friend) for friend in response] for friend in friends: if friend.bdate is not None: date = friend.bdate.split('.') if len(date) == 3: bday = int(date[0]) bmonth = int(date[1]) age = datetime.now().year - int(date[2]) if (datetime.now().month < bmonth) or ((datetime.now().month == bmonth) and (datetime.now().day < bday)): age -= 1 ages.append(age) if ages != []: return median(ages) else: return None
def age_predict(user_id: int) -> Optional[float]: assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" dates = [] ages = [] data = get_friends(user_id, 'bdate') for i in data['response']['items']: if i.get('bdate'): dates.append(i['bdate']) withyear = [] for elem in dates: if len(elem) in range(8, 11): withyear.append(elem) dates = withyear for elem in dates: a = list(map(int, elem.split('.'))) data = datetime.date(a[2], a[1], a[0]) age = (datetime.date.today() - data).days // 365 ages.append(age) if ages: ages.sort() if len(ages) % 2 == 1: return ages[len(ages) // 2] else: return (ages[len(ages) // 2 - 1] + ages[len(ages) // 2]) / 2 else: return None
def age_predict(user_id: int) -> Optional[float]: """ Наивный прогноз возраста по возрасту друзей Возраст считается как медиана среди возраста всех друзей пользователя :param user_id: идентификатор пользователя :return: медианный возраст пользователя """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" friends = [User(**friend) for friend in get_friends(user_id, 'bdate')] # current_date = dt.datetime.now() # tests fix current_date = dt.datetime.now() - dt.timedelta(days=365) result = [] for friend in friends: birthday = friend.bdate if birthday: # ДР указан try: # ДР полный bd = dt.datetime.strptime(birthday, "%d.%m.%Y") except: continue age = current_date - bd result.append(int(age.days / 365.25)) if result: return median(result)
def age_predict(user_id: int) -> Optional[float]: """ Наивный прогноз возраста по возрасту друзей Возраст считается как медиана среди возраста всех друзей пользователя :param user_id: идентификатор пользователя :return: медианный возраст пользователя """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" friends = get_friends(user_id, 'bdate') if friends is not None: ages = [] for i in range(len(friends)): try: birthday = friends[i]['bdate'] except KeyError: pass else: fulldate = birthday.split('.') if len(fulldate) == 3: day = dt.datetime.now().day month = dt.datetime.now().month year = dt.datetime.now().year if month > int(fulldate[1]) or month == int(fulldate[1]) and day >= int(fulldate[0]): ages.append(year - int(fulldate[2])) else: ages.append(year - int(fulldate[2]) - 1) if len(ages) == 0: return None else: return median(ages) return None
def get_network(users_ids, as_edgelist=True): labels = [0] # чтобы не выдавал ошибку при построении графф, если он несвязан edges = [] id2num = {} for i in range(len(users_ids)): id2num.update({users_ids[i]:i+1}) # создаём координаты labels.append(i) # добавляем их в список for id in users_ids: try: friends = get_friends(id, "sex") # перебираем id друзей друзей edges.append((0, id2num[id])) for fr in friends: # перебираем друга из друзей друзей if fr["id"] in users_ids: # если друг есть в списке users_ids, то создаём ребро edges.append((id2num[id], id2num[fr["id"]])) except: pass plot_graph((labels, edges)) if as_edgelist: return edges else: matrix = [] for k in range(len(users_ids)): matrix.append([]) for j in range(len(users_ids)): matrix[k].append(0) for edge in edges: i, j = edge matrix[i][j] = 1 matrix[j][i] = 1 return matrix
def age_predict(user_id: int) -> Optional[float]: """ Наивный прогноз возраста по возрасту друзей Возраст считается как медиана среди возраста всех друзей пользователя :param user_id: идентификатор пользователя :return: медианный возраст пользователя """ link = get_friends(user_id, 'bdate') assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" arr = [] try: for i in link.json()['response']['items']: try: born = i['bdate'].split('.') if len(born) != 3: continue new = datetime.datetime.today() year = new.year month = new.month day = new.year age = int(year) - int(born[2]) if (int(born[1]) > month) or (born[1] == month and born[0] > day): age -= 1 arr.append(age) except: pass if len(arr): return median(arr) else: return None except: return "Не получилось получить json"
def get_network(users_ids: List, as_edgelist: bool = True) -> List[List]: """ Create user friends network in form of matrix or graph :param users_ids: list with ID's of user's friends :param as_edgelist: result in form of graph edges if True, and in matrix if False :return: user's friends network in form of matrix or in edges list (as_edgeList option) """ graph_edges = [] matrix = [[0 for _ in range(len(users_ids))] for _ in range(len(users_ids))] for l1_friend in range(len(users_ids)): friend_ids = get_friends(users_ids[l1_friend]) for l2_friend in range(l1_friend + 1, len(users_ids)): if users_ids[l2_friend] in friend_ids: if as_edgelist: graph_edges.append((l1_friend, l2_friend)) else: matrix[l1_friend][l2_friend] = 1 matrix[l2_friend][l1_friend] = 1 time.sleep(0.4) progress = ((l1_friend + 1) * 100) // len(users_ids) if progress > ((l1_friend) * 100) // len(users_ids): print(str(progress) + "% completed...") if as_edgelist: return graph_edges else: return matrix
def age_predict(user_id: int) -> Optional[float]: """ Наивный прогноз возраста по возрасту друзей Возраст считается как медиана среди возраста всех друзей пользователя :param user_id: идентификатор пользователя :return: медианный возраст пользователя """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" friends = get_friends(user_id) ages = [] for friend in friends: user = User(**friend) try: bdate = str(user.bdate) bd, bm, by = map(int, bdate.split('.')) today = dt.date.today() ages.append(today.year - by - ((today.month, today.day) < (bm, bd))) except ValueError: pass try: return median(ages) except: pass
def get_network(users, as_edgelist=True): vertices = [user.id for user in users] vertices_names = [user.first_name + ' ' + user.last_name for user in users] edges_map = [[0 for col in range(len(vertices))] for row in range(len(vertices))] edges = [] for user in users: friends = get_friends(user.id, 'bdate') if friends: for friend in friends: try: vertices.index(friend.id) except: pass else: if as_edgelist: edges.append((vertices.index(user.id), vertices.index(friend.id))) else: edges_map[vertices.index(user.id)][vertices.index( friend.id)] = 1 edges_map[vertices.index(friend.id)][vertices.index( user.id)] = 1 for rown, row in enumerate(edges_map): for coln, el in enumerate(row): if el == 1: edges.append((rown, coln)) for edge in edges: if edge[0] == edge[1]: edges.remove(edge) graph = (vertices_names, edges) return graph
def age_predict(user_id: int) -> Optional[float]: """ Наивный прогноз возраста по возрасту друзей Возраст считается как медиана среди возраста всех друзей пользователя :param user_id: идентификатор пользователя :return: медианный возраст пользователя """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" dates = [] ages = [] data = get_friends(user_id, 'bdate') for i in data: if i.get('bdate'): dates.append(i['bdate']) y_dates = [] for elem in dates: if len(elem) in range(8, 11): y_dates.append(elem) dates = y_dates for elem in dates: a = list(map(int, elem.split('.'))) data = datetime.date(a[2], a[1], a[0]) age = (datetime.date.today() - data) / 365.25 ages.append(age.days) if len(ages) > 0: return median(ages) else: return None
def age_predict(user_id: int) -> Optional[float]: """ Наивный прогноз возраста по возрасту друзей Возраст считается как медиана среди возраста всех друзей пользователя :param user_id: идентификатор пользователя :return: медианный возраст пользователя """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" friends = get_friends(user_id, fields='bdate') count_real_friends = 0 sum_of_ages = 0 count = friends['response']['count'] for friend in friends: try: birthday_date_str = friends['response']['items'][i]['bdate'] d, m, y = birthday_date_str.split('.') # all ok, there is birthday date, we can calculate age of # this friends date_today = datetime.date.today() birthday_date = datetime.date(int(y), int(m), int(d)) delta_dates = date_today - birthday_date age = delta_dates.days // 365 sum_of_ages += age count_real_friends += 1 except: pass return sum_of_ages // count_real_friends
def age_predict(user_id: int) -> Optional[float]: """ Наивный прогноз возраста по возрасту друзей Возраст считается как медиана среди возраста всех друзей пользователя :param user_id: идентификатор пользователя :return: медианный возраст пользователя """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" friends = get_friends(user_id, 'bdate') months = [] for i in range(friends['response']['count']): try: date = list( map(int, friends['response']['items'][i]['bdate'].split('.'))) months += [date[1] + date[2] * 12] except: pass date = dt.datetime.now() count_months = date.year * 12 + date.month return (count_months - median(months)) // 12
def age_predict(user_id: int) -> Optional[float]: assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" #получаем список дат рождения dates = [] ages = [] data = get_friends(user_id, 'bdate') for i in data['response']['items']: if i.get('bdate'): dates.append(i['bdate']) #учитываем только даты с годом new = [] for elem in dates: if len(elem) in range(8, 11): new.append(elem) dates = new #считаем возраст друзей for elem in dates: a = list(map(int, elem.split('.'))) data = datetime.date(a[2], a[1], a[0]) age = (datetime.date.today() - data) // 365 ages.append(age.days) #считаем медиану if ages: ages.sort() if len(ages) % 2 == 1: return ages[len(ages) // 2] else: return (ages[len(ages) // 2 - 1] + ages[len(ages) // 2]) / 2 else: return None
def age_predict(user_id): """ >>> age_predict(???) ??? """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" friends = get_friends(user_id, 'bdate') bdates = {} for i in range(friends['response']['count']): try: birthday = [] bdate = friends['response']['items'][i]['bdate'].split('.') for k in range(3): birthday.append(bdate[k]) bdates[i] = birthday except: pass all_age = 0 count = len(bdates.keys()) for i in bdates.keys(): date = bdates[i] all_age += get_age(date) avg_age = all_age // count return avg_age
def plot_graph(graph: List[Tuple]): """ Draw the graph :param graph: graph edges list :return: nothing """ friends = get_friends(config.VK_CONFIG['my_id'], 'bdate') vertexes = [friend['last_name'] for friend in friends] # graph vertexes edges = graph # graph edges # Create graph g = Graph(vertex_attrs={"label": vertexes, "shape": "circle", "size": 10}, edges=edges, directed=False) # Graph appearance n = len(vertexes) visual_style = { "vertex_size": 10, "vertex_color": "blue", "edge_color": "gray", "bbox": (1500, 1000), "layout": g.layout_fruchterman_reingold( maxiter=1000, area=n ** 3, repulserad=n ** 3) } # Delete loops and repeating edges g.simplify(multiple=True, loops=True) # Separate vertices in groups by interconnection g.community_multilevel() # Draw graph plot(g, **visual_style)
def plot_graph(user_id): friends = get_friends(user_id, 'bdate') edges = get_network(user_id) vertices = [(i['first_name'] + ' ' + i['last_name']) for i in friends] graph = Graph(vertex_attrs={"label": vertices}, edges=edges, directed=False) N = len(vertices) visual_style = { "vertex_size": 20, "edge_color": "gray", "layout": graf.layout_fruchterman_reingold(maxiter=100000, area=N**2, repulserad=N**2) } graph.simplify(multiple=True, loops=True) communities = graph.community_edge_betweenness(directed=False) clusters = communities.as_clustering() pal = igraph.drawing.colors.ClusterColoringPalette(len(clusters)) graph.vs['color'] = pal.get_many(clusters.membership) igraph.plot(graph, **visual_style)
def get_network(users_ids: list, as_edgelist: bool = True) -> Any: # Создание вершин и ребер vertices = [] matrix = [[0] * len(users_ids) for _ in range(len(users_ids))] edges = [] n = 0 m = 0 for user in users_ids: n += 1 print(n, names[m]) m += 1 friends = get_friends(user, "") if 'error' in friends: print("deleted page") else: for friend in friends: if friend in users_ids: edges.append( (users_ids.index(user), users_ids.index(friend))) matrix[users_ids.index(user)][users_ids.index(friend)] = 1 friends = [] if as_edgelist: return edges else: return matrix
def get_friend_ids(user_id): for i in range(10): try: ids = [int(u) for u in get_friends(user_id)['items']] return ids except Exception as e: sleep(0.1 * i)
def age_predict(user_id: int) -> Optional[float]: """ Наивный прогноз возраста по возрасту друзей Возраст считается как медиана среди возраста всех друзей пользователя :param user_id: идентификатор пользователя :return: медианный возраст пользователя """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" friends = get_friends(user_id, "bdate") friends = [User(**friend) for friend in friends] ages = [] for friend in friends: if friend.bdate is not None: data = friend.bdate.split(".") if len(data) == 3: age = datetime.now().year - int(data[2]) born_month = int(data[1]) born_day = int(data[0]) if (datetime.now().month < born_month) or (datetime.now().month == born_month and datetime.now().day < born_day): ages.append(age - 1) else: ages.append(age) if ages: ages.sort() if len(ages) % 2 == 1: return ages[len(ages) // 2] else: return (ages[len(ages) // 2 - 1] + ages[len(ages) // 2]) / 2 else: return None
def age_predict(user_id: int) -> Optional[float]: """ Наивный прогноз возраста по возрасту друзей Возраст считается как медиана среди возраста всех друзей пользователя :param user_id: идентификатор пользователя :return: медианный возраст пользователя """ assert isinstance(user_id, int), "user_id must be positive integer" assert user_id > 0, "user_id must be positive integer" cur_date = dt.datetime.now() fr_list = get_friends(user_id, 'bdate') age_list = [] for fr in fr_list: user = User(**fr) if user.bdate: try: bd = dt.datetime.strptime(user.bdate, '%d.%m.%Y') except: continue age = cur_date.year - bd.year if cur_date.month < bd.month: age -= 1 elif (cur_date.month == bd.month) and (cur_date.day <= bd.day): age -= 1 age_list.append(age) if age_list: return float(median(age_list))
def plot_graph(user_id): surnames = get_friends(user_id, 'last_name') vertices = [i['last_name'] for i in surnames] edges = get_network(user_id, True) draf = igraph.Graph(vertex_attrs={ "shape": "circle", "label": vertices, "size": 10 }, edges=edges, directed=False) n = len(vertices) visual_style = { "vertex_size": 20, "edge_color": "gray", "layout": draf.layout_fruchterman_reingold(maxiter=100000, area=n**2, repulserad=n**2) } draf.simplify(multiple=True, loops=True) clusters = draf.community_multilevel() pal = igraph.drawing.colors.ClusterColoringPalette(len(clusters)) draf.vs['color'] = pal.get_many(clusters.membership) igraph.plot(draf, **visual_style)
def plot_graph(graph): friends = get_friends(user_id) vertices = [i for i in range(len(friends))] # количество вершин edges = graph # ребра графа # Создание графа g = Graph(vertex_attrs={"label": vertices, "shape": "circle", "size": 10}, edges=edges, directed=False) # Задаем стиль отображения графа n = len(vertices) visual_style = { "vertex_size": 20, "edge_color": "gray", "layout": g.layout_fruchterman_reingold( maxiter=1000, area=n ** 3, repulserad=n ** 3) } # Удаляем петли и повторяющиеся ребра g.simplify(multiple=True, loops=True) # Разделяем вершины на группы по взаимосвязям clusters = g.community_multilevel() print(clusters) # Раскрашиваем разные группы вершин в разные цвета pal = igraph.drawing.colors.ClusterColoringPalette(len(clusters)) g.vs['color'] = pal.get_many(clusters.membership) # Отрисовываем граф plot(g, **visual_style)