Exemple #1
0
def main():
    arg_parser = argparse.ArgumentParser(
        description='Runs CompFacts Twitter status updater.')
    arg_parser.add_argument('--log-dir', help='Logging directory')
    arg_parser.add_argument('--config', help='Config file path',
        default='/etc/compfacts.conf')
    arg_parser.add_argument('--simulate', help='Simulate posting',
        action='store_true', default=False)
    arg_parser.add_argument('database', help='Database path')

    args = arg_parser.parse_args()

    if args.log_dir:
        if not os.path.exists(args.log_dir):
            os.makedirs(args.log_dir)

        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG)
        handler = logging.handlers.RotatingFileHandler(os.path.join(
            args.log_dir, 'compfacts.log'), maxBytes=4194304, backupCount=9)
        logger.addHandler(handler)
        formatter = logging.Formatter(
            '%(asctime)s %(name)s %(funcName)s:%(lineno)d %(levelname)s '
            '%(message)s')
        handler.setFormatter(formatter)

    config_parser = ConfigParser.ConfigParser()
    config_parser.read([args.config])

    database = Database(args.database)
    fact_builder = FactBuilder()
    sched_kwargs = {}

    if args.simulate:
        api_service = NullAPIService()
        sched_kwargs['interval'] = 30
        sched_kwargs['deviation'] = 5
        sched_kwargs['retry_delay'] = 5
    else:
        consumer_key = config_parser.get('twitter', 'consumer_key')

        if consumer_key == u'TODO':
            raise Exception('Please set consumer key')

        consumer_secret = config_parser.get('twitter', 'consumer_secret')
        access_token = config_parser.get('twitter', 'access_token')
        access_token_secret = config_parser.get('twitter',
            'access_token_secret')
        api_service = TwitterAPIService(consumer_key, consumer_secret,
            access_token, access_token_secret)

    post_sched = PostScheduler(fact_builder, database, api_service,
        **sched_kwargs)

    while True:
        try:
            post_sched.join(timeout=1)
        except KeyboardInterrupt:
            break
Exemple #2
0
def run_web_server():
    arg_parser = argparse.ArgumentParser(
        description='CompFacts info web server.')
    arg_parser.add_argument('database', help='Database path')

    args = arg_parser.parse_args()

    app = Application(Database(args.database))
    app.listen(8796, 'localhost', xheaders=True)
    tornado.ioloop.IOLoop.instance().start()
Exemple #3
0
    def test_database(self):
        '''It should insert and get facts'''

        database = Database(self.temp_dir + '/test.db')

        self.assertFalse(database.get_last_timestamp())

        database.insert_fact(u'kittens')
        database.insert_fact(u'hi')
        database.insert_fact(u'hello')
        facts = database.get_facts()

        self.assertTrue(facts)
        self.assertEqual(facts[0][1], u'hello')
        self.assertEqual(facts[1][1], u'hi')
        self.assertEqual(facts[2][1], u'kittens')

        self.assertAlmostEqual(database.get_last_timestamp(), time.time(),
            delta=2.0)
        self.assertGreaterEqual(facts[0][0], facts[1][0])
Exemple #4
0
class TestPostScheduler(unittest.TestCase):
    def setUp(self):
        self.temp_dir = tempfile.mkdtemp()
        self.db = Database(self.temp_dir + '/test.db')

    def tearDown(self):
        shutil.rmtree(self.temp_dir)

    def test_post(self):
        event = threading.Event()
        api_service = MockAPIService()
        fact_builder = FactBuilder()

        api_service.texts = []
        api_service.tested_error = False

        def post_message(text):
            if len(api_service.texts) == 1 and not api_service.tested_error:
                api_service.tested_error = True
                raise Exception('testing')

            api_service.texts.append(text)

            if len(api_service.texts) > 4:
                event.set()
                post_sched.stop()

        api_service.post_message = post_message

        post_sched = PostScheduler(fact_builder, self.db, api_service,
            interval=0.2, deviation=0.01, retry_delay=0.1)

        event.wait(timeout=5)
        post_sched.stop()
        post_sched.join(timeout=1)

        self.assertEqual(len(api_service.texts), 5)
        self.assertEqual(len(self.db.get_facts()), 5)
Exemple #5
0
 def setUp(self):
     self.temp_dir = tempfile.mkdtemp()
     self.db = Database(self.temp_dir + '/test.db')