forked from zhwindy/follow_center
/
instagram_oper.py
executable file
·259 lines (229 loc) · 9.1 KB
/
instagram_oper.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pg
from public_bz import storage
import public_bz
import time
import requests
import time_bz
import instagram
from instagram.client import InstagramAPI
import json
import ConfigParser
import public_db
config = ConfigParser.ConfigParser()
import sys
sys.setrecursionlimit(5000) # set the maximum depth as 5000
with open('conf/instagram.ini', 'r') as cfg_file:
config.readfp(cfg_file)
access_token = config.get('secret', 'access_token')
client_secret = config.get('secret', 'client_secret')
client_id = config.get('secret', 'client_id')
api = InstagramAPI(access_token=access_token, client_secret=client_secret)
def getCode():
#url = 'https://instagram.com/oauth/authorize/?client_id=%s&redirect_uri=http://follow.center/i_call_back&response_type=code' % client_id
get = '''
curl -F 'client_id=%s' \
-F 'client_secret=%s' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=http://follow.center/i_call_back' \
-F 'code=%s' \
https://api.instagram.com/oauth/access_token
''' % (client_id, client_secret, 'd97863d71e1c41759d9f132f342c9750')
print get
def getUser(user_name, always_check=False):
'''
modify by bigzhu at 15/07/31 16:41:16 api找用户时是按专注度来排序的,名字绝对匹配的未必是第一位, 从10个里面找
'''
users = list(pg.select('instagram_user', where="lower(username)=lower('%s')" % user_name))
if users and not always_check:
return users[0]
else:
user = None
try:
for this_user in api.user_search(user_name, 10):
if this_user.username.lower() == user_name.lower():
user = this_user
break
except instagram.bind.InstagramClientError:
print public_bz.getExpInfoAll()
return
# 如果没有这个用户
if user is None:
public_db.delNoName('instagram', user_name)
return
try:
user = api.user(user.id)
except (instagram.bind.InstagramAPIError, instagram.bind.InstagramClientError):
# 通常是没有访问权限
print public_bz.getExpInfoAll()
public_db.delNoName('instagram', user_name)
return
db_user = storage()
db_user.id_str = user.id
db_user.username = user.username
db_user.full_name = user.full_name
db_user.profile_picture = user.profile_picture
db_user.bio = user.bio
db_user.website = user.website
db_user.counts = json.dumps(user.counts)
#pg.insert('instagram_user', **db_user)
pg.insertOrUpdate(pg, 'instagram_user', db_user, "id_str='%s'" % db_user.id_str)
#db_bz.insertIfNotExist(pg, 'instagram_user', db_user, "id=%s" % db_user.id)
return getUser(user_name)
def saveMedias(user, medias):
'''
create by bigzhu at 15/09/04 20:58:54 保存meedias
"attribution":null,
"tags":[ ],
"type":"image",
"location":{ },
"comments":{ },
"filter":"Normal",
"created_time":"1441362020",
"link":"https:\/\/instagram.com\/p\/7NIHiLJJs3\/",
"likes":{ },
"images":{ },
"users_in_photo":[ ],
"caption":{ },
"user_has_liked":false,
"id":"1066544388859271991_262341",
"user":{ }
'''
for media_d in medias['data']:
media = storage(media_d)
db_media = storage()
#db_media.attribution = media.attribution
#db_media.tags = json.dumps(media.tags, cls=public_bz.ExtEncoder)
db_media.type = media.type
#db_media.location = json.dumps(media.location, cls=public_bz.ExtEncoder)
db_media.comments = json.dumps(media.comments, cls=public_bz.ExtEncoder)
db_media.filter = media.filter
#db_media.created_time = time_bz.timestampToDateTime(media.created_time) + timedelta(hours=8)
db_media.created_time = time_bz.timestampToDateTime(media.created_time)
db_media.link = media.link
#db_media.likes = json.dumps(media.likes, cls=public_bz.ExtEncoder)
db_media.low_resolution = json.dumps(media.images['low_resolution'])
db_media.standard_resolution = json.dumps(media.images['standard_resolution'])
db_media.thumbnail = json.dumps(media.images['thumbnail'])
#db_media.users_in_photo = json.dumps(media.users_in_photo, cls=public_bz.ExtEncoder)
if media.caption:
caption = media.caption
caption['user_id'] = caption['from']['id']
del caption['from']
else:
caption = ''
db_media.caption = json.dumps(caption, cls=public_bz.ExtEncoder)
db_media.id_str = media.id
db_media.user_id_str = user.id
#id = pg.insertIfNotExist(pg, 'instagram_media', db_media, "id_str='%s'" % db_media.id_str)
m = public_bz.storage()
m.id_str = db_media.id_str
m.m_type = 'instagram'
m.m_user_id = db_media.user_id_str
m.created_at = db_media.created_time
m.extended_entities = db_media.standard_resolution
m.content = db_media.comments
if media.caption:
m.text = media.caption['text']
else:
m.text = None
m.href = db_media.link
m.type = db_media.type
id = pg.insertIfNotExist(pg, 'm', m, "id_str='%s'" % db_media.id_str)
if id is None:
# 似乎就是会有重复的
error = '重复记录 id=%s, name=%s' % (media.id, user.username)
print error
#raise Exception(error)
else:
print 'new=', media.id, user.username
# if id is not None and len(medias) <= 2: # 新增加消息,微信通知只通知2条以内
# openids = public_db.getOpenidsByName('instagram', user.username)
# for data in openids:
# if caption != '':
# text = caption.get('text')
# else:
# text = ''
# wechat_oper.sendInstagram(data.openid, text, media.images['low_resolution']['url'], user.username, id)
if medias['pagination']:
next_url = medias['pagination']['next_url']
medias = callGetMeidaApi(next_url=next_url)
saveMedias(user, medias)
def saveLastId(user, medias):
'''
create by bigzhu at 15/09/04 21:42:06 保存最后那条记录,删除重复记录
modify by bigzhu at 15/09/05 09:12:42 删除重复记录不应该影响update id
'''
medias = medias['data']
if medias:
if medias[-1]['id'] == user.last_id: # 会取出最后一条,要删了
del medias[-1]
if medias:
last_id = medias[0]['id']
pg.update('instagram_user', where="lower(username)=lower('%s')" % user.username, last_id=last_id)
def callGetMeidaApi(user_id=None, min_id=None, next_url=None):
params = {'count': 9999,
'access_token': access_token,
}
if min_id:
params['min_id'] = min_id
if user_id:
url = '''https://api.instagram.com/v1/users/%s/media/recent''' % user_id
try:
if next_url:
r = requests.get(next_url)
else:
r = requests.get(url, params=params)
except requests.exceptions.ConnectionError:
print public_bz.getExpInfoAll()
return
if r.status_code == 200:
medias = r.json()
return medias
else:
print r.status_code
def main(user_name=None):
user = getUser(user_name, always_check=True)
if user is None:
# 用户都没有,不用往下了
return
try:
# https://api.instagram.com/v1/users/1337827037/media/recent/?access_token=1337827037.933ab14.2a607a5fc0534f9f9900e75196a2dfbb&min_id=1054034416535329463_1337827037
# 即使设置了min_id,instagram还是会把当前这条min_id返回来,简直了
#medias, next_ = api.user_recent_media(user_id=user.id, min_id=user.last_id)
medias = callGetMeidaApi(user.id, user.last_id)
except instagram.bind.InstagramClientError:
print public_bz.getExpInfoAll()
public_db.delNoName('instagram', user_name)
return
if medias is None:
return
saveLastId(user, medias)
if len(medias['data']) != 0:
print len(medias['data'])
saveMedias(user, medias)
def check(user_name=None):
'''
create by bigzhu at 15/07/31 14:28:30
modify by bigzhu at 15/08/23 22:30:25 可以指定查某个user_name
'''
where = '''
instagram is not null and instagram!=''
'''
if user_name:
where += " and instagram='%s'" % user_name
users = pg.select('user_info', what='instagram', where=where)
for user in users:
if user.instagram and user.instagram != '':
print 'check instagram %s' % user.instagram
main(user.instagram)
# try:
# getMedia(user.instagram)
# except Exception:
# print public_bz.getExpInfoAll()
if __name__ == '__main__':
while True:
check()
pg.refresh('messages')
time.sleep(600)