From 4985ffcd40612e1723917074c2800df355d21913 Mon Sep 17 00:00:00 2001 From: Alexis Pentori Date: Mon, 21 Oct 2024 11:04:24 +0200 Subject: [PATCH] twitter: adding account endpoint Signed-off-by: Alexis Pentori --- .../sample_files/configured_catalog.json | 22 ++++++-- .../schemas/account.json | 50 +++++++++++++++++++ .../source_twitter_fetcher/source.py | 20 ++++++++ 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 source-twitter-fetcher/source_twitter_fetcher/schemas/account.json diff --git a/source-twitter-fetcher/sample_files/configured_catalog.json b/source-twitter-fetcher/sample_files/configured_catalog.json index cdc393c..8a4e3a0 100644 --- a/source-twitter-fetcher/sample_files/configured_catalog.json +++ b/source-twitter-fetcher/sample_files/configured_catalog.json @@ -1,5 +1,19 @@ { "streams": [ + { + "stream": { + "name": "account", + "json_schema": { + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object" + }, + "supported_sync_modes": [ + "full_refresh", "incremental" + ] + }, + "sync_mode": "incremental", + "destination_sync_mode": "overwrite" + }, { "stream": { "name": "tweet", @@ -8,10 +22,10 @@ "type": "object" }, "supported_sync_modes": [ - "full_refresh", "incremental" + "full_refresh", "incremental" ] }, - "sync_mode": "incremental", + "sync_mode": "incremental", "destination_sync_mode": "overwrite" }, { @@ -22,10 +36,10 @@ "type": "object" }, "supported_sync_modes": [ - "full_refresh", "incremental" + "full_refresh", "incremental" ] }, - "sync_mode": "incremental", + "sync_mode": "incremental", "destination_sync_mode": "overwrite" } ] diff --git a/source-twitter-fetcher/source_twitter_fetcher/schemas/account.json b/source-twitter-fetcher/source_twitter_fetcher/schemas/account.json new file mode 100644 index 0000000..1aefa63 --- /dev/null +++ b/source-twitter-fetcher/source_twitter_fetcher/schemas/account.json @@ -0,0 +1,50 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "id": { + "type": [ "null", "string" ] + }, + "name": { + "type": [ "null", "string" ] + }, + "username": { + "type": [ "null", "string" ] + }, + "description": { + "type": [ "null", "string" ] + }, + "most_recent_tweet_id": { + "type": [ "null", "string" ] + }, + "pinned_tweet_id": { + "type": [ "null", "string" ] + }, + "protected": { + "type": [ "null", "bool" ] + }, + "verified_type": { + "type": [ "null", "string" ] + }, + "public_metrics": { + "type": ["null", "object" ], + "properties": { + "tweet_count": { + "type": [ "null", "number" ] + }, + "like_count": { + "type": [ "null", "number" ] + }, + "following_count": { + "type": [ "null", "number" ] + }, + "follower_count": { + "type": [ "null", "number" ] + }, + "listed_count": { + "type": [ "null", "number" ] + } + } + } + } +} diff --git a/source-twitter-fetcher/source_twitter_fetcher/source.py b/source-twitter-fetcher/source_twitter_fetcher/source.py index dbf125e..6e87a32 100644 --- a/source-twitter-fetcher/source_twitter_fetcher/source.py +++ b/source-twitter-fetcher/source_twitter_fetcher/source.py @@ -39,6 +39,25 @@ class TwitterStream(HttpStream): if delay_time: return int(delay_time) +class Account(TwitterStream): + primary_key= "id" + + def path( + self, stream_state: Mapping[str, Any] = None, + stream_slice: Mapping[str, Any] = None, + next_page_token: Mapping[str, Any] = None) -> str: + return f"users/me?user.fields=public_metrics,protected,description,url,most_recent_tweet_id,pinned_tweet_id,created_at,verified_type" + + def parse_response( + self, + response: requests.Response, + stream_slice: Mapping[str, Any] = None, + **kwargs + ) -> Iterable[Mapping]: + logger.debug("Response: %s", response.json()) + data=response.json()['data'] + yield data + class Tweet(TwitterStream): primary_key = "id" @@ -144,6 +163,7 @@ class SourceTwitterFetcher(AbstractSource): parent=tweet ) return [ + Account(authenticator=auth, account_id=config["account_id"]), tweet, tweetMetrics ]