-
-
Notifications
You must be signed in to change notification settings - Fork 419
added User-Agent before session request #369
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
base: main
Are you sure you want to change the base?
Conversation
Reviewer's GuideEnsure all HTTP requests in handle_x_migration include a browser User-Agent string to satisfy x.com’s requirements and avoid request rejections. Sequence diagram for handle_x_migration HTTP requests with User-Agent headersequenceDiagram
participant Caller
participant handle_x_migration
participant session
Caller->>handle_x_migration: Call with session, headers
handle_x_migration->>handle_x_migration: Set headers["User-Agent"]
handle_x_migration->>session: request(GET, https://x.com, headers)
session-->>handle_x_migration: response
alt migration_redirection_url found
handle_x_migration->>handle_x_migration: Set headers["User-Agent"]
handle_x_migration->>session: request(GET, migration_redirection_url, headers)
session-->>handle_x_migration: response
end
alt migration_form found
handle_x_migration->>handle_x_migration: Set headers["User-Agent"]
handle_x_migration->>session: request(method, url, data, headers)
session-->>handle_x_migration: response
end
handle_x_migration-->>Caller: home_page
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
WalkthroughThe function Changes
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Hey @akn714 - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `twikit/x_client_transaction/utils.py:11` </location>
<code_context>
home_page = None
migration_redirection_regex = re.compile(
r"""(http(?:s)?://(?:www\.)?(twitter|x){1}\.com(/x)?/migrate([/?])?tok=[a-zA-Z0-9%\-_]+)+""", re.VERBOSE)
+ headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/128.0.0.0 Safari/537.36"
response = await session.request(method="GET", url="https://x.com", headers=headers)
home_page = bs4.BeautifulSoup(response.content, 'lxml')
</code_context>
<issue_to_address>
Repeatedly setting the same User-Agent header may be redundant.
If the User-Agent value remains constant, set it once at the start to simplify the code and reduce redundancy.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
migration_redirection_regex = re.compile(
r"""(http(?:s)?://(?:www\.)?(twitter|x){1}\.com(/x)?/migrate([/?])?tok=[a-zA-Z0-9%\-_]+)+""", re.VERBOSE)
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/128.0.0.0 Safari/537.36"
response = await session.request(method="GET", url="https://x.com", headers=headers)
home_page = bs4.BeautifulSoup(response.content, 'lxml')
migration_url = home_page.select_one("meta[http-equiv='refresh']")
migration_redirection_url = re.search(migration_redirection_regex, str(
migration_url)) or re.search(migration_redirection_regex, str(response.content))
if migration_redirection_url:
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/128.0.0.0 Safari/537.36"
response = await session.request(method="GET", url=migration_redirection_url.group(0), headers=headers)
home_page = bs4.BeautifulSoup(response.content, 'lxml')
=======
migration_redirection_regex = re.compile(
r"""(http(?:s)?://(?:www\.)?(twitter|x){1}\.com(/x)?/migrate([/?])?tok=[a-zA-Z0-9%\-_]+)+""", re.VERBOSE)
# Set User-Agent header once at the start
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/128.0.0.0 Safari/537.36"
response = await session.request(method="GET", url="https://x.com", headers=headers)
home_page = bs4.BeautifulSoup(response.content, 'lxml')
migration_url = home_page.select_one("meta[http-equiv='refresh']")
migration_redirection_url = re.search(migration_redirection_regex, str(
migration_url)) or re.search(migration_redirection_regex, str(response.content))
if migration_redirection_url:
# No need to set User-Agent again, already set above
response = await session.request(method="GET", url=migration_redirection_url.group(0), headers=headers)
home_page = bs4.BeautifulSoup(response.content, 'lxml')
>>>>>>> REPLACE
</suggested_fix>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
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.
Actionable comments posted: 1
🧹 Nitpick comments (1)
twikit/x_client_transaction/utils.py (1)
21-21
: Remove unnecessary f-string prefix.The f-string on line 21 doesn't contain any placeholders, making the
f
prefix unnecessary.- migration_form = home_page.select_one("form[name='f']") or home_page.select_one(f"form[action='https://x.com/x/migrate']") + migration_form = home_page.select_one("form[name='f']") or home_page.select_one("form[action='https://x.com/x/migrate']")
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
twikit/x_client_transaction/utils.py
(1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: d60
PR: d60/twikit#44
File: twikit/client.py:53-62
Timestamp: 2024-07-27T10:11:05.183Z
Learning: The User-agent string in `twikit/client.py` is randomly generated to prevent account banning. A trailing space issue occurs in specific environments, which will be addressed by using `.strip()` in the next update.
Learnt from: d60
PR: d60/twikit#44
File: twikit/client.py:53-62
Timestamp: 2024-10-12T01:08:20.405Z
Learning: The User-agent string in `twikit/client.py` is randomly generated to prevent account banning. A trailing space issue occurs in specific environments, which will be addressed by using `.strip()` in the next update.
twikit/x_client_transaction/utils.py (2)
Learnt from: d60
PR: d60/twikit#44
File: twikit/client.py:53-62
Timestamp: 2024-10-12T01:08:20.405Z
Learning: The User-agent string in `twikit/client.py` is randomly generated to prevent account banning. A trailing space issue occurs in specific environments, which will be addressed by using `.strip()` in the next update.
Learnt from: d60
PR: d60/twikit#44
File: twikit/client.py:53-62
Timestamp: 2024-07-27T10:11:05.183Z
Learning: The User-agent string in `twikit/client.py` is randomly generated to prevent account banning. A trailing space issue occurs in specific environments, which will be addressed by using `.strip()` in the next update.
🪛 Ruff (0.11.9)
twikit/x_client_transaction/utils.py
21-21: f-string without any placeholders
Remove extraneous f
prefix
(F541)
🪛 Flake8 (7.2.0)
twikit/x_client_transaction/utils.py
[error] 21-21: f-string is missing placeholders
(F541)
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.
Actionable comments posted: 0
♻️ Duplicate comments (2)
twikit/x_client_transaction/utils.py (2)
11-12
: Eliminate hardcoded User-Agent duplication and align with existing patterns.The same User-Agent string is hardcoded multiple times in this function, and it doesn't follow the existing pattern of using randomly generated User-Agent strings to prevent account banning.
Based on the retrieved learnings, the codebase uses randomly generated User-Agent strings. Consider using the existing User-Agent generation approach:
+ # Use existing User-Agent if available, or set a consistent default + if "User-Agent" not in headers: + headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/128.0.0.0 Safari/537.36" - # Set User-Agent header once at the start - headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/128.0.0.0 Safari/537.36"
27-27
: Remove redundant User-Agent assignment.This is the same hardcoded User-Agent string set earlier in the function, creating unnecessary duplication.
Since the User-Agent header is already set in the headers dictionary and persists across requests, this line can be removed:
- headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/128.0.0.0 Safari/537.36"
🧹 Nitpick comments (1)
twikit/x_client_transaction/utils.py (1)
22-22
: Remove unnecessary f-string prefix.The f-string has no placeholders and should be a regular string.
- migration_form = home_page.select_one("form[name='f']") or home_page.select_one(f"form[action='https://x.com/x/migrate']") + migration_form = home_page.select_one("form[name='f']") or home_page.select_one("form[action='https://x.com/x/migrate']")
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
twikit/x_client_transaction/utils.py
(1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: d60
PR: d60/twikit#44
File: twikit/client.py:53-62
Timestamp: 2024-07-27T10:11:05.183Z
Learning: The User-agent string in `twikit/client.py` is randomly generated to prevent account banning. A trailing space issue occurs in specific environments, which will be addressed by using `.strip()` in the next update.
Learnt from: d60
PR: d60/twikit#44
File: twikit/client.py:53-62
Timestamp: 2024-10-12T01:08:20.405Z
Learning: The User-agent string in `twikit/client.py` is randomly generated to prevent account banning. A trailing space issue occurs in specific environments, which will be addressed by using `.strip()` in the next update.
twikit/x_client_transaction/utils.py (2)
Learnt from: d60
PR: d60/twikit#44
File: twikit/client.py:53-62
Timestamp: 2024-10-12T01:08:20.405Z
Learning: The User-agent string in `twikit/client.py` is randomly generated to prevent account banning. A trailing space issue occurs in specific environments, which will be addressed by using `.strip()` in the next update.
Learnt from: d60
PR: d60/twikit#44
File: twikit/client.py:53-62
Timestamp: 2024-07-27T10:11:05.183Z
Learning: The User-agent string in `twikit/client.py` is randomly generated to prevent account banning. A trailing space issue occurs in specific environments, which will be addressed by using `.strip()` in the next update.
🪛 Ruff (0.11.9)
twikit/x_client_transaction/utils.py
22-22: f-string without any placeholders
Remove extraneous f
prefix
(F541)
🪛 Flake8 (7.2.0)
twikit/x_client_transaction/utils.py
[error] 22-22: f-string is missing placeholders
(F541)
User-Agent is necessary, without this x.com is rejecting the requests
Summary by Sourcery
Bug Fixes:
Summary by CodeRabbit