theenglishway (time) 7 лет назад
Родитель
Сommit
50f7a9f4a2
3 измененных файлов с 64 добавлено и 1 удалено
  1. 24 1
      twhatter/cli.py
  2. 1 0
      twhatter/output/__init__.py
  3. 39 0
      twhatter/output/json.py

+ 24 - 1
twhatter/cli.py

@@ -6,7 +6,7 @@ import click
 import IPython
 
 from twhatter.client import ClientTimeline, ClientProfile
-from twhatter.output import Print
+from twhatter.output import Print, Json
 from twhatter.output.sqlalchemy import Database, Tweet, User
 from twhatter.log import log_setup
 
@@ -77,5 +77,28 @@ def shell(ctx):
     ctx.obj['db'].stop(session)
 
 
+@main.group()
+@click.option('-f', '--json_file', type=str, default="/tmp/output.json", show_default=True)
+@click.pass_context
+def json(ctx, json_file):
+    ctx.obj['json'] = Json(json_file)
+
+
+@json.command()
+@click.option('-l', '--limit', type=int, default=100, show_default=True)
+@click.argument('user')
+@click.pass_context
+def timeline(ctx, limit, user):
+    """Push user's Tweets into a database"""
+    ctx.obj['json'].output_tweets(user, limit)
+
+
+@json.command()
+@click.argument('user')
+@click.pass_context
+def profile(ctx, user):
+    """Push some user into a database"""
+    ctx.obj['json'].output_user(user)
+
 if __name__ == "__main__":
     main(obj={})

+ 1 - 0
twhatter/output/__init__.py

@@ -1,2 +1,3 @@
 from .print import Print
 from .base import OutputBase
+from .json import Json

+ 39 - 0
twhatter/output/json.py

@@ -0,0 +1,39 @@
+import json
+import logging
+from datetime import datetime
+from bs4 import PageElement
+
+from .base import OutputBase
+from twhatter.client import ClientTimeline, ClientProfile
+
+
+logger = logging.getLogger(__name__)
+
+
+class TweeterEncoder(json.JSONEncoder):
+    def default(self, o):
+        if isinstance(o, datetime):
+            return o.strftime("%c %z")
+
+        if isinstance(o, PageElement):
+            return None
+
+        return o.__dict__
+
+
+class Json(OutputBase):
+    def __init__(self, json_path):
+        logger.info("Output set to {}".format(json_path))
+        self.json_path = json_path
+
+    def output_tweets(self, user, limit):
+        client_timeline = ClientTimeline(user, limit)
+
+        with open(self.json_path, 'w') as f:
+            json.dump([t for t in client_timeline], f, cls=TweeterEncoder, indent=4)
+
+    def output_user(self, user):
+        p = ClientProfile(user)
+
+        with open(self.json_path, 'w') as f:
+            json.dump(p.user, f, cls=TweeterEncoder, indent=4)