Explorar el Código

Some renaming

theenglishway (time) hace 7 años
padre
commit
2d5db0e897
Se han modificado 6 ficheros con 28 adiciones y 36 borrados
  1. 2 2
      tests/conftest.py
  2. 7 7
      tests/test_cli.py
  3. 3 3
      twhatter/__main__.py
  4. 11 13
      twhatter/cli.py
  5. 4 9
      twhatter/api.py
  6. 1 2
      twhatter/parser/user.py

+ 2 - 2
tests/conftest.py

@@ -4,7 +4,7 @@ from datetime import datetime
 from click.testing import CliRunner
 from bs4 import BeautifulSoup
 
-from twhatter.api import ApiUser
+from twhatter.client import ClientTimeline
 from twhatter.parser import tweet_factory
 from typing import NamedTuple, List
 
@@ -117,7 +117,7 @@ def tweet_collection():
 @pytest.fixture(scope="session")
 def raw_html_user_initial_page_factory():
     def _raw_html_user_initial_page(user):
-        a = ApiUser(user)
+        a = ClientTimeline(user)
         response = a.get_initial()
         return BeautifulSoup(response.text, "lxml")
     return _raw_html_user_initial_page

+ 7 - 7
tests/test_cli.py

@@ -17,10 +17,10 @@ def test_command_line_interface(cli_runner):
     assert 'Show this message and exit.' in help_result.output
 
 
-class TestOwn:
+class TestMain:
     @pytest.mark.send_request
-    def test_no_limit(self, cli_runner, user):
-        result = cli_runner.invoke(cli.main, ['own', user])
+    def test_timeline_no_limit(self, cli_runner, user):
+        result = cli_runner.invoke(cli.main, ['timeline', user])
         assert result.exit_code == 0
 
         lines = result.output.split('\n')[:-1]
@@ -30,8 +30,8 @@ class TestOwn:
             assert "Tweet" in l
 
     @pytest.mark.send_request
-    def test_limit(self, cli_runner, user, tweet_limit):
-        result = cli_runner.invoke(cli.main, ['--limit', tweet_limit, 'own', user])
+    def test_timeline_limit(self, cli_runner, user, tweet_limit):
+        result = cli_runner.invoke(cli.main, ['timeline', user, '--limit', tweet_limit])
         assert result.exit_code == 0
 
         lines = result.output.split('\n')[:-1]
@@ -40,6 +40,6 @@ class TestOwn:
 
 class TestDb:
     @pytest.mark.send_request
-    def test_no_limit(self, cli_runner, user):
-        result = cli_runner.invoke(cli.main, ['db', 'own', user])
+    def test_timeline_no_limit(self, cli_runner, user):
+        result = cli_runner.invoke(cli.main, ['db', 'timeline', user])
         assert result.exit_code == 0

+ 3 - 3
twhatter/__main__.py

@@ -1,9 +1,9 @@
-from twhatter.api import ApiUser
+from twhatter.client import ClientTimeline
 from twhatter.output import Print
 
 
 user = "the_english_way"
-a = ApiUser(user)
+timeline = ClientTimeline(user)
 
-for t in a.iter_tweets():
+for t in timeline:
     Print(t)()

+ 11 - 13
twhatter/cli.py

@@ -5,28 +5,25 @@
 import click
 import IPython
 
-from twhatter.api import ApiUser
+from twhatter.client import ClientTimeline
 from twhatter.output import Database, Tweet
 
 
 @click.group()
-@click.option('-l', '--limit', type=int, default=100, show_default=True)
 @click.pass_context
-def main(ctx, limit):
+def main(ctx):
     ctx.ensure_object(dict)
 
-    ctx.obj['limit'] = limit
-
 
 @main.command()
+@click.option('-l', '--limit', type=int, default=100, show_default=True)
 @click.argument('user')
-@click.pass_context
-def own(ctx, user):
+def timeline(limit, user):
     """Get some user's Tweets"""
-    a = ApiUser(user)
+    timeline = ClientTimeline(user)
 
-    for n, t in enumerate(a.iter_tweets()):
-        if n >= ctx.obj['limit']:
+    for n, t in enumerate(timeline):
+        if n >= limit:
             break
 
         click.echo(t)
@@ -40,14 +37,15 @@ def db(ctx, db_url):
 
 
 @db.command()
+@click.option('-l', '--limit', type=int, default=100, show_default=True)
 @click.argument('user')
 @click.pass_context
-def own(ctx, user):
+def timeline(ctx, limit, user):
     """Push user's Tweets into a database"""
-    a = ApiUser(user)
+    timeline = ClientTimeline(user)
 
     tweets = [
-        Tweet.from_raw(t) for n, t in enumerate(a.iter_tweets()) if n < ctx.obj['limit']
+        Tweet.from_raw(t) for n, t in enumerate(timeline) if n < limit
     ]
     ctx.obj['db'].add_all(*tweets)
 

+ 4 - 9
twhatter/api.py

@@ -6,7 +6,7 @@ from twhatter.parser import TweetList
 import json
 
 
-class Api():
+class Client():
     HEADERS_LIST = [
         'Mozilla/5.0 (Windows; U; Windows NT 6.1; x64; fr; rv:1.9.2.13) Gecko/20101203 Firebird/3.6.13',
         'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko',
@@ -15,14 +15,9 @@ class Api():
         'Mozilla/5.0 (Windows NT 5.2; RW; rv:7.0a1) Gecko/20091211 SeaMonkey/9.23a1pre'
     ]
 
-    def get_initial(self):
-        raise NotImplementedError()
-
-    def get_more_tweets(self):
-        raise NotImplementedError()
-
 
-class ApiUser(Api):
+class ClientTimeline(Client):
+    """Access and explore some user's timeline"""
     def __init__(self, user, limit=100):
         self.user = user
         self.earliest_tweet = None
@@ -47,7 +42,7 @@ class ApiUser(Api):
             headers={'User-Agent': choice(self.HEADERS_LIST)}
         )
 
-    def iter_tweets(self):
+    def __iter__(self):
         tweets = self.get_initial()
         soup = BeautifulSoup(tweets.text, "lxml")
         t_list = TweetList(soup)

+ 1 - 2
twhatter/parser/user.py

@@ -1,8 +1,7 @@
 from datetime import datetime
 
 from bs4 import BeautifulSoup
-from dataclasses import dataclass, fields, InitVar, field
-from typing import List
+from dataclasses import dataclass, fields, InitVar
 
 from .mixins import ExtractableMixin