-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add v1.1 feature for favourites entry point #517
Draft
CharleoY
wants to merge
1
commit into
DocNow:main
Choose a base branch
from
CharleoY:Add_Likes_entrypoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -328,6 +328,81 @@ def timeline( | |
|
||
max_id = str(int(status["id_str"]) - 1) | ||
|
||
def likes( | ||
self, user_id=None, screen_name=None, max_id=None, since_id=None, max_pages=None | ||
): | ||
""" | ||
Returns a collection of the most recent tweets posted | ||
by the user indicated by the user_id or screen_name parameter. | ||
Provide a user_id or screen_name. | ||
""" | ||
|
||
if user_id and screen_name: | ||
raise ValueError("only user_id or screen_name may be passed") | ||
|
||
# Strip if screen_name is prefixed with '@' | ||
if screen_name: | ||
screen_name = screen_name.lstrip("@") | ||
id = screen_name or str(user_id) | ||
id_type = "screen_name" if screen_name else "user_id" | ||
log.info("starting user timeline for user %s", id) | ||
|
||
if screen_name or user_id: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused about the logic here - I'm not sure what this if statement is for |
||
url = "https://api.twitter.com/1.1/favorites/list.json" | ||
else: | ||
url = "https://api.twitter.com/1.1/favorites/list.json" | ||
|
||
params = {"count": 200, id_type: id, "include_ext_alt_text": "true"} | ||
|
||
retrieved_pages = 0 | ||
reached_end = False | ||
|
||
while True: | ||
if since_id: | ||
# Make the since_id inclusive, so we can avoid retrieving | ||
# an empty page of results in some cases | ||
params["since_id"] = str(int(since_id) - 1) | ||
if max_id: | ||
params["max_id"] = max_id | ||
|
||
try: | ||
resp = self.get(url, params=params, allow_404=True) | ||
retrieved_pages += 1 | ||
except requests.exceptions.HTTPError as e: | ||
if e.response.status_code == 404: | ||
log.warn("no timeline available for %s", id) | ||
break | ||
elif e.response.status_code == 401: | ||
log.warn("protected account %s", id) | ||
break | ||
raise e | ||
|
||
statuses = resp.json() | ||
|
||
if len(statuses) == 0: | ||
log.info("no new tweets matching %s", params) | ||
break | ||
|
||
for status in statuses: | ||
# We've certainly reached the end of new results | ||
if since_id is not None and status["id_str"] == str(since_id): | ||
reached_end = True | ||
break | ||
# If you request an invalid user_id, you may still get | ||
# results so need to check. | ||
if not user_id or id == status.get("user", {}).get("id_str"): | ||
yield status | ||
|
||
if reached_end: | ||
log.info("no new tweets matching %s", params) | ||
break | ||
|
||
if max_pages is not None and retrieved_pages == max_pages: | ||
log.info("reached max page limit for %s", params) | ||
break | ||
|
||
max_id = str(int(status["id_str"]) - 1) | ||
|
||
def user_lookup(self, ids, id_type="user_id"): | ||
""" | ||
A generator that returns users for supplied user ids, screen_names, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This description should be for favourites, not tweets posted