update list memberships to use expansions.py
@@ -21,6 +21,7 @@ from twarc.expansions import (
MEDIA_FIELDS,
POLL_FIELDS,
PLACE_FIELDS,
+ LIST_FIELDS,
ensure_flattened,
)
from twarc.decorators2 import *
@@ -156,6 +157,13 @@ class Twarc2:
else ",".join(PLACE_FIELDS)
)
+ if "list_fields" in kwargs:
+ params["list.fields"] = (
+ kwargs.pop("list_fields")
+ if kwargs["list_fields"]
+ else ",".join(LIST_FIELDS)
+ )
+
# Format start_time and end_time
if "start_time" in kwargs:
start_time = kwargs["start_time"]
@@ -296,46 +304,53 @@ class Twarc2:
def list_memberships(
self,
- id,
+ user,
expansions=None,
list_fields=None,
max_results=None,
pagination_token=None,
- user_field=None
+ user_fields=None,
):
"""
- Function allows to get all the membership list from an specific user ID
+ Returns all Lists a specified user is a member of.
- Calls [GET /2/users/:id/list_memberships](https://developer.twitter.com/en/docs/twitter-api/lists/list-members/introduction)
+ Calls [GET /2/users/:id/list_memberships](https://developer.twitter.com/en/docs/twitter-api/lists/list-members/api-reference/get-users-id-list_memberships)
Args:
+ user (int): ID of the user.
expansions enum (owner_id): enable you to request additional data objects that relate to the originally returned List.
- list.fields enum (created_at, follower_count, member_count, private, description, owner_id): This fields parameter enables you to select which specific List fields will deliver with each returned List objects.
+ list_fields enum (created_at, follower_count, member_count, private, description, owner_id): This fields parameter enables you to select which specific List fields will deliver with each returned List objects.
max_results (int): The maximum number of results to be returned per page. This can be a number between 1 and 100.
pagination_token (string): Used to request the next page of results if all results weren't returned with the latest request, or to go back to the previous page of results.
- user.fields( enum (created_at, description, entities, id, location, name, pinned_tweet_id, profile_image_url, protected, public_metrics, url, username, verified, withheld):
+ user_fields enum (created_at, description, entities, id, location, name, pinned_tweet_id, profile_image_url, protected, public_metrics, url, username, verified, withheld):
This fields parameter enables you to select which specific user fields will deliver with the users object. Specify the desired fields in a comma-separated list without spaces between commas and fields.
- """
- user_id = self._ensure_user_id(id)
+ Returns:
+ generator[dict]: A generator, dict for each page of results.
+ """
+ user_id = self._ensure_user_id(user)
url = f"https://api.twitter.com/2/users/{user_id}/list_memberships"
params = self._prepare_params(
list_fields=list_fields,
max_results=max_results,
pagination_token=pagination_token,
- user_field=user_field
+ user_fields=user_fields,
)
if expansions:
params["expansions"] = "owner_id"
- resp = self.get(url, params=params)
- data = resp.json()
-
- return data
-
-
+ count = 0
+ for response in self.get_paginated(url, params=params):
+ # can return without 'data' if there are no results
+ if "data" in response:
+ count += len(response["data"])
+ yield response
+ else:
+ log.info(
+ f"Retrieved an empty page of results for list memberships of {user_id}"
+ )
def search_recent(
self,
