コード例 #1
0
def main():
    '''
    This is the main method, validate args, load credentials, start the daemon.
    '''
    message = 'Listen to tweets; dump them to the queue.'
    socket_help = ('a list containing the host, port numbers to listen to; '
                   'defaults to localhost:7711 (for disque)')

    parser = ArgumentParser(description=message)
    parser.add_argument('-s',
                        '--sockets',
                        help=socket_help,
                        default=['localhost:7711'],
                        dest='sockets',
                        metavar=('HOST:PORT'),
                        nargs='+')
    parser.add_argument('-c',
                        '--channels',
                        help='Twitter accounts to follow',
                        dest='channels',
                        metavar=('CHANNEL'),
                        nargs='+',
                        required=True)
    parser.add_argument('-d',
                        '--debug',
                        help='enable debugging',
                        action='store_true',
                        default=False)

    args = vars(parser.parse_args())

    if args['debug']:
        LOGGER.setLevel(DEBUG)
        LOGGER.addHandler(HANDLER)
    else:
        LOGGER.setLevel(INFO)
        LOGGER.addHandler(HANDLER)

    try:
        # Connect to the redis-queue.
        queue = Client(args['sockets'])
        queue.connect()
        LOGGER.info('[start-daemon]')
        queue_info = json.dumps(queue.info(), indent=4)
        LOGGER.debug('[queue-init]\n%s', queue_info)

        # Load credentials, initialize authentication module, listen to tweets.
        api = load_credentials()
        if not api:
            LOGGER.error('[load_credentials] unable to load credentials!')
            return

        listener = StreamDaemon(queue)
        streamer = tweepy.Stream(auth=api.auth, listener=listener)
        args['channels'] = [re.sub('@', '', _) for _ in args['channels']]
        streamer.userstream(track=args['channels'])

    except Exception:
        LOGGER.error('[error] unknown error')
        LOGGER.error('[error] unable to connect to the redis-queue (disque)!')

    except KeyboardInterrupt:
        LOGGER.critical('[stop-daemon]')
    return
コード例 #2
0
def main():
    '''
    Validate arguments; send data to the message bus.
    '''
    message = 'Push data to the message bus.'
    socket_help = ('a list containing the host, port numbers to listen to; '
                   'defaults to localhost:7711 (for disque)')
    ttl_help = ('a TTL (in seconds) for the data on Twitter and GitHub; '
                'if not specified, the data will remain forever')

    parser = ArgumentParser(description=message)
    parser.add_argument('-s',
                        '--sockets',
                        help=socket_help,
                        default=['localhost:7711'],
                        dest='sockets',
                        metavar=('HOST:PORT'),
                        nargs='+')
    parser.add_argument('-d',
                        '--debug',
                        help='enable debugging',
                        action='store_true',
                        default=False)
    parser.add_argument('-r',
                        '--recipient',
                        help='keybase-id to send',
                        required=True,
                        metavar=('KEYBASE-ID'))
    parser.add_argument('-t',
                        '--ttl',
                        help=ttl_help,
                        default=0,
                        type=int,
                        metavar=('N'))
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('-i', '--in-file', metavar=('FILE'), default=None)
    group.add_argument('-m', '--message', type=str)

    args = vars(parser.parse_args())

    if args['debug']:
        LOGGER.setLevel(DEBUG)
        LOGGER.addHandler(HANDLER)
    else:
        LOGGER.setLevel(INFO)
        LOGGER.addHandler(HANDLER)

    plaintext, queue = None, None

    if args['in_file']:
        name = args['in_file']
        if not re.match(r'^text\/.*', magic.from_file(name, mime=True)):
            LOGGER.error('[file-error] input-file mimetype should be text/.*')
            return
        else:
            plaintext = open(name, 'r').read()
    else:
        plaintext = args['message']

    try:
        # Instantiate a connection to the queue only if a TTL is specified.
        if args['ttl']:
            queue = Client(args['sockets'])
            queue.connect()
            queue_info = json.dumps(queue.info(), indent=4)
            LOGGER.debug('[queue-init]\n%s', queue_info)

        auth = load_credentials()
        if None in auth:
            LOGGER.error('[load_credentials] unable to load credentials!')
            return

        send(plaintext=plaintext,
             auth=auth,
             recipient=args['recipient'],
             ttl=args['ttl'],
             queue=queue,
             debug=args['debug'])

    except Exception:
        LOGGER.error('[error] unable to connect to the redis-queue (disque)!')
コード例 #3
0
def main():
    '''
    Initialize authentication, client connection.
    '''
    message = 'Delete gists, tweets if a TTL is set.'
    socket_help = ('a list containing the host, port numbers to listen to; '
                   'defaults to localhost:7711 (for disque)')
    retry_help = 'queue check frequncy (in seconds); defaults to 8'

    parser = ArgumentParser(description=message)
    parser.add_argument('-s',
                        '--sockets',
                        help=socket_help,
                        default=['localhost:7711'],
                        dest='sockets',
                        metavar=('HOST:PORT'),
                        nargs='+')
    parser.add_argument('-d',
                        '--debug',
                        help='enable debugging',
                        action='store_true',
                        default=False)
    parser.add_argument('-r',
                        '--retry',
                        help=retry_help,
                        default=8,
                        type=int,
                        metavar=('DELAY'))

    args = vars(parser.parse_args())

    if args['debug']:
        LOGGER.setLevel(DEBUG)
        LOGGER.addHandler(HANDLER)
    else:
        LOGGER.setLevel(INFO)
        LOGGER.addHandler(HANDLER)

    # Load the credentials.
    tokens = load_credentials()

    if None in tokens:
        LOGGER.error('[load_credentials] unable to load credentials!')
        return

    try:
        # Connect to the redis-queue.
        queue = Client(args['sockets'])
        queue.connect()
        LOGGER.info('[start-daemon]')
        queue_info = json.dumps(queue.info(), indent=4)
        LOGGER.debug('[queue-init]\n%s', queue_info)
        listen(queue, tokens, args['debug'], args['retry'])

    except Exception:
        LOGGER.error('[error] unable to connect to the redis-queue (disque)!')

    except KeyboardInterrupt:
        LOGGER.critical('[stop-daemon]')

    return