-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
99 lines (84 loc) · 3.28 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
__author__ = 'yardeneitan'
from instagram import client
import foursquare
from Levenshtein import distance
from more_itertools import unique_everseen
from config import IG_CONFIG, FS_CONFIG
class InstagramUser:
def __init__(self, name, username, picture, followers, id):
self.name = name
self.username = username
self.picture = picture
self.followers = followers
self.id = id
def foursquare_connect():
# Construct the client object
client = foursquare.Foursquare(client_id=FS_CONFIG['client_id'], client_secret=FS_CONFIG['client_secret'])
return client
def instagram_connect():
api = client.InstagramAPI(client_id=IG_CONFIG['client_id'], client_secret=IG_CONFIG['client_secret'])
return api
def write_to_file(instagram_user, i, file):
file.write('----------------------------\n')
file.write('User #' + str(i) + '\n')
file.write('name: ' + instagram_user.name + '\n')
file.write('id: ' + instagram_user.id + '\n')
file.write('username: ' + instagram_user.username + '\n')
file.write('profile picture: ' + instagram_user.picture + '\n')
file.write('followers: ' + str(instagram_user.followers) + '\n\n')
fs_api = foursquare_connect()
ig_api = instagram_connect()
places = [line.rstrip('\n') for line in open('cool_places')]
f = open('influencers','w')
i = 1
user_output = []
for place in places:
place = place.encode('utf-8')
print(place)
response = fs_api.venues.search(params={'query': place, 'near': 'San Francisco, CA'})
if not response['venues'] or len(response['venues']) == 0:
print('venue not found')
continue
best_dist = 1000
best_index = 0
for index,fs_venue in enumerate(response['venues']):
if index == 5:
break
dist = distance(place, fs_venue['name'].encode('utf-8'))
if dist < best_dist:
best_dist = dist
best_index = index
fs_venue_id = response['venues'][best_index]['id']
ig_venue = ig_api.location_search(foursquare_v2_id=fs_venue_id)
if not ig_venue or len(ig_venue) == 0:
print('couldnt find the venue on instagram')
continue
ig_venue_id = ig_venue[0].id
recent_media_for_venue = ig_api.location_recent_media(location_id=ig_venue_id, count=100)
if len(recent_media_for_venue) == 0:
print('couldnt find media for venue')
continue
for media in recent_media_for_venue[0]:
user = media.user
if not user:
print('this media has no proper user')
continue
full_user = ig_api.user(user_id=user.id)
if full_user.counts['followed_by'] > 1000:
print(i)
ig_user = InstagramUser(name=user.full_name.encode('utf-8'),
username=user.username.encode('utf-8'),
id=user.id,
picture=user.profile_picture,
followers=full_user.counts['followed_by'])
write_to_file(ig_user, i, f)
user_output.append(ig_user)
i+=1
f.close()
i = 1
user_output.sort(key=lambda x: x.followers, reverse=True)
user_output = list(unique_everseen(user_output))
f = open('influencers_ordered','w')
for user in user_output:
write_to_file(user, i, f)
i+=1