/
main.py
204 lines (166 loc) · 6.12 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from __future__ import division
from database import Db
import MySQLdb as _mysql
from config import ig_config
from instagram import client
import urlparse
from instagram.bind import InstagramAPIError
import multiprocessing, sys, logging, time
reload(sys)
sys.setdefaultencoding("utf-8")
__author__ = 'jaychow'
class igMine:
#API var
apiLimit = 5000
apiCurrent = 0
clientPosition = 0
clientLength = 0
#mine var
usernames = [
'sex_on_water',
'lavimeer',
'vita_century'
]
def __init__(self):
self.ig_config = ig_config
self.clientLength = len(ig_config)
print self.clientLength
def initDB(self):
self.db = Db()
self.db.connect()
def initIg(self):
pos = self.clientPosition
self.api = client.InstagramAPI(
client_id=self.ig_config[pos]['client_id'],
client_secret=self.ig_config[pos]['client_secret']
)
def search(self, user_id, max_id):
user = self.api.user(user_id)
media_search = self.api.user_recent_media(user_id=user_id, count=35, max_id=max_id);
return media_search
def getUserId(self, username):
user_search = self.api.user_search(username)
return int(user_search[0].id)
programStartTime = time.time()
apiCall = 0
totalCounter = 0
miner = igMine()
miner.initDB()
miner.initIg()
def addImage(media, tags):
try:
miner.db.addImage('user_ig', media)
for tag in tags:
tagId = miner.db.getTagId(tag=tag.name)
if(tagId):
if(not miner.db.tagRelationExists(tagId, media['id'])):
miner.db.addTagRelation(tagId, media['id'])
else:
tagId = miner.db.addTag(tag.name)
if(not miner.db.tagRelationExists(tagId, media['id'])):
miner.db.addTagRelation(tagId, media['id'])
except Exception as e:
logging.warning(media['id'] + " - " + str(e))
return
for username in miner.usernames:
elapseTime = time.time()-programStartTime
for username in miner.usernames:
user_id = miner.getUserId(username)
more_records = True
max_id = None
count = 0
while(more_records):
result = miner.search(user_id, max_id)
if result[1] is not None:
next = urlparse.parse_qs(result[1])
else:
next = False
collection = result[0]
for media in collection:
ids = media.id.split('_')
lat = 0
lon = 0
location = 0
caption = ""
if hasattr(media, 'location'):
if(hasattr(media.location.point,'latitude')):
lat = media.location.point.latitude
if(hasattr(media.location.point,'longitude')):
lon = media.location.point.longitude
if media.caption is not None:
caption = media.caption.text.replace("'", r"\'")
image = {
'id': ids[0],
'type': media.type,
'like_count': media.like_count,
'caption': caption,
'filter': media.filter,
'link': media.link,
'user_id': media.user.id,
'username': media.user.username,
'created_time': media.created_time,
'low_res': media.images['low_resolution'].url,
'thumbnail': media.images['thumbnail'].url,
'standard_res': media.images['standard_resolution'].url,
'lat': lat,
'lon': lon
}
print image
addImage(image,media.tags)
count += 1
if next is not False:
more_records = True
max_id = str(next['max_id'][0])
else:
more_records = False
'''
try:
percentDone = (totalCounter/totalIter)*100
print "Elapse Time:" + elapseTime.__str__()
print str(percentDone) + "%.... - " + str(apiCall) + " - " + str(timeStart)
nextTime = timeStart - advInterval
media_search = miner.search(max_timestamp = timeStart, min_timestamp = nextTime)
processList = []
for media in media_search:
#print "-----" + str(miner.apiCurrent) + ' - entry: ' + media.id + " - " + media.link
miner.apiCurrent += 1
ids = media.id.split('_')
lat = 0
lon = 0
location = 0
caption = ""
if(hasattr(media, 'location')):
lat = media.location.point.latitude
lon = media.location.point.longitude
if(media.caption is not None):
caption = media.caption.text.replace("'", r"\'")
image = {
'id': ids[0],
'type': media.type,
'like_count': media.like_count,
'caption': caption,
'filter': media.filter,
'link': media.link,
'user_id': media.user.id,
'username': media.user.username,
'created_time': media.created_time,
'low_res': media.images['low_resolution'].url,
'thumbnail': media.images['thumbnail'].url,
'standard_res': media.images['standard_resolution'].url,
'lat': lat,
'lon': lon
}
addImage(image,media.tags)
apiCall += 1
totalCounter += 1
timeStart = nextTime
except InstagramAPIError as e:
logging.warning(str(timeStart) + " - " + str(e.status_code) + ": " + e.error_type + " - " + e.error_message)
print e.status_code
if(e.status_code == '429'):
if miner.clientPosition == miner.clientLength-1:
miner.clientPosition == 0
else:
miner.clientPosition += 1
miner.initIg()
'''