def test_no_urls(self): """ utils.urls.actual_url will not be called if the tweet contains no URLs. """ with mock.patch("utils.urls.actual_url", mocksignature=True) as m: urls.sanitize_urls("abc") self.assertEquals(0, m.call_count)
def test_single_url(self): """ utils.urls.actual_url will be called once if the tweet contains a single URL. """ data = ("http://www.example.com/1/2", "http://a.b.c/x/y/z") with mock.patch("utils.urls.actual_url", mocksignature=True) as m: m.side_effect = lambda _: data[1] r = urls.sanitize_urls("see: %s" % data[0]) self.assertEquals(1, m.call_count) self.assertEquals("see: %s" % data[1], r)
def test_multi_url(self): """ utils.urls.actual_url will be called once per URL contained in the tweet. """ data = (("http://a.com/1/2", "http://x.org/x/y"), ("http://b.com/3/4", "http://y.net/o/p")) data = dict(data) inputs = tuple(data.keys()) results = data.values() with mock.patch("utils.urls.actual_url", mocksignature=True) as m: m.side_effect = lambda _: results.pop(0) r = urls.sanitize_urls("see: %s and %s" % inputs) self.assertEquals(len(data), m.call_count) self.assertEquals("see: %s and %s" % tuple(data.values()), r)
tweets = [] try: if since_id["id"]: tweets = api.getHomeTimeline(since_id=since_id["id"], count=199) else: tweets = api.getHomeTimeline(count=199) except Exception, e: log.err(e) log.msg(len(tweets)) if tweets: for tweet in tweets: if not isinstance(tweet, dict): continue try: tweet["text"] = urls.sanitize_urls(tweet["text"]) except: pass # not much we can do here messages.insert(tweets) ids = [int(tweet["id"]) for tweet in tweets if isinstance(tweet, dict)] if ids: since_id["id"] = max(ids) maxids.save(since_id) l = task.LoopingCall(pull_and_save_tweets, api) cfg = config.get("twitter") l.start(float(cfg.get("interval", 600))) # call every ten minutes # l.stop() will stop the looping calls reactor.run()