def getUserTimeline(screen_name): ''' 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 避免批量微信通知 ''' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) try: public_tweets = api.user_timeline(screen_name=screen_name) for tweet in public_tweets: #tweet.created_at = tz.localize(tweet.created_at) tweet.created_at += timedelta(hours=8) # print tweet.created_at id = saveTwitter(copy.deepcopy(tweet)) if id is not None and len(public_tweets) <= 3: # 新增加消息 openids = public_db.getOpenidsByName('twitter', screen_name) print 'new=', tweet.text for data in openids: wechat_oper.sendTwitter(data.openid, tweet, screen_name, id) except tweepy.error.TweepError: print 'screen_name=', screen_name public_bz.getExpInfo()
def sendArticle(openid, articles): ''' 发送信息 create by bigzhu at 15/07/24 09:35:31 ''' wechat = getWechat() try: wechat.send_article_message(openid, articles) except wechat_sdk.exceptions.OfficialAPIError: print 'openid=', openid, ' ', public_bz.getExpInfo() except Exception: print public_bz.getExpInfo()
def wrapper(self, *args, **kwargs): self.set_header("Content-Type", "application/json") try: method(self, *args, **kwargs) except Exception: print public_bz.getExpInfoAll() self.write(json.dumps({'error': public_bz.getExpInfo()}))
def createTable(Model, db_name, user=None, password=None, host='127.0.0.1'): ''' create by bigzhu at 15/04/04 01:08:30 建立数据库表; peewee 要在定义时候指定 db 相当不人性化,修正 modify by bigzhu at 15/04/04 01:32:46 没有这个数据库的时候,直接返回建立数据的语句 modify by bigzhu at 15/04/04 01:45:43 如果表已经存在,不能往下继续了 ''' if user is None: user = db_name if password is None: password = db_name if host is None: host = '127.0.0.1' #db = PostgresqlExtDatabase(db_name, user=user, password=password, host='127.0.0.1', register_hstore=False) db = PostgresqlExtDatabase(db_name, user=user, password=password, host=host, register_hstore=False) Model._meta.database = db try: if Model.table_exists(): print 'table %s already exists' % Model.__name__ return createBaseTable(db) Model.create_table() print 'create table ' + Model.__name__ except peewee.OperationalError: print public_bz.getExpInfo() showDBCreate(db_name) exit(1) table_name = Model.__name__ if table_name != 'base': sql = ''' alter table %s inherit base; ''' % table_name db.execute_sql(sql) resetBaseDefault(db) # add table comment comment = Model.__doc__ sql = ''' COMMENT ON TABLE %s IS '%s'; ''' % (table_name, comment) db.execute_sql(sql)
def dropTable(Model, db_name, user=None, password=None, host='127.0.0.1'): ''' create by bigzhu at 15/04/04 13:12:02 还是需要一个删除表的功能 ''' if user is None: user = db_name if password is None: password = db_name if host is None: host = '127.0.0.1' #db = PostgresqlDatabase(db_name, user=user, password=password, host='127.0.0.1') #db = PostgresqlExtDatabase(db_name, user=user, password=password, host='127.0.0.1', register_hstore=False) db = PostgresqlExtDatabase(db_name, user=user, password=password, host=host, register_hstore=False) Model._meta.database = db try: Model.drop_table(True) except peewee.OperationalError: print public_bz.getExpInfo() showDBCreate(db_name) exit(1) print 'drop table ' + Model.__name__
def waitReset(god_name, twitter_name, god_id): while True: try: remaining = getRemaining() except tweepy.error.TweepError: error_info = public_bz.getExpInfo() print error_info time.sleep(1200) continue print 'remaining:', remaining if remaining == 0: time.sleep(1200) else: main(god_name, twitter_name, god_id, wait=True) break
def wrapper(self, *args, **kwargs): openid = self.get_secure_cookie("openid") if openid is None: # 连openid 都没有,首先要获取 openid params = { "appid": self.settings['appid'], # "redirect_uri": "http://" + self.settings['domain'] + "/setOpenid?url=/" + self.__class__.__name__, # "redirect_uri": "http://" + self.settings['domain'] + "/setOpenid?url=" + self.request.uri, "redirect_uri": "http://" + "admin.hoywe.com/" + self.settings['suburl'] + "/?url=" + self.request.uri, "response_type": "code", "scope": "snsapi_base", } auth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?%s#wechat_redirect" auth_url = auth_url % urllib.urlencode(params) self.redirect(auth_url) return else: #exists_users = list(self.pg.db.select('wechat_user', where="openid='%s'" % openid)) # if not exists_users: try: wechat_user_info = self.wechat.get_user_info(openid, lang='zh_CN') except OfficialAPIError as e: print public_bz.getExpInfoAll() self.clear_cookie(name='openid') error = public_bz.getExpInfo() if error.find('40001') != -1: raise e else: error_info = ''' <html> <script type="text/javascript"> alert("微信服务器异常,请关闭后,重新打开"); WeixinJSBridge.call('closeWindow'); </script> </html> ''' self.write(error_info) return # 没有关注的,跳转到配置的关注页面 if wechat_user_info['subscribe'] == 0: self.redirect('http://' + self.settings["domain"] + self.settings["subscribe"]) return # else: # print 'add user' # self.pg.db.insert('wechat_user', **wechat_user_info) return method(self, *args, **kwargs)
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)
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 getUserTimeline(screen_name): ''' 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 避免批量微信通知 ''' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) try: public_tweets = api.user_timeline(screen_name=screen_name) for tweet in public_tweets: tweet.created_at += timedelta(hours=8) saveTwitter(copy.deepcopy(tweet)) except tweepy.error.TweepError: print 'screen_name=', screen_name error_info = public_bz.getExpInfo() print error_info if 'Sorry, that page does not exist.' in error_info: public_db.delNoName('twitter', screen_name)