def main(god, wait): ''' create by bigzhu at 15/07/15 17:54:08 取github modify by bigzhu at 15/07/22 16:20:42 时间同样要加入8小时,否则不正确 ''' god_name = god.name github_name = god.github['name'] god_id = god.id etag = god.github.get('sync_key') github_user = getGithubUser(github_name, god_name) if github_user is None: return headers = {'If-None-Match': etag} try: r = requests.get('https://api.github.com/users/%s/events' % github_name, headers=headers, params=params) except requests.exceptions.ConnectionError: print public_bz.getExpInfoAll() return if r.status_code == 200: etag = r.headers['etag'] limit = r.headers['X-RateLimit-Remaining'] if limit == '0': return for message in r.json(): message = storage(message) saveMessage(god_name, github_name, god_id, message) # oper.noMessageTooLong('github', github_name) saveUser(god_name, github_name, github_user, etag) # 为了update etag if r.status_code == 404: # public_db.sendDelApply('github', god_name, github_name, '404') god_oper.delNoName('github', god_name)
def getTumblrUserNotSaveKey(god_name, tumblr_name): blogs = callGetMeidaApi(god_name=tumblr_name, limit=1) if blogs is None: # public_db.sendDelApply('tumblr', god_name, tumblr_name, 'not have user') god_oper.delNoName('tumblr', god_name) return tumblr_user = blogs['response']['blog'] tumblr_user['updated'] = None saveUser(god_name, tumblr_name, tumblr_user)
def getTumblrUser(god_name, tumblr_name, save=True): blogs = callGetMeidaApi(god_name=tumblr_name, limit=1) if blogs is None: # public_db.sendDelApply('tumblr', god_name, tumblr_name, 'not have user') god_oper.delNoName('tumblr', god_name) raise NoUser('%s blog is None' % tumblr_name) tumblr_user = blogs['response']['blog'] if save: saveUser(god_name, tumblr_name, tumblr_user) return tumblr_user
def getGithubUser(github_name, god_name): try: r = requests.get('https://api.github.com/users/%s' % github_name, params=params) github_user = r.json() message = github_user.get('message') if message == 'Not Found': god_oper.delNoName('github', god_name) return saveUser(god_name, github_name, github_user) return github_user except requests.exceptions.ConnectionError: print public_bz.getExpInfoAll() return except ValueError: print public_bz.getExpInfoAll() return
def main(god): ''' create by bigzhu at 16/06/10 14:01:16 facebook ''' god_name = god['name'] facebook_name = god['facebook']['name'] god_id = god.id etag = god['facebook'].get('sync_key') user_id = god['facebook'].get('out_id') if etag: pass else: user_id = getFaceBookUserId(facebook_name) params = { 'access_token': access_token, 'fields': 'username,link,bio,picture,feed{created_time,full_picture,message,link,description,type,source}' } url = "https://graph.facebook.com/%s" % user_id headers = {'If-None-Match': etag} r = requests.get(url, params=params, headers=headers) if r.status_code == 200: etag = r.headers['etag'] r = r.json() r['user_id'] = user_id saveUser(god_name, facebook_name, r, etag) for message in r['feed']['data']: saveMessage(god_name, facebook_name, god_id, message) elif r.status_code == 304: pass elif r.status_code == 404: # public_db.sendDelApply('facebook', god_name, facebook_name, '404') god_oper.delNoName('facebook', god_name) else: print r.status_code
def getFacebookUser(facebook_name, god_name): ''' 只是给添加时候用 ''' user_id = getFaceBookUserId(facebook_name) params = { 'access_token': access_token, 'fields': 'username,link,bio,picture' } url = "https://graph.facebook.com/%s" % user_id r = requests.get(url, params=params) if r.status_code == 200: r = r.json() r['user_id'] = user_id saveUser(god_name, facebook_name, r, None) elif r.status_code == 404: # public_db.sendDelApply('facebook', god_name, facebook_name, '404') god_oper.delNoName('facebook', god_name) else: print r.status_code
def getTwitterUser(twitter_name, god_name): twitter_user = None try: twitter_user = api.get_user(screen_name=twitter_name) except tweepy.error.TweepError: print 'twitter_name=', twitter_name error_info = public_bz.getExpInfo() print error_info if 'User not found.' in error_info: god_oper.delNoName('twitter', god_name) if 'User has been suspended.' in error_info: # 帐号被冻结了 god_oper.delNoName('twitter', god_name) if 'Not authorized.' in error_info: # 私有 god_oper.delNoName('twitter', god_name) if 'Sorry, that page does not exist.' in error_info: # 没用户 god_oper.delNoName('twitter', god_name) if twitter_user: twitter_user = saveUser(god_name, twitter_name, twitter_user) else: god_oper.delNoName('twitter', god_name) return twitter_user
def sync(god): ''' create by bigzhu at 16/06/12 16:19:09 api disabled ''' ins_name = god.instagram['name'] god_name = god.name etag = god.instagram.get('sync_key') headers = {'If-None-Match': etag} url = "https://www.instagram.com/%s" % ins_name r = requests.get(url, headers=headers) if r.status_code == 200: etag = r.headers.get('etag') soup = BeautifulSoup(r.text) scripts = soup.find_all("script", type="text/javascript") for script in scripts: if '_sharedData' in str(script): if script.contents is None: return content = script.contents[0] content = content.replace('window._sharedData =', '') content = content.replace(';', '') content = json.loads(content) user_info = content['entry_data']['ProfilePage'][0]['user'] saveUser(god_name, ins_name, user_info, etag) if user_info['media'].get('nodes'): for message in user_info['media']['nodes']: saveMessage(ins_name, god_name, god.id, message) elif r.status_code == 304: pass elif r.status_code == 404: god_oper.delNoName(M_TYPE, god_name) else: print r.status_code
def main(god, wait): ''' create by bigzhu at 15/07/04 22:49:04 用 https://api.twitter.com/1.1/statuses/user_timeline.json 可以取到某个用户的信息 参看 https://dev.twitter.com/rest/reference/get/statuses/user_timeline modify by bigzhu at 15/07/04 22:53:09 考虑使用 http://www.tweepy.org/ 来调用twitter api modify by bigzhu at 15/08/02 21:35:46 避免批量微信通知 create by bigzhu at 16/04/30 09:56:02 不再取转发的消息 ''' god_name = god.name twitter_name = god.twitter['name'] god_id = god.id try: twitter_user = getTwitterUser(twitter_name, god_name) if not twitter_user: return public_tweets = api.user_timeline(screen_name=twitter_name, include_rts=False, exclude_replies=True) for tweet in public_tweets: tweet.created_at += timedelta(hours=8) saveMessage(god_name, twitter_name, god_id, tweet) # oper.noMessageTooLong('twitter', user.twitter) except tweepy.error.TweepError: print 'twitter_name=', twitter_name error_info = public_bz.getExpInfo() print error_info if 'User not found.' in error_info: god_oper.delNoName('twitter', god_name) if 'Rate limit exceeded' in error_info: # 调用太多 if wait: waitReset(god_name, twitter_name, god_id) else: raise Exception('Twitter api 的调用次数用完了,请等个10分钟再添加!') return 'Rate limit exceeded' if 'User has been suspended.' in error_info: # 帐号被冻结了 god_oper.delNoName('twitter', god_name) if 'Not authorized.' in error_info: # 私有 god_oper.delNoName('twitter', god_name) if 'Sorry, that page does not exist.' in error_info: # 没用户 god_oper.delNoName('twitter', god_name)