def main(): inifile = configparser.ConfigParser() inifile.read('./user.conf') init = { 'consumer_key': inifile.get('twitter', 'consumer_key'), 'consumer_secret': inifile.get('twitter', 'consumer_secret'), 'access_key': inifile.get('twitter', 'access_key'), 'access_secret': inifile.get('twitter', 'access_secret'), } github_user = { 'repo_owner': inifile.get('github', 'repo_owner'), 'repo_name': inifile.get('github', 'repo_name'), 'username': inifile.get('github', 'username'), 'password': inifile.get('github', 'password'), } auth = get_oauth(init) stream = Stream(auth, AbstractedlyListener(github_user=github_user), secure=True) error = 0 while True: try: stream.userstream() except: error += 1 print(error, traceback.format_exc())
class TweepyStreamTests(unittest.TestCase): def setUp(self): self.auth = create_auth() self.listener = MockStreamListener(self) self.stream = Stream(self.auth, self.listener, timeout=3.0) def tearDown(self): self.stream.disconnect() def test_userstream(self): # Generate random tweet which should show up in the stream. def on_connect(): API(self.auth).update_status(mock_tweet()) self.listener.connect_cb = on_connect self.listener.status_stop_count = 1 self.stream.userstream() self.assertEqual(self.listener.status_count, 1) def test_sample(self): self.listener.status_stop_count = 10 self.stream.sample() self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_filter_track(self): self.listener.status_stop_count = 5 phrases = ['twitter'] self.stream.filter(track=phrases) self.assertEquals(self.listener.status_count, self.listener.status_stop_count)
class TweepyStreamTests(unittest.TestCase): def setUp(self): self.auth = create_auth() self.listener = MockStreamListener(self) self.stream = Stream(self.auth, self.listener, timeout=3.0) def tearDown(self): self.stream.disconnect() def test_userstream(self): # Generate random tweet which should show up in the stream. def on_connect(): API(self.auth).update_status(mock_tweet()) self.listener.connect_cb = on_connect self.listener.status_stop_count = 1 self.stream.userstream() self.assertEqual(self.listener.status_count, 1) def test_userstream_with_params(self): # Generate random tweet which should show up in the stream. def on_connect(): API(self.auth).update_status(mock_tweet()) self.listener.connect_cb = on_connect self.listener.status_stop_count = 1 self.stream.userstream(_with='user', replies='all', stall_warnings=True) self.assertEqual(self.listener.status_count, 1) def test_sample(self): self.listener.status_stop_count = 10 self.stream.sample() self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_filter_track(self): self.listener.status_stop_count = 5 phrases = ['twitter'] self.stream.filter(track=phrases) self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_track_encoding(self): s = Stream(None, None) s._start = lambda async: None s.filter(track=[u'Caf\xe9']) # Should be UTF-8 encoded self.assertEqual(u'Caf\xe9'.encode('utf8'), s.session.params['track']) def test_follow_encoding(self): s = Stream(None, None) s._start = lambda async: None s.filter(follow=[u'Caf\xe9']) # Should be UTF-8 encoded self.assertEqual(u'Caf\xe9'.encode('utf8'), s.session.params['follow'])
class TweepyStreamTests(unittest.TestCase): def setUp(self): self.auth = create_auth() self.listener = MockStreamListener(self) self.stream = Stream(self.auth, self.listener, timeout=3.0) def tearDown(self): self.stream.disconnect() def test_userstream(self): # Generate random tweet which should show up in the stream. def on_connect(): API(self.auth).update_status(mock_tweet()) self.listener.connect_cb = on_connect self.listener.status_stop_count = 1 self.stream.userstream() self.assertEqual(self.listener.status_count, 1) def test_userstream_with_params(self): # Generate random tweet which should show up in the stream. def on_connect(): API(self.auth).update_status(mock_tweet()) self.listener.connect_cb = on_connect self.listener.status_stop_count = 1 self.stream.userstream(_with='user', replies='all', stall_warnings=True) self.assertEqual(self.listener.status_count, 1) def test_sample(self): self.listener.status_stop_count = 10 self.stream.sample() self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_filter_track(self): self.listener.status_stop_count = 5 phrases = ['twitter'] self.stream.filter(track=phrases) self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_track_encoding(self): s = Stream(None, None) s._start = lambda async: None s.filter(track=[u'Caf\xe9']) # Should be UTF-8 encoded self.assertEqual(u'Caf\xe9'.encode('utf8'), s.parameters['track']) def test_follow_encoding(self): s = Stream(None, None) s._start = lambda async: None s.filter(follow=[u'Caf\xe9']) # Should be UTF-8 encoded self.assertEqual(u'Caf\xe9'.encode('utf8'), s.parameters['follow'])
def main(): logging.info("bot starting") bot = Bot() logging.info("markov data loading") if load_csv == True: markov = MarkovGenerator() logging.info("load_csv == True") csv_tweets = Bot.load_csv_tweets("./tweets.csv") #newest_tweet_idを保存 newest_tweet = csv_tweets.next() save_data(newest_tweet[0], "newest_tweet_id") markov.add_from_sentence(newest_tweet[7]) #読み込み for tweet in csv_tweets: markov.add_from_sentence(tweet) markov.save_dictionary() else: markov = MarkovGenerator(markov_dictionary=load_data("markov_dictionary"), reversed_markov_dictionary=load_data("reversed_markov_dictionary")) # 20分ごとの定期ツイート #multiprocess間での値のやり取り用変数 logging.info("regular tweet starting") p = Process(target=regular_tweet, args=(bot, markov)) p.start() # userstreamによるTL監視(リプライ・ふぁぼ・パクツイ・RT) logging.info("start timeline watching") stream = Stream(bot.auth, AbstractedlyListener(), secure=True) user_stream = stream.userstream() user_stream.start()
class TweepyStreamTests(unittest.TestCase): def setUp(self): self.auth = create_auth() self.listener = MockStreamListener(self) self.stream = Stream(self.auth, self.listener, timeout=3.0) def tearDown(self): self.stream.disconnect() def test_userstream(self): # Generate random tweet which should show up in the stream. def on_connect(): API(self.auth).update_status(mock_tweet()) self.listener.connect_cb = on_connect self.listener.status_stop_count = 1 self.stream.userstream() self.assertEqual(self.listener.status_count, 1) def test_sample(self): self.listener.status_stop_count = 10 self.stream.sample() self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_filter_track(self): self.listener.status_stop_count = 5 phrases = ['twitter'] self.stream.filter(track=phrases) self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_on_data(self): test_wrong_data = [ '{"disc', # this is actual data read from twitter '600', # this is actual data read from twitter '41\n', # this is actual data read from twitter 'obviously non-json', '"json but not dict"', '{"json dict":"but not a twitter message"}' ] for raw_data in test_wrong_data: # should log errors but not raise / not return False self.assertEquals(self.listener.on_data(raw_data), None) self.assertEquals(self.listener.status_count, 0)
class Crawler(): ''' The global crawler manager ''' def __init__(self, name): self.name = name self.regexp = re.compile(r'@%s\s+' % (self.name), re.IGNORECASE) self._start_stream() def _auth(self): self.auth = OAuthHandler(Ukey, Usecret) self.auth.set_access_token(Akey, Asecret) self.api = API(self.auth) def _start_stream(self): self.listener = Streamer(self) self._auth() self.stream = Stream(self.auth, self.listener) self.stream.userstream() def on_request(self, text, author): text = re.sub(self.regexp, '', text) text = re.sub(r'\s+$', '', text) lang = get_language(text, key=u'startword') if lang is not None: core = Core(lang) self.send_message(core.send_message(), author) def send_message(self, text, target=None): if(target is not None): text = u'@%s %s' % (target, text) print(text) print '>>>', len(text) self.api.update_status(text)
self.output_filenames = output_filenames self.OpenOutputFiles() def ReloadOutputFiles(self): self.CloseOutputFiles() self.OpenOutputFiles() """ Let's stare abstractedly at the User Streams ! """ def on_status(self, status): dump_text = json.dumps(status._json) + "\n" if self.output_files is None: print dump_text sys.stdout.flush() return for file in self.output_files: file.write(dump_text) file.flush() listener = Listener() def SigHupHandler(signum, frame): listener.ReloadOutputFiles() if __name__ == '__main__': signal.signal(signal.SIGHUP, SigHupHandler) auth = get_oauth() listener.SetOutputFilenames(sys.argv[1:]) stream = Stream(auth, listener, secure=True) daemon.daemonize(PID_FILE_NAME) stream.userstream()
filename = '/home/pi/sif/cap/manage_sif/icon' + str( num) + '.gif' command = "wget '" + str(member_url) + "' -O " + filename check = commands.getoutput(command) # print check if dobu == 1: tweet = tweet + 'ド(・8・)ブ' tweet = tweet + "#スクフェス" print "tweet:" + tweet icons = [] for i in range(0, 11): icons.insert( i, "/home/pi/sif/cap/manage_sif/icon" + str(i) + ".gif") self.useCV.create_gacha_image(icons) self.useTwitter.post_tweet_image( status.id, tweet, '/home/pi/sif/cap/manage_sif/result.jpg') if __name__ == '__main__': auth = get_oauth() stream = Stream(auth, AbstractedlyListener(), secure=True) # stream.timeout = None stream.userstream() print "gatcha.py end\n"
def main(): auth = GetOauth() stream = Stream(auth, AbstractedlyListener(), secure=True) stream.userstream()