Przeglądaj źródła

Stop client loop as early as possible

theenglishway (time) 7 lat temu
rodzic
commit
e4a4d93197
3 zmienionych plików z 13 dodań i 8 usunięć
  1. 8 2
      tests/test_cli.py
  2. 3 6
      twhatter/cli.py
  3. 2 0
      twhatter/client.py

+ 8 - 2
tests/test_cli.py

@@ -26,7 +26,10 @@ class TestMain:
         )
         assert result.exit_code == 0
 
-        lines = result.output.split('\n')[:-1]
+        # Remove log lines
+        lines = [
+            l for l in result.output.split('\n')[:-1] if "twhatter" not in l
+        ]
         assert len(lines) == 100
 
         for l in lines:
@@ -40,7 +43,10 @@ class TestMain:
         )
         assert result.exit_code == 0
 
-        lines = result.output.split('\n')[:-1]
+        # Remove log lines
+        lines = [
+            l for l in result.output.split('\n')[:-1] if "twhatter" not in l
+        ]
         assert len(lines) == tweet_limit
 
     @pytest.mark.send_request

+ 3 - 6
twhatter/cli.py

@@ -25,12 +25,9 @@ def main(ctx, verbosity):
 @click.argument('user')
 def timeline(limit, user):
     """Get some user's Tweets"""
-    timeline = ClientTimeline(user)
-
-    for n, t in enumerate(timeline):
-        if n >= limit:
-            break
+    timeline = ClientTimeline(user, limit)
 
+    for t in timeline:
         click.echo(t)
 
 
@@ -55,7 +52,7 @@ def db(ctx, db_url):
 @click.pass_context
 def timeline(ctx, limit, user):
     """Push user's Tweets into a database"""
-    timeline = ClientTimeline(user)
+    timeline = ClientTimeline(user, limit)
 
     tweets = [
         Tweet.from_raw(t) for n, t in enumerate(timeline) if n < limit

+ 2 - 0
twhatter/client.py

@@ -61,6 +61,8 @@ class ClientTimeline(Client):
         for t in t_list:
             yield t
             self._update_state(t)
+            if self.nb_tweets >= self.limit:
+                break
 
         while True and self.nb_tweets < self.limit:
             more_tweets = self.get_more_tweets()