Advertisement
Guest User

Proj

a guest
Apr 1st, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.86 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 not get_all:
  38.         friends_url = append_to_query(friends_url, count=15)
  39.     friends = get_response(friends_url)
  40.     return dict(((friend_id, 0) for friend_id in friends))
  41.  
  42. def get_likes(url, item_id, likers_ids):
  43.     likers = get_response(url, item_id=item_id)
  44.     if likers:
  45.         likers_ids.update(likers['users'])
  46.  
  47. def get_comments(url, item, item_id, commentators_ids, friends_ids):
  48.     comments_url = create_url(url, item=item_id)
  49.     if item == 'photo_id':
  50.         comments_url = append_to_query(comments_url, access_token=token)
  51.     comments = get_response(comments_url)
  52.     commentators_ids.update((comment['from_id'] for comment in comments[1:] if comment['from_id'] in friends_ids))
  53.     for comment in comments[1:]:
  54.         if comment['likes']['count']:
  55.             get_likes(create_url('likes.getList', owner_id=user_id, type='comment', count=100, friends_only=1), comment['cid'], likers_ids)
  56.  
  57. def analyse_wall(user_id, likers_ids, commentators_ids, posters_ids, reposters_ids, friends_ids):
  58.     #get posts from user's wall and some info about likes and comments there
  59.     posts = get_response(create_url('wall.get'), owner_id=user_id, count=100)
  60.     likes_url = create_url('likes.getList', owner_id=user_id, type='post', count=100, friends_only=1)
  61.     reposts_url = create_url('wall.getReposts', owner_id=user_id, count=1000)
  62.     comments_url = create_url('wall.getComments', owner_id=user_id, count=100, need_likes=1)
  63.     for post in posts[1:]:
  64.         from_id = post['from_id']
  65.         if from_id in friends_ids:
  66.             posters_ids.update([from_id])
  67.         if from_id == user_id and post['likes']['count']:
  68.             get_likes(likes_url, post['id'], likers_ids)
  69.         if from_id == user_id and post['comments']['count']:
  70.             get_comments('post_id', post['id'], commentators_ids, friends_ids)
  71.  
  72.         #analyse reposts of our user's posts
  73.         if post['reposts']['count'] and from_id == int(user_id):
  74.             reposts = get_response(reposts_url, post_id=post['id'])
  75.             if reposts:
  76.                 reposters_ids.update((repost['from_id'] for repost in reposts['items'] if repost['from_id'] in friends_ids))
  77.  
  78. #move to the photos, start with getting all the albums and all the photos in them
  79. def analyse_photos(user_id, likers_ids, commentators_ids, tagged_ids, friends_ids):
  80.     albums = get_response(create_url('photos.getAlbums'), owner_id=user_id, need_system=1)
  81.     photos_url = create_url('photos.get', owner_id=user_id, count=1000)
  82.     likes_url = create_url('likes.getList', type='photo', owner_id=user_id, count=100, friends_only=1)
  83.     comments_url = create_url('photos.getComments', owner_id=user_id, count=100, need_likes=1)
  84.     tags_url = create_url('photos.getTags', owner_id=user_id, access_token=token)
  85.     for album in albums:
  86.         photos = get_response(photos_url, album_id=album['aid'])
  87.         for photo in photos:
  88.             get_likes(likes_url, photo['pid'], likers_ids)
  89.             get_comments(comments_url, 'photo_id', photo['pid'], commentators_ids, friends_ids)
  90.  
  91.             #...and the tags
  92.             tags = get_response(tags_url, photo_id=photo['pid'])
  93.             tagged_ids.update((tag['user_id'] for tag in tags[1:] if tag['user_id'] in friends_ids))
  94.  
  95. def get_social_activity(user_id):
  96.     time_between_posts = 0
  97.     last_post = 0
  98.     cnt = 0
  99.     posts = get_response(create_url('wall.get'), owner_id=user_id, count=100, filter='owner')
  100.     if posts:
  101.         users_ids[int(user_id)] += posts[0]*k_post
  102.     for post in posts[1:]:
  103.         if (last_post):
  104.             time_between_posts += abs(last_post - post['date'])
  105.         last_post = post['date']
  106.         cnt += 1
  107.     if (time_between_posts):
  108.         users_ids[int(user_id)] += round(cnt / time_between_posts)
  109.  
  110. def get_k(friends):
  111.     for f in friends:
  112.         if f in likers_ids:
  113.             friends[f] += k_like*likers_ids[f]
  114.         if f in commentators_ids:
  115.             friends[f] += k_comment*commentators_ids[f]
  116.         if f in posters_ids:
  117.             friends[f] += k_post*posters_ids[f]
  118.         if f in reposters_ids:
  119.             friends[f] += k_repost*reposters_ids[f]
  120.         if f in tagged_ids:
  121.             friends[f] += k_tag*tagged_ids[f]
  122.     return friends
  123.  
  124. def find_more_edges(user_id, users_ids):
  125.     mutual_friends_ids = {}
  126.     for some_id in users_ids:
  127.         mutual_friends = get_response(create_url('friends.getMutual'), source_id=user_id, target_uid=some_id, access_token=token)
  128.         mutual_friends_ids.update(dict(((mutual_friend, 0) for mutual_friend in mutual_friends)))
  129.        
  130.     likers_ids = Counter()
  131.     commentators_ids = Counter()
  132.     posters_ids = Counter()
  133.     reposters_ids = Counter()
  134.     tagged_ids = Counter()
  135.        
  136.     analyse_wall(str(user_id), likers_ids, commentators_ids, posters_ids, reposters_ids, mutual_friends_ids)
  137.     analyse_photos(str(user_id), likers_ids, commentators_ids, tagged_ids, mutual_friends_ids)
  138.     get_k(mutual_friends_ids)
  139.  
  140.     return mutual_friends_ids
  141.  
  142. if __name__ == '__main__':
  143.     additive_edges= {}
  144.     users_ids = {int(user): 0}
  145.     for i in range(depth):
  146.         people = users_ids.copy()
  147.         for p in people:
  148.             users_ids.update(get_friends(p, 0))
  149.  
  150.     edges = []
  151.     for u in users_ids:      
  152.         likers_ids = Counter()
  153.         commentators_ids = Counter()
  154.         posters_ids = Counter()
  155.         reposters_ids = Counter()
  156.         tagged_ids = Counter()
  157.  
  158.         if not users_ids[u]:
  159.             get_social_activity(str(u))
  160.         friends_ids = get_friends(str(u), 1)
  161.         analyse_wall(str(u), likers_ids, commentators_ids, posters_ids, reposters_ids, friends_ids)
  162.         analyse_photos(str(u), likers_ids, commentators_ids, tagged_ids, friends_ids)
  163.         get_k(friends_ids)
  164.         edges.append([u, friends_ids])      
  165.     print(edges, '\n')
  166.  
  167.     users_ids.update(friends_ids)
  168.     for f in friends_ids:
  169.         if not users_ids[f]:
  170.             get_social_activity(str(f))
  171.     print(users_ids)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement