def get(self): self.response.out.write('TwiNa service is running now!<br>') last_id = LastID.get_by_key_name(s_name) if not last_id: self.response.out.write( """This is your first time to this page!<br>""") self.response.out.write( """TwiNa will now synchronize your last tweet!<br>""") auth = BasicAuthHandler(s_name, s_pass) api = API(auth, source=s_app) tl_file = urllib.urlopen(t_timeline_url % t_name) timeline = json.load(tl_file) tweet = replace_tweet(timeline[0]['text']) status_id = LastID(key_name=s_name) status_id.twitter_last_id = timeline[0]['id_str'] status_id.put() try: api.update_status(tweet) except WeibopError, e: self.response.out.write(e) else: self.response.out.write( 'Your Last Tweet has already been synchronize:<br>') self.response.out.write("<b>%s</b>" % timeline[0]['text'])
def get(self): status_id = LastID.get_by_key_name(s_name) if not status_id: return tl_file = urllib.urlopen(t_timeline_url % t_name) timeline = json.load(tl_file) if isinstance(timeline, list): last_id = int(status_id.twitter_last_id) tweets_to_be_post = [] for tl in reversed(timeline): if int(tl['id_str']) > last_id: tweets_to_be_post.append({ 'id_str': tl['id_str'], 'text': tl['text'] }) if len(tweets_to_be_post) > 0: auth = BasicAuthHandler(s_name, s_pass) api = API(auth, source=s_app) for tweet_obj in tweets_to_be_post: cur_id = tweet_obj['id_str'] cur_tweet = tweet_obj['text'] if cur_tweet.find('#nosina') != -1 or cur_tweet.startswith( '@'): continue tweet = replace_tweet(cur_tweet) try: api.update_status(tweet) status_id.twitter_last_id = cur_id status_id.put() except WeibopError, e: self.response.out.write(e) self.response.out.write('<br>')
def __init__(self, username, password, listener, timeout=5.0, retry_count = None, retry_time = 10.0, snooze_time = 5.0, buffer_size=1500, headers=None): self.auth = BasicAuthHandler(username, password) self.running = False self.timeout = timeout self.retry_count = retry_count self.retry_time = retry_time self.snooze_time = snooze_time self.buffer_size = buffer_size self.listener = listener self.api = API() self.headers = headers or {} self.body = None
def basicAuth(self, source, username, password): self.auth = BasicAuthHandler(username, password) self.api = API(self.auth, source=source)
raise if USE_HTTPS: prefix = 'https://' else: prefix = 'http://' buzz_url = prefix + 'www.googleapis.com/buzz/v1/activities/' + BUZZ_USERID + '/@public?alt=json' # 读buzz fp = urlopen(buzz_url) #fp = open('buzz.json') buzz = load(fp) fp.close() # 微博认证 auth = BasicAuthHandler(WEIBO_USERNAME, WEIBO_PASSWORD) api = API(auth, source=WEIBO_APP_KEY) # 读已经同步过的activity id synced_ids = set() try: fp = open(HISTORY_FILE, 'r') for line in fp: synced_ids.add(line.strip()) fp.close() except IOError as (errno, strerror): # 如果文件不存在,就继续;否则,触发异常 if errno != 2: raise # 开始同步