1
0
mirror of https://github.com/balkian/bitter.git synced 2024-12-22 08:28:12 +00:00

Fixes for serialize

Some commands in the CLI did not properly include serialize (get_tweet,
get_users and search)
This commit is contained in:
J. Fernando Sánchez 2020-01-07 19:36:18 +01:00
parent 80b58541e7
commit bba73091e4
3 changed files with 9 additions and 8 deletions

View File

@ -1 +1 @@
0.10.0 0.10.1

View File

@ -36,7 +36,7 @@ def serialize(function):
@click.option('--csv', help='Print each object as a csv row. Provide a list of comma-separated fields to print.', default='', type=str) @click.option('--csv', help='Print each object as a csv row. Provide a list of comma-separated fields to print.', default='', type=str)
@click.option('--header', help='Header that will be printed at the beginning of the file', default=None) @click.option('--header', help='Header that will be printed at the beginning of the file', default=None)
@click.option('--json', '--jsonlines', help='Print each object as JSON in a new line.', is_flag=True) @click.option('--jsonlines', '--json', help='Print each object as JSON in a new line.', is_flag=True)
@click.option('--indented', help='Print each object as an indented JSON object', is_flag=True) @click.option('--indented', help='Print each object as an indented JSON object', is_flag=True)
@click.option('--outfile', help='Output file. It defaults to STDOUT', default=sys.stdout) @click.option('--outfile', help='Output file. It defaults to STDOUT', default=sys.stdout)
def decorated(csv, header, jsonlines, indented, outfile, **kwargs): def decorated(csv, header, jsonlines, indented, outfile, **kwargs):
@ -48,10 +48,11 @@ def serialize(function):
def do(out): def do(out):
if csv: if csv:
writer = tsv.writer(out, quoting=tsv.QUOTE_ALL, delimiter='\t') delimiter = '\t'
writer = tsv.writer(out, quoting=tsv.QUOTE_ALL, delimiter=delimiter)
if header is None: if header is None:
# Print fields as header unless told otherwise # Print fields as header unless told otherwise
print(csv, file=out) print(csv.replace(',', delimiter), file=out)
fields = list(token.strip().split('.') for token in csv.split(',')) fields = list(token.strip().split('.') for token in csv.split(','))
for obj in it: for obj in it:
writer.writerow(list(reduce(operator.getitem, field, obj) for field in fields)) writer.writerow(list(reduce(operator.getitem, field, obj) for field in fields))
@ -212,11 +213,11 @@ def get_tweets(ctx, tweetsfile, folder, update, retry, delimiter, skip, quotecha
@tweet.command('search') @tweet.command('search')
@click.argument('query') @click.argument('query')
@serialize
@click.pass_context @click.pass_context
def search(ctx, query): def search(ctx, query):
wq = crawlers.TwitterQueue.from_config(conffile=bconf.CONFIG_FILE) wq = crawlers.TwitterQueue.from_config(conffile=bconf.CONFIG_FILE)
t = utils.search_tweet(wq, query) yield from utils.search_tweet(wq, query)
print(json.dumps(t, indent=2))
@tweet.command('timeline') @tweet.command('timeline')
@click.argument('user') @click.argument('user')
@ -277,8 +278,8 @@ def get_user(user, write, folder, update):
@click.option('-q', '--quotechar', default='"') @click.option('-q', '--quotechar', default='"')
@click.option('--commentchar', help='Lines starting with this character will be ignored', default=None) @click.option('--commentchar', help='Lines starting with this character will be ignored', default=None)
@click.option('-c', '--column', type=int, default=0) @click.option('-c', '--column', type=int, default=0)
@click.pass_context
@serialize @serialize
@click.pass_context
def get_users(ctx, usersfile, folder, update, retry, delimiter, skip, quotechar, commentchar, column): def get_users(ctx, usersfile, folder, update, retry, delimiter, skip, quotechar, commentchar, column):
if update and not click.confirm('This may overwrite existing users. Continue?'): if update and not click.confirm('This may overwrite existing users. Continue?'):
click.echo('Cancelling') click.echo('Cancelling')

View File

@ -459,7 +459,7 @@ def get_tweet(c, tid):
return c.statuses.show(id=tid) return c.statuses.show(id=tid)
def search_tweet(c, query): def search_tweet(c, query):
return c.search.tweets(q=query) yield from c.search.tweets(q=query)['statuses']
def user_timeline(c, query): def user_timeline(c, query):
try: try: