cli.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. """Console script for twhatter."""
  4. import click
  5. import IPython
  6. from twhatter.client import ClientTimeline, ClientProfile
  7. from twhatter.output import Database, Tweet
  8. @click.group()
  9. @click.pass_context
  10. def main(ctx):
  11. ctx.ensure_object(dict)
  12. @main.command()
  13. @click.option('-l', '--limit', type=int, default=100, show_default=True)
  14. @click.argument('user')
  15. def timeline(limit, user):
  16. """Get some user's Tweets"""
  17. timeline = ClientTimeline(user)
  18. for n, t in enumerate(timeline):
  19. if n >= limit:
  20. break
  21. click.echo(t)
  22. @main.command()
  23. @click.argument('user')
  24. def profile(user):
  25. """Get basic info about some user"""
  26. p = ClientProfile(user)
  27. click.echo(p.user)
  28. @main.group()
  29. @click.option('-d', '--db_url', type=str, default="sqlite:////tmp/db.sqlite3", show_default=True)
  30. @click.pass_context
  31. def db(ctx, db_url):
  32. ctx.obj['db'] = Database(db_url)
  33. @db.command()
  34. @click.option('-l', '--limit', type=int, default=100, show_default=True)
  35. @click.argument('user')
  36. @click.pass_context
  37. def timeline(ctx, limit, user):
  38. """Push user's Tweets into a database"""
  39. timeline = ClientTimeline(user)
  40. tweets = [
  41. Tweet.from_raw(t) for n, t in enumerate(timeline) if n < limit
  42. ]
  43. ctx.obj['db'].add_all(*tweets)
  44. @db.command()
  45. @click.pass_context
  46. def shell(ctx):
  47. session = ctx.obj['db'].start()
  48. user_ns = {
  49. 'db': ctx.obj['db'],
  50. 'session': session,
  51. 'Tweet': Tweet
  52. }
  53. IPython.start_ipython(argv=[], user_ns=user_ns)
  54. ctx.obj['db'].stop(session)
  55. if __name__ == "__main__":
  56. main(obj={})