Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from collections import Counter
- import requests
- token = 'f94f52d6ef63a7f08b899641385b687a140e0a09ccb56951b552938c31f9944e00b9a194f3b53e29cd21b'
- #Катя
- user = '9951179'
- #Козлов
- #user = '672736'
- depth = 1
- k_like = 5
- k_post = 5
- k_repost = 5
- k_comment = 5
- k_tag = 5
- session = requests.session()
- def append_to_query(query_url, **kwargs):
- result = query_url
- for (key, value) in kwargs.items():
- result += '&%s=%s' % (key, value)
- return result
- def create_url(query, **kwargs):
- url = 'https://api.vk.com/method/%s.json?' % query
- url = append_to_query(url, **kwargs)
- return url
- def get_response(url, **kwargs):
- query_url = append_to_query(url, **kwargs)
- return session.get(query_url).json().get('response', [])
- def get_friends(user_id, get_all):
- friends_url = create_url('friends.get', user_id=user_id, order='hints', access_token=token)
- if not get_all:
- friends_url = append_to_query(friends_url, count=15)
- friends = get_response(friends_url)
- return dict(((friend_id, 0) for friend_id in friends))
- def get_likes(url, item_id, likers_ids):
- likers = get_response(url, item_id=item_id)
- if likers:
- likers_ids.update(likers['users'])
- def get_comments(url, item, item_id, commentators_ids, friends_ids):
- comments_url = create_url(url, item=item_id)
- if item == 'photo_id':
- comments_url = append_to_query(comments_url, access_token=token)
- comments = get_response(comments_url)
- commentators_ids.update((comment['from_id'] for comment in comments[1:] if comment['from_id'] in friends_ids))
- for comment in comments[1:]:
- if comment['likes']['count']:
- get_likes(create_url('likes.getList', owner_id=user_id, type='comment', count=100, friends_only=1), comment['cid'], likers_ids)
- def analyse_wall(user_id, likers_ids, commentators_ids, posters_ids, reposters_ids, friends_ids):
- #get posts from user's wall and some info about likes and comments there
- posts = get_response(create_url('wall.get'), owner_id=user_id, count=100)
- likes_url = create_url('likes.getList', owner_id=user_id, type='post', count=100, friends_only=1)
- reposts_url = create_url('wall.getReposts', owner_id=user_id, count=1000)
- comments_url = create_url('wall.getComments', owner_id=user_id, count=100, need_likes=1)
- for post in posts[1:]:
- from_id = post['from_id']
- if from_id in friends_ids:
- posters_ids.update([from_id])
- if from_id == user_id and post['likes']['count']:
- get_likes(likes_url, post['id'], likers_ids)
- if from_id == user_id and post['comments']['count']:
- get_comments('post_id', post['id'], commentators_ids, friends_ids)
- #analyse reposts of our user's posts
- if post['reposts']['count'] and from_id == int(user_id):
- reposts = get_response(reposts_url, post_id=post['id'])
- if reposts:
- reposters_ids.update((repost['from_id'] for repost in reposts['items'] if repost['from_id'] in friends_ids))
- #move to the photos, start with getting all the albums and all the photos in them
- def analyse_photos(user_id, likers_ids, commentators_ids, tagged_ids, friends_ids):
- albums = get_response(create_url('photos.getAlbums'), owner_id=user_id, need_system=1)
- photos_url = create_url('photos.get', owner_id=user_id, count=1000)
- likes_url = create_url('likes.getList', type='photo', owner_id=user_id, count=100, friends_only=1)
- comments_url = create_url('photos.getComments', owner_id=user_id, count=100, need_likes=1)
- tags_url = create_url('photos.getTags', owner_id=user_id, access_token=token)
- for album in albums:
- photos = get_response(photos_url, album_id=album['aid'])
- for photo in photos:
- get_likes(likes_url, photo['pid'], likers_ids)
- get_comments(comments_url, 'photo_id', photo['pid'], commentators_ids, friends_ids)
- #...and the tags
- tags = get_response(tags_url, photo_id=photo['pid'])
- tagged_ids.update((tag['user_id'] for tag in tags[1:] if tag['user_id'] in friends_ids))
- def get_social_activity(user_id):
- time_between_posts = 0
- last_post = 0
- cnt = 0
- posts = get_response(create_url('wall.get'), owner_id=user_id, count=100, filter='owner')
- if posts:
- users_ids[int(user_id)] += posts[0]*k_post
- for post in posts[1:]:
- if (last_post):
- time_between_posts += abs(last_post - post['date'])
- last_post = post['date']
- cnt += 1
- if (time_between_posts):
- users_ids[int(user_id)] += round(cnt / time_between_posts)
- def get_k(friends):
- for f in friends:
- if f in likers_ids:
- friends[f] += k_like*likers_ids[f]
- if f in commentators_ids:
- friends[f] += k_comment*commentators_ids[f]
- if f in posters_ids:
- friends[f] += k_post*posters_ids[f]
- if f in reposters_ids:
- friends[f] += k_repost*reposters_ids[f]
- if f in tagged_ids:
- friends[f] += k_tag*tagged_ids[f]
- return friends
- def find_more_edges(user_id, users_ids):
- mutual_friends_ids = {}
- for some_id in users_ids:
- mutual_friends = get_response(create_url('friends.getMutual'), source_id=user_id, target_uid=some_id, access_token=token)
- mutual_friends_ids.update(dict(((mutual_friend, 0) for mutual_friend in mutual_friends)))
- likers_ids = Counter()
- commentators_ids = Counter()
- posters_ids = Counter()
- reposters_ids = Counter()
- tagged_ids = Counter()
- analyse_wall(str(user_id), likers_ids, commentators_ids, posters_ids, reposters_ids, mutual_friends_ids)
- analyse_photos(str(user_id), likers_ids, commentators_ids, tagged_ids, mutual_friends_ids)
- get_k(mutual_friends_ids)
- return mutual_friends_ids
- if __name__ == '__main__':
- additive_edges= {}
- users_ids = {int(user): 0}
- for i in range(depth):
- people = users_ids.copy()
- for p in people:
- users_ids.update(get_friends(p, 0))
- edges = []
- for u in users_ids:
- likers_ids = Counter()
- commentators_ids = Counter()
- posters_ids = Counter()
- reposters_ids = Counter()
- tagged_ids = Counter()
- if not users_ids[u]:
- get_social_activity(str(u))
- friends_ids = get_friends(str(u), 1)
- analyse_wall(str(u), likers_ids, commentators_ids, posters_ids, reposters_ids, friends_ids)
- analyse_photos(str(u), likers_ids, commentators_ids, tagged_ids, friends_ids)
- get_k(friends_ids)
- edges.append([u, friends_ids])
- print(edges, '\n')
- users_ids.update(friends_ids)
- for f in friends_ids:
- if not users_ids[f]:
- get_social_activity(str(f))
- print(users_ids)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement