GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Resolve redirects when testing URLs for equality by hseg · Pull Request #3 · papis/python-doi

Conversation

@hseg

@hseg hseg commented Apr 15, 2025

Copy link
Copy Markdown

The URL DOIs resolve to can move around, with redirects pointing to the new location. To make the tests more robust, only fail if the URLs differ after redirections.

See also https://www.crossref.org/blog/urls-and-dois-a-complicated-relationship/ and item 10 in https://pardalotus.tech/posts/2024-10-02-falsehoods-programmers-believe-about-dois/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@alexfikl
makefile: simplify targets
f27f38d
@hseg

hseg commented Apr 15, 2025

Copy link
Copy Markdown
Author

I noticed this since the test case with https://aip.scitation.org/doi/10.1063/1.5081715 started failing

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hseg
hseg force-pushed the canonicalize_urls branch from 9c3e06c to b301736 Compare April 15, 2025 18:52
@hseg

hseg commented Apr 15, 2025

Copy link
Copy Markdown
Author

Short-circuited the normalized equality so as to cut down on the number of attempts at resolving redirects -- otherwise, have been running into 403 errors that seem to arise from rate-limiting (since they go away with more attempts)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hseg
hseg force-pushed the canonicalize_urls branch from b301736 to c51f6be Compare April 15, 2025 18:56
@alejandrogallo

alejandrogallo commented Apr 16, 2025 via email

Copy link
Copy Markdown
Member
Is this good to go?
…
-- Alejandro Gallo
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hseg

hseg commented Apr 16, 2025 via email

Copy link
Copy Markdown
Author
There's three points I'm unhappy/uncertain about that I'd like feedback on: - this just makes the tests _more_ robust, but I'm unsure it entirely avoids the intermittent 403s I was getting during testing. User-agent and forcing https looks like it should work, from my testing - it might be undesirable to unconditionally upgrade to https - printing the HTTPException when it's caught might be unnecessary - there might be a pytest option for that. I was doing it to try to get more detail on the exception. Other than that, I'm happy with this. 16 abr 2025 09:27:22 Alejandro Gallo ***@***.***>:
…
[Imagen]*alejandrogallo* left a comment (papis/python-doi#3)[#3 (comment)] <br> <br> Is this good to go?<br> <br> -- <br> Alejandro Gallo<br> <br> — Reply to this email directly, view it on GitHub[#3 (comment)], or unsubscribe[https://github.com/notifications/unsubscribe-auth/AAJTW5IFEAHYLDGENOXZ3CL2ZXZ4TAVCNFSM6AAAAAB3GKAMRGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDQMBYGUYTINJWGU]. You are receiving this because you authored the thread.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hseg
hseg force-pushed the canonicalize_urls branch 2 times, most recently from 289b398 to c67beee Compare April 17, 2025 17:13
@hseg

hseg commented Apr 17, 2025

Copy link
Copy Markdown
Author

OK, so I've addressed most of my concerns -- I've documented the problem with unconditionally upgrading to https, have made the test case warn when it needs to fall back to resolving redirects, and removed the HTTPException (further testing revealed that the information it provided is accessible by invoking pytest with --showlocals).
However, further testing does indeed show that the test remains flaky, with scitation abusing HTTP 403 when they want to respond with 429. I'd recommend adding pytest-rerunfailures or similar to the dev requirements and marking this particular test as flaky and needing to be rerun on HTTPException.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hseg

hseg commented Apr 17, 2025

Copy link
Copy Markdown
Author

Added pytest-rerunfailures to address the flakiness, I have no more notes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

alexfikl added 5 commits April 17, 2025 20:56
@alexfikl
setup: switch to pyproject and hatchling
3a03a4a
@alexfikl
ci: remove travis.yaml
758318b
@alexfikl
ci: add tests to ci
507acb0
@alexfikl
style: fix flake8 issues
a64941e
@alexfikl
tests: fix tests
844fdfd
@hseg

hseg commented Apr 27, 2025

Copy link
Copy Markdown
Author

No good, the test still fails with 403. Can't think of what else they're probing that we need to send -- curl -A Mozilla/5.0 works fine at the https endpoint.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hseg hseg mentioned this pull request Jun 23, 2025
Modernize #2
Closed
hseg added 2 commits June 23, 2025 21:59
@hseg
tests: Configure pytest to ignore docs
34ea67c
@hseg
Resolve redirects when testing URLs for equality
2e4b622
The URL DOIs resolve to can move around, with redirects pointing to the
new location. To make the tests more robust, only fail if the URLs
differ after redirections.

See also
https://www.crossref.org/blog/urls-and-dois-a-complicated-relationship/
@hseg
hseg force-pushed the canonicalize_urls branch from c2929c9 to 2e4b622 Compare June 24, 2025 16:56
@hseg

hseg commented Jun 24, 2025

Copy link
Copy Markdown
Author

OK, I've rebased this on top of #2 (including alexfikl#1 on the assumption @alexfikl will merge it), so now both can be merged with no issue.
I've dropped the pytest-rerunfailures commit, since ultimately what seems to be the correct advice is to just update the URLs when they change, as done in #2.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hseg

hseg commented Jun 24, 2025

Copy link
Copy Markdown
Author

Hm. I'm not fully satisfied by my implementation -- I would like to figure out how come I'm managing to get the page to load in the browser but urllib returns 403. I have discovered that urllib is properly redirecting the URL, it's just that the final URL returns 403 (presumably because Cloudflare is detecting the scraper).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@alexfikl

alexfikl commented Jun 24, 2025

Copy link
Copy Markdown

I have discovered that urllib is properly redirecting the URL, it's just that the final URL returns 403 (presumably because Cloudflare is detecting the scraper).

Yeah, Cloudflare anti-scraper stuff is making a lot of the downloaders in Papis also fail (even though they would work properly if fed the actual webpage). I'm not sure there's much we can do there.. from what I recall the "fix" for that is to use something like playwright that can fake being a known browser.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hseg

hseg commented Jun 24, 2025 via email

Copy link
Copy Markdown
Author
24 jun 2025 22:00:24 Alex Fikl ***@***.***>:
[Imagen]*alexfikl* left a comment (papis/python-doi#3)[#3 (comment)] I have discovered that *urllib* /is/ properly redirecting the URL, it's just that the final URL returns 403 (presumably because Cloudflare is detecting the scraper). Yeah, Cloudflare anti-scraper stuff is making a lot of the downloaders in Papis also fail (even though they would work properly if fed the actual webpage). I'm not sure there's much we can do there.. from what I recall the "fix" for that is to use something like playwright[https://playwright.dev/] that can fake being a known browser. — Reply to this email directly, view it on GitHub[#3 (comment)], or unsubscribe[https://github.com/notifications/unsubscribe-auth/AAJTW5INPN24E7M46FNZM333FGN4PAVCNFSM6AAAAAB3GKAMRGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTAMBRGU2TMNRVGE]. You are receiving this because you authored the thread. [Imagen de rastreo][https://github.com/notifications/beacon/AAJTW5OF4MY45VOFEFE2TVT3FGN4PA5CNFSM6AAAAAB3GKAMRGWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTVS5APKW.gif]
Alternatively, we could take a page out of fanficfare's book and use cloudscraper.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hseg

hseg commented Jun 29, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

OK, cloudscraper indeed seems to work:

import cloudscraper
scraper = cloudscraper.create_scraper()
print(scraper.get('http://aip.scitation.org/doi/10.1063/1.5081715').url)
# https://pubs.aip.org/aip/jcp/article-abstract/150/7/074102/197572/Exact-two-component-equation-of-motion-coupled?redirectedFrom=fulltext
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hseg
hseg force-pushed the canonicalize_urls branch 2 times, most recently from 7cfe04a to b161e62 Compare June 29, 2025 14:40
@hseg

hseg commented Jun 29, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Author

OK, added a [challenges] extra to bypass websites using CloudFlare (and friends, in the future) to protect against DDoS. Also added a test to check my redirect resolution code works, as @alexfikl asked for in #2 (comment), though ATM it only has testcases for cloudscraper-dependent sites. Still, I'm happy with the code as it stands now, and would like to have it merged.
@alejandrogallo, can we get this merged?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hseg
Use cloudscraper to solve cloudflare challenges
ca6dcf0
Also put in a fallback using requests, but it is hacky and only works
sometimes. cloudscraper stands a better chance of consistently being
able to get to the final URL
@hseg
Parametrize tests
8e5f3c9
This eg makes it easier to spot which particular iteration breaks
@hseg
hseg force-pushed the canonicalize_urls branch from b161e62 to 8e5f3c9 Compare June 29, 2025 14:46
@hseg
Make test_redirect cases prettier
ab9d72a
@hseg

hseg commented Jun 29, 2025

Copy link
Copy Markdown
Author

Note this still isn't perfect -- my testing must've gotten my on scitation's warnlist, because I'm starting to get redirected to their captcha page.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hseg

hseg commented Dec 17, 2025

Copy link
Copy Markdown
Author

So I tried to investigate this further today, see if I could make the test more robust.
First problem is that scitation is very aggressive with their anti-scraping, so that testcase works at best 3-4 times before their countermeasures kick in.
In trying to investigate this, I tried using different test cases to replace it.
Found some irreproducible flakiness with this article. But even worse, trying to bulk out the testcase collection led me to discover multiple resolution, and the papers cited there all break our current system, to a point I'm no longer sure what the correct semantics are here. Does "a canonical URL" even exist? If not, is this PR useless?

EDIT: Reading further on that page, it seems that among the multiple valid URLs for the DOI, there will always be a "primary" URL, which can obtained by adding ?locatt=mode:legacy to the doi.org query string. But that means our equality needs to be at the level of DOIs, and that naively following redirects is insufficient.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@alejandrogallo

alejandrogallo commented Jan 7, 2026

Copy link
Copy Markdown
Member

From what I've seen this seems fine, thank you for your work, what do you think @alexfikl ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@alexfikl

alexfikl commented Jan 7, 2026

Copy link
Copy Markdown

@alejandrogallo Yeah, this looks good to me too! It doesn't seem like we can do much better these days + scraping seems to be disallowed more and more with AI wreaking havoc all over the place :(

It also includes #2, so I'll go ahead and close that.

First problem is that scitation is very aggressive with their anti-scraping, so that testcase works at best 3-4 times before their countermeasures kick in.

Might be worth looking for some other website then? The main point is to test that the DOI detection in the library works.. not necessarily that it works with scitation 😁

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hseg

hseg commented Jan 7, 2026 via email

Copy link
Copy Markdown
Author
Only concern I have remaining is multiple resolution, but we could reopen that as a new issue - idk how much work it'd be to support that.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@hseg @alejandrogallo @alexfikl
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.