Skip to content
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 the bookmarks count to tweet #286

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
lib/
__pycache__/
.DS_Store
twikit.egg-info/
pyvenv.cfg
build/
bin/
18 changes: 18 additions & 0 deletions example_project/test_twikit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import asyncio
from twikit import Client

async def main():
client = Client()

# Load your saved cookies if you have them
client.load_cookies('path/to/cookies.json')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use environment variables or command-line arguments for the cookies path.

Hardcoded paths reduce flexibility and portability. Consider using environment variables or command-line arguments.

-    client.load_cookies('path/to/cookies.json')
+    cookies_path = os.getenv('TWIKIT_COOKIES_PATH', 'cookies.json')
+    client.load_cookies(cookies_path)

Committable suggestion skipped: line range outside the PR's diff.


# Or login directly
# await client.login(auth_info_1='...', auth_info_2='...', password='...')

# Get a tweet and check its bookmark count
tweet = await client.get_tweet_by_id('1519480761749016577')
print(f"Tweet bookmark count: {tweet.bookmark_count}")
Comment on lines +14 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling and make tweet ID configurable.

The script needs proper error handling for API calls and should make the tweet ID configurable.

-    tweet = await client.get_tweet_by_id('1519480761749016577')
-    print(f"Tweet bookmark count: {tweet.bookmark_count}")
+    tweet_id = os.getenv('TWEET_ID', '1519480761749016577')
+    try:
+        tweet = await client.get_tweet_by_id(tweet_id)
+        print(f"Tweet bookmark count: {tweet.bookmark_count}")
+    except Exception as e:
+        print(f"Error fetching tweet: {e}")

Committable suggestion skipped: line range outside the PR's diff.


if __name__ == "__main__":
asyncio.run(main())
40 changes: 0 additions & 40 deletions examples/delete_all_tweets.py

This file was deleted.

41 changes: 0 additions & 41 deletions examples/dm_auto_reply.py

This file was deleted.

23 changes: 0 additions & 23 deletions examples/download_tweet_media.py

This file was deleted.

137 changes: 0 additions & 137 deletions examples/example.py

This file was deleted.

26 changes: 0 additions & 26 deletions examples/guest.py

This file was deleted.

37 changes: 0 additions & 37 deletions examples/listen_for_new_tweets.py

This file was deleted.

5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

from setuptools import setup
from setuptools import setup, find_packages

with open('README.md', encoding='utf-8') as f:
long_description = f.read()
Expand All @@ -10,6 +10,7 @@

setup(
name='twikit',
packages=find_packages(),
version=version,
install_requires=[
'httpx[socks]',
Expand All @@ -24,5 +25,5 @@
long_description_content_type='text/markdown',
license='MIT',
url='https://github.com/d60/twikit',
package_data={'twikit': ['py.typed']}
include_package_data=True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Remove include_package_data=True as it's not needed

The codebase doesn't contain any non-Python package data files that need to be included in the distribution. The existing non-Python files are development-related (documentation, requirements, linter config) and don't need to be packaged. You can safely remove this setting from setup.py.

🔗 Analysis chain

Verify MANIFEST.in file exists.

The include_package_data=True setting requires a MANIFEST.in file to specify which non-Python files should be included in the distribution.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if MANIFEST.in exists and contains necessary include statements
if [ -f "MANIFEST.in" ]; then
    echo "MANIFEST.in contents:"
    cat MANIFEST.in
else
    echo "Warning: MANIFEST.in file not found"
fi

Length of output: 105


Script:

#!/bin/bash
# Search for non-Python files that might need to be included in distribution
fd --type f \
   --exclude '*.py' \
   --exclude '*.pyc' \
   --exclude '__pycache__' \
   --exclude '.git' \
   --exclude '.github' \
   --exclude 'tests' \
   --exclude '*.md' \
   --exclude 'LICENSE' \
   --exclude '.gitignore' \
   --exclude 'setup.py' \
   --exclude 'setup.cfg' \
   --exclude 'pyproject.toml' \
   --exclude '*.ini'

Length of output: 365

)
4 changes: 4 additions & 0 deletions twikit/client/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .client import Client
from .gql import GQLClient, Endpoint

__all__ = ['Client', 'GQLClient', 'Endpoint']
10 changes: 10 additions & 0 deletions twikit/guest/tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class Tweet:
Information about URLs contained in the tweet.
full_text : :class:`str` | None
The full text of the tweet.
bookmark_count : :class:`int` | None
The number of bookmarks of the tweet.
"""

def __init__(self, client: GuestClient, data: dict, user: User = None) -> None:
Expand Down Expand Up @@ -115,6 +117,14 @@ def __init__(self, client: GuestClient, data: dict, user: User = None) -> None:
self.view_count_state: str = data['views'].get('state') if 'views' in data else None
self.has_community_notes: bool = data.get('has_birdwatch_notes')

# Get bookmark count from public_metrics if available, otherwise from legacy
public_metrics = data.get('public_metrics', {})
self.bookmark_count: int | None = (
public_metrics.get('bookmark_count')
if public_metrics.get('bookmark_count') is not None
else legacy.get('bookmark_count')
)

if data.get('quoted_status_result'):
quoted_tweet = data.pop('quoted_status_result')['result']
if 'tweet' in quoted_tweet:
Expand Down
Loading