def main(): if len(cfg.http_proxy_host) == 0 or len(cfg.http_proxy_port) == 0: client = oauthapi.tpOAuth() else: client = oauthapi.tpOAuth(http_proxy={ 'host': cfg.http_proxy_host, 'port': cfg.http_proxy_port }) maxid = getmaxid() try: response = client.fetch_user_timeline(cfg.screen_name, 0, maxid + 1, 1, cfg.sync_rt_message) tweets = simplejson.loads(response) except: writelog('Twitter API limited') print 'API limited' return if maxid == 0: writelog('now tweets max id is %s' % tweets[0]['id']) savemaxid(tweets[0]['id']) return sina = Sina() sina.setToken(cfg.sina_access_token_key, cfg.sina_access_token_secret) #sina.update('hello sina microblog, abcd, F**K the developers of the API') for tweet in tweets: if maxid <= long(tweet['id']): maxid = long(tweet['id']) if not cfg.sync_reply_message and tweet['in_reply_to_user_id']: continue try: retweet = tweet['retweeted_status'] text = retweet['text'] mentions = retweet['entities']['user_mentions'] for mention in mentions: p = re.compile('@' + mention['screen_name']) text = p.sub(mention['screen_name'], text) text = u"转:" + text except Exception, e: text = tweet['text'] mentions = tweet['entities']['user_mentions'] for mention in mentions: p = re.compile('@' + mention['screen_name']) text = p.sub(mention['screen_name'], text) try: msg = "update---: " + text writelog(msg.encode('utf8')) sina.update(text) except WeibopError as e: writelog('sina error, maybe api limited') writelog('now tweets max id is %ld' % maxid) savemaxid(maxid) time.sleep(120)
def main(): if len(cfg.http_proxy_host) == 0 or len(cfg.http_proxy_port) == 0: client = oauthapi.tpOAuth() else: client = oauthapi.tpOAuth(http_proxy = { 'host': cfg.http_proxy_host, 'port': cfg.http_proxy_port }) maxid = getmaxid() try: response = client.fetch_user_timeline(cfg.screen_name, 0, maxid + 1, 1, cfg.sync_rt_message) tweets = simplejson.loads(response) except: writelog('Twitter API limited') print 'API limited' return if maxid == 0: writelog('now tweets max id is %s' % tweets[0]['id']) savemaxid(tweets[0]['id']) return sina = Sina() sina.setToken(cfg.sina_access_token_key, cfg.sina_access_token_secret) #sina.update('hello sina microblog, abcd, F**K the developers of the API') for tweet in tweets: if maxid <= long(tweet['id']): maxid = long(tweet['id']) if not cfg.sync_reply_message and tweet['in_reply_to_user_id']: continue try: retweet = tweet['retweeted_status'] text = retweet['text'] mentions = retweet['entities']['user_mentions'] for mention in mentions: p = re.compile('@' + mention['screen_name']) text = p.sub(mention['screen_name'], text) text = u"转:" + text except Exception, e: text = tweet['text'] mentions = tweet['entities']['user_mentions'] for mention in mentions: p = re.compile('@' + mention['screen_name']) text = p.sub(mention['screen_name'], text) try: msg = "update---: " + text writelog(msg.encode('utf8')) sina.update(text) except WeibopError as e: writelog('sina error, maybe api limited') writelog('now tweets max id is %ld' % maxid) savemaxid(maxid) time.sleep(120)
def oauth_request_thread(self, param = None): cfg = config.tp_cfg() cfg.load() self.client = oauthapi.tpOAuth(http_proxy = { 'host': cfg.proxy_host, 'port': cfg.proxy_port } if cfg.proxy_enable else None) self.request_token = self.client.fetch_request_token() if self.request_token is None: gtk.gdk.threads_enter() self.progress_bar.set_text('Fetch Request Token Failed!') self.oauth_area_destroy() self.start_button.show() gtk.gdk.threads_leave() return False gtk.gdk.threads_enter() self.progress_bar.set_text('Fetch Request Token Success!') gtk.gdk.threads_leave() self.client.authorization_token(self.request_token) return True
def create_param_panel(self): # generate parameters area frame = gtk.Frame('Parameters') self.gen_panel.pack_start(frame, False, False, 5) fixed = gtk.Fixed() fixed.set_size_request(460, 200) frame.add(fixed) self.tweets_type = TWEETS_MY_TIMELINE # add radio button in the left radio_button = gtk.RadioButton(None, label = 'my timeline') radio_button.connect('toggled', self.radio_button_toggled, TWEETS_MY_TIMELINE) fixed.put(radio_button, 10, 10) radio_button = gtk.RadioButton(radio_button, 'my tweets,retweeted') radio_button.connect('toggled', self.radio_button_toggled, TWEETS_MY_RETWEETED) fixed.put(radio_button, 10, 35) radio_button = gtk.RadioButton(radio_button, 'tweets retweeted by me') radio_button.connect('toggled', self.radio_button_toggled, TWEETS_RETWEETED_BY_ME) fixed.put(radio_button, 10, 60) radio_button = gtk.RadioButton(radio_button, 'my favorites') radio_button.connect('toggled', self.radio_button_toggled, TWEETS_MY_FAVORITES) fixed.put(radio_button, 10, 85) radio_button = gtk.RadioButton(radio_button, 'other\'s timeline') radio_button.connect('toggled', self.radio_button_toggled, TWEETS_OTHERS_TIMELINE) fixed.put(radio_button, 10, 110) radio_button = gtk.RadioButton(radio_button, 'other\'s favorites') radio_button.connect('toggled', self.radio_button_toggled, TWEETS_OTHERS_FAVORITES) fixed.put(radio_button, 10, 135) # add tweets options in the right label = gtk.Label('screen name: @') fixed.put(label, 220, 10) self.sn_entry = gtk.Entry() self.sn_entry.set_size_request(130, 20) self.sn_entry.set_sensitive(False) fixed.put(self.sn_entry, 328, 8) label = gtk.Label('Max tweets ID:') fixed.put(label, 220, 33) self.matd_entry = gtk.Entry() self.matd_entry.set_size_request(130, 20) fixed.put(self.matd_entry, 328, 33) label = gtk.Label('Min tweets ID:') fixed.put(label, 220, 58) self.mitd_entry = gtk.Entry() self.mitd_entry.set_size_request(130, 20) fixed.put(self.mitd_entry, 328, 58) label = gtk.Label('Tweets count:') fixed.put(label, 220, 83) self.tc_entry = gtk.Entry() self.tc_entry.set_size_request(130, 20) fixed.put(self.tc_entry, 328, 83) self.wc_btn = gtk.CheckButton('Without cover') self.wc_btn.connect('toggled', self.cover_button_toggle) fixed.put(self.wc_btn, 250, 118) #add generate button in the middle self.client = oauthapi.tpOAuth() if not self.client.restore_access_token(): self.start_button = gtk.Button('OAuth') self.start_button.set_sensitive(True) self.start_button.set_size_request(440, 30) self.start_btn_handler_id = self.start_button.connect('clicked', self.oauth_button_clicked) else: self.start_button = gtk.Button('Start Generate') self.start_button.set_size_request(440, 30) self.start_btn_handler_id = self.start_button.connect('clicked', self.start_button_clicked) fixed.put(self.start_button, 15, 160) self.button_in_fixed = fixed #genearate progress area frame = gtk.Frame('Progress') self.gen_panel.pack_start(frame, True, True, 5) vbox = gtk.VBox(False, 5) frame.add(vbox) self.progress_bar = gtk.ProgressBar() self.progress_bar.set_size_request(300, 50) self.progress_bar.set_text('Now Idle') vbox.pack_start(self.progress_bar, False, False, 10)
def start_button_clicked(self, widget, data= None): cfg = config.tp_cfg() if not cfg.load(): self.show_warning(''' This is the first time you run tweets2pdf. \nGo to Preference and do some settings. ''') return client = oauthapi.tpOAuth(http_proxy = { 'host': cfg.proxy_host, 'port': cfg.proxy_port } if cfg.proxy_enable else None) if not client.restore_access_token(): token = client.fetch_request_token() if token is None: return client.authorization_token(token) verifier = raw_input("please input PIN code:") token = client.fetch_access_token(token, verifier) client.save_access_token() if self.tweets_type == TWEETS_OTHERS_TIMELINE or \ self.tweets_type == TWEETS_OTHERS_FAVORITES: screen_name = self.sn_entry.get_text() if len(screen_name) == 0: self.show_warning('Please input screen name of the account') return else: screen_name = client.access_token.screen_name tweets_max_id = self.get_id_from_entry(self.matd_entry) tweets_min_id = self.get_id_from_entry(self.mitd_entry) if self.tweets_type == TWEETS_MY_TIMELINE or self.tweets_type == TWEETS_OTHERS_TIMELINE: default_name = 'tweets_%s.pdf' % screen_name elif self.tweets_type == TWEETS_RETWEETED_BY_ME: default_name = 'tweets_%s_rt.pdf' % screen_name elif self.tweets_type == TWEETS_MY_RETWEETED: default_name = 'tweets_%s_rted.pdf' % screen_name elif self.tweets_type == TWEETS_MY_FAVORITES or \ self.tweets_type == TWEETS_OTHERS_FAVORITES: default_name = 'tweets_%s_fav.pdf' % screen_name path = cfg.output_path if len(path) > 0: if path[len(path) - 1] != '/': path += '/' pdf_name = path + default_name if self.wc_btn.get_active(): has_cover = False else: has_cover = True tpdoc = tp_document(oauth_client = client, \ screen_name = screen_name, \ tweets_type = self.tweets_type, \ pdf_name = pdf_name, \ has_cover = has_cover, \ tweets_min_id = tweets_min_id, \ tweets_max_id = tweets_max_id, \ font_path = cfg.font_path, \ link_color = cfg.link_color, \ main_panel = self) tpdoc.set_tweets_count(self.get_id_from_entry(self.tc_entry)) self.start_button.set_sensitive(False) self.thread = threading.Thread(target = generate_thread, args = [[tpdoc, self]]) self.thread.setDaemon(1) self.thread.start()