Advertisement
Guest User

Proj

a guest
Apr 1st, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.87 KB | None | 0 0
  1. import json
  2. from collections import Counter
  3. import requests
  4.  
  5. token = 'f94f52d6ef63a7f08b899641385b687a140e0a09ccb56951b552938c31f9944e00b9a194f3b53e29cd21b'
  6.  
  7. #Катя
  8. user = '9951179'
  9. #Козлов
  10. #user = '672736'
  11.  
  12. depth = 1
  13. k_like = 5
  14. k_post = 5
  15. k_repost = 5
  16. k_comment = 5
  17. k_tag = 5
  18. session = requests.session()
  19.  
  20. def append_to_query(query_url, **kwargs):
  21.     result = query_url
  22.     for (key, value) in kwargs.items():
  23.         result += '&%s=%s' % (key, value)
  24.     return result
  25.  
  26. def create_url(query, **kwargs):
  27.     url = 'https://api.vk.com/method/%s.json?' % query
  28.     url = append_to_query(url, **kwargs)
  29.     return url
  30.  
  31. def get_response(url, **kwargs):
  32.     query_url = append_to_query(url, **kwargs)
  33.     return session.get(query_url).json().get('response', [])
  34.  
  35. def get_friends(user_id, get_all):
  36.     friends_url = create_url('friends.get', user_id=user_id, order='hints', access_token=token)
  37.     if get_all:
  38.         friends = get_response(friends_url)
  39.     else:
  40.         friends = get_response(friends_url, count=15)
  41.     return dict(((friend_id, 0) for friend_id in friends))
  42.  
  43. def get_likes(url, item_id, likers_ids):
  44.     likers = get_response(url, item_id=item_id)
  45.     if likers:
  46.         likers_ids.update(likers['users'])
  47.  
  48. def get_comments(url, item, item_id, commentators_ids, friends_ids):
  49.     comments_url = create_url(url, item=item_id)
  50.     if item == 'photo_id':
  51.         comments = get_response(comments_url, access_token=token)
  52.     else:
  53.         comments = get_response(comments_url)
  54.     commentators_ids.update((comment['from_id'] for comment in comments[1:] if comment['from_id'] in friends_ids))
  55.     for comment in comments[1:]:
  56.         if comment['likes']['count']:
  57.             get_likes(create_url('likes.getList', owner_id=user_id, type='comment', count=100, friends_only=1), comment['cid'], likers_ids)
  58.  
  59. def analyse_wall(user_id, likers_ids, commentators_ids, posters_ids, reposters_ids, friends_ids):
  60.     #get posts from user's wall and some info about likes and comments there
  61.     posts = get_response(create_url('wall.get'), owner_id=user_id, count=100)
  62.     likes_url = create_url('likes.getList', owner_id=user_id, type='post', count=100, friends_only=1)
  63.     reposts_url = create_url('wall.getReposts', owner_id=user_id, count=1000)
  64.     comments_url = create_url('wall.getComments', owner_id=user_id, count=100, need_likes=1)
  65.     for post in posts[1:]:
  66.         from_id = post['from_id']
  67.         if from_id in friends_ids:
  68.             posters_ids.update([from_id])
  69.         if from_id == user_id and post['likes']['count']:
  70.             get_likes(likes_url, post['id'], likers_ids)
  71.         if from_id == user_id and post['comments']['count']:
  72.             get_comments('post_id', post['id'], commentators_ids, friends_ids)
  73.  
  74.         #analyse reposts of our user's posts
  75.         if post['reposts']['count'] and from_id == int(user_id):
  76.             reposts = get_response(reposts_url, post_id=post['id'])
  77.             if reposts:
  78.                 reposters_ids.update((repost['from_id'] for repost in reposts['items'] if repost['from_id'] in friends_ids))
  79.  
  80. #move to the photos, start with getting all the albums and all the photos in them
  81. def analyse_photos(user_id, likers_ids, commentators_ids, tagged_ids, friends_ids):
  82.     albums = get_response(create_url('photos.getAlbums'), owner_id=user_id, need_system=1)
  83.     photos_url = create_url('photos.get', owner_id=user_id, count=1000)
  84.     likes_url = create_url('likes.getList', type='photo', owner_id=user_id, count=100, friends_only=1)
  85.     comments_url = create_url('photos.getComments', owner_id=user_id, count=100, need_likes=1)
  86.     tags_url = create_url('photos.getTags', owner_id=user_id, access_token=token)
  87.     for album in albums:
  88.         photos = get_response(photos_url, album_id=album['aid'])
  89.         for photo in photos:
  90.             get_likes(likes_url, photo['pid'], likers_ids)
  91.             get_comments(comments_url, 'photo_id', photo['pid'], commentators_ids, friends_ids)
  92.  
  93.             #...and the tags
  94.             tags = get_response(tags_url, photo_id=photo['pid'])
  95.             tagged_ids.update((tag['user_id'] for tag in tags[1:] if tag['user_id'] in friends_ids))
  96.  
  97. def get_social_activity(user_id):
  98.     time_between_posts = 0
  99.     last_post = 0
  100.     cnt = 0
  101.     posts = get_response(create_url('wall.get'), owner_id=user_id, count=100, filter='owner')
  102.     if posts:
  103.         users_ids[int(user_id)] += posts[0]*k_post
  104.     for post in posts[1:]:
  105.         if (last_post):
  106.             time_between_posts += abs(last_post - post['date'])
  107.         last_post = post['date']
  108.         cnt += 1
  109.     if (time_between_posts):
  110.         users_ids[int(user_id)] += round(cnt / time_between_posts)
  111.  
  112. def get_k(friends):
  113.     for f in friends:
  114.         if f in likers_ids:
  115.             friends[f] += k_like*likers_ids[f]
  116.         if f in commentators_ids:
  117.             friends[f] += k_comment*commentators_ids[f]
  118.         if f in posters_ids:
  119.             friends[f] += k_post*posters_ids[f]
  120.         if f in reposters_ids:
  121.             friends[f] += k_repost*reposters_ids[f]
  122.         if f in tagged_ids:
  123.             friends[f] += k_tag*tagged_ids[f]
  124.     return friends
  125.  
  126. def find_more_edges(user_id, users_ids):
  127.     mutual_friends_ids = {}
  128.     for some_id in users_ids:
  129.         mutual_friends = get_response(create_url('friends.getMutual'), source_id=user_id, target_uid=some_id, access_token=token)
  130.         mutual_friends_ids.update(dict(((mutual_friend, 0) for mutual_friend in mutual_friends)))
  131.        
  132.     likers_ids = Counter()
  133.     commentators_ids = Counter()
  134.     posters_ids = Counter()
  135.     reposters_ids = Counter()
  136.     tagged_ids = Counter()
  137.        
  138.     analyse_wall(str(user_id), likers_ids, commentators_ids, posters_ids, reposters_ids, mutual_friends_ids)
  139.     analyse_photos(str(user_id), likers_ids, commentators_ids, tagged_ids, mutual_friends_ids)
  140.     get_k(mutual_friends_ids)
  141.  
  142.     return mutual_friends_ids
  143.  
  144. if __name__ == '__main__':
  145.     additive_edges= {}
  146.     users_ids = {int(user): 0}
  147.     for i in range(depth):
  148.         people = users_ids.copy()
  149.         for p in people:
  150.             users_ids.update(get_friends(p, 0))
  151.  
  152.     edges = []
  153.     for u in users_ids:      
  154.         likers_ids = Counter()
  155.         commentators_ids = Counter()
  156.         posters_ids = Counter()
  157.         reposters_ids = Counter()
  158.         tagged_ids = Counter()
  159.  
  160.         if not users_ids[u]:
  161.             get_social_activity(str(u))
  162.         friends_ids = get_friends(str(u), 1)
  163.         analyse_wall(str(u), likers_ids, commentators_ids, posters_ids, reposters_ids, friends_ids)
  164.         analyse_photos(str(u), likers_ids, commentators_ids, tagged_ids, friends_ids)
  165.         get_k(friends_ids)
  166.         edges.append([u, friends_ids])      
  167.     print(edges, '\n')
  168.  
  169.     users_ids.update(friends_ids)
  170.     for f in friends_ids:
  171.         if not users_ids[f]:
  172.             get_social_activity(str(f))
  173.     print(users_ids)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement