コード例 #1
0
 def run(self) -> None:
     if self.in_dir:
         batch_results = BatchResults(self.in_dir)
         batch_results.unidify(
             self.settings.twitter_api, self.out_dir if self.out_dir else self.in_dir
         )
     else:
         for tweet in statuses_lookup(
             (TweetId(line.strip()) for line in sys.stdin), self.settings.twitter_api
         ):
             if tweet is not None:
                 sys.stdout.write(json.dumps(tweet.to_json()) + "\n")
コード例 #2
0
def test_unidify_fail_and_restart(
    requests: Iterable[Request],
    settings: NastySettings,
    monkeypatch: MonkeyPatch,
    tmp_path: Path,
) -> None:
    idify_dir = tmp_path / "idify"
    unidify_dir = tmp_path / "unidify"

    batch = Batch()
    for request in requests:
        batch.append(request)
    results = batch.execute()
    assert results is not None

    tweets = {
        tweet.id: tweet
        for entry in results for tweet in results.tweets(entry)
    }
    tweets_truncated = dict(tweets)
    del tweets_truncated[TweetId("1115690615612825601")]

    idified = results.idify(idify_dir)
    assert idified is not None

    monkeypatch.setattr(
        nasty.batch.batch_results,
        nasty.batch.batch_results.statuses_lookup.__name__,  # type: ignore
        _mock_statuses_lookup(tweets_truncated),
    )

    # Assert KeyError is propagated, because a Tweet is missing from tweets_truncated.
    with pytest.raises(KeyError):
        idified.unidify(settings.twitter_api, unidify_dir)
    unidified = BatchResults(unidify_dir)
    assert len(batch) > len(unidified)

    monkeypatch.setattr(
        nasty.batch.batch_results,
        nasty.batch.batch_results.statuses_lookup.__name__,  # type: ignore
        _mock_statuses_lookup(tweets),
    )

    unidified = idified.unidify(settings.twitter_api, unidify_dir)
    assert unidified is not None
    assert len(batch) == len(unidified)
    assert tweets == {
        tweet.id: tweet
        for entry in unidified for tweet in unidified.tweets(entry)
    }
コード例 #3
0
def _assert_tweet_ids(results: BatchResults) -> None:
    assert [
        TweetId("1115690002233556993"),
        TweetId("1115690615612825601"),
        TweetId("1115691710657499137"),
    ] == list(results.tweet_ids(results[0]))
コード例 #4
0
    tweets: Mapping[TweetId, Tweet]
) -> Callable[[Iterable[TweetId], TwitterApiSettings],
              Iterable[Optional[Tweet]]]:
    def statuses_lookup(
            tweet_ids: Iterable[TweetId],
            twitter_api_settings: TwitterApiSettings) -> Iterable[Tweet]:
        return (tweets[tweet_id] for tweet_id in tweet_ids)

    return statuses_lookup


@pytest.mark.parametrize(
    "requests",
    list(
        permutations([
            Replies(TweetId("1115690002233556993")),
            Replies(TweetId("1115690615612825601")),
            Replies(TweetId("1115691710657499137")),
        ])),
    ids=repr,
)
def test_unidify_fail_and_restart(
    requests: Iterable[Request],
    settings: NastySettings,
    monkeypatch: MonkeyPatch,
    tmp_path: Path,
) -> None:
    idify_dir = tmp_path / "idify"
    unidify_dir = tmp_path / "unidify"

    batch = Batch()
コード例 #5
0
def test_max_tweets(max_tweets: int) -> None:
    tweets = list(
        Thread(TweetId("1183715553057239040"),
               max_tweets=max_tweets).request())
    assert max_tweets == len(tweets)
    assert len(tweets) == len({tweet.id for tweet in tweets})
コード例 #6
0
import pytest

from nasty.request.thread import Thread
from nasty.tweet.tweet import TweetId


@pytest.mark.parametrize("max_tweets", [1, 10, 100], ids=repr)
def test_max_tweets(max_tweets: int) -> None:
    tweets = list(
        Thread(TweetId("1183715553057239040"),
               max_tweets=max_tweets).request())
    assert max_tweets == len(tweets)
    assert len(tweets) == len({tweet.id for tweet in tweets})


@pytest.mark.parametrize("tweet_id", [TweetId("1110485791279595525")],
                         ids=repr)
def test_no_thread(tweet_id: TweetId) -> None:
    assert not list(Thread(tweet_id).request())


@pytest.mark.parametrize(
    "args",
    [
        (
            TweetId("1115689254271819777"),
            [
                TweetId("1115690002233556993"),
                TweetId("1115690615612825601"),
                TweetId("1115691710657499137"),
            ],
コード例 #7
0
def test_max_tweets(max_tweets: int) -> None:
    tweets = list(
        Replies(TweetId("1096092704709070851"), max_tweets=max_tweets).request()
    )
    assert max_tweets == len(tweets)
    assert len(tweets) == len({tweet.id for tweet in tweets})
コード例 #8
0
import pytest

from nasty.request.replies import Replies
from nasty.tweet.tweet import TweetId


@pytest.mark.parametrize("max_tweets", [1, 10, 100], ids=repr)
def test_max_tweets(max_tweets: int) -> None:
    tweets = list(
        Replies(TweetId("1096092704709070851"), max_tweets=max_tweets).request()
    )
    assert max_tweets == len(tweets)
    assert len(tweets) == len({tweet.id for tweet in tweets})


@pytest.mark.parametrize("tweet_id", [TweetId("1110485791279595525")], ids=repr)
def test_no_replies(tweet_id: TweetId) -> None:
    assert not list(Replies(tweet_id).request())


@pytest.mark.parametrize(
    "args",
    [
        (
            TweetId("1115689254271819777"),
            {
                TweetId("1115690002233556993"),
                TweetId("1115947355000406016"),
                TweetId("1115692135808999424"),
                TweetId("1115903315773153280"),
                TweetId("1115692500730171392"),