Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #197825 > unrolled thread

Re: No more Redirect page.

Started byLawrence D’Oliveiro <ldo@nz.invalid>
First post2026-07-25 04:33 +0000
Last post2026-07-29 23:45 +0000
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: No more Redirect page. Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-07-25 04:33 +0000
    Re: No more Redirect page. Gilmeh Serda <gilmeh.serda@nothing.here.invalid> - 2026-07-25 15:59 +0000
      Re: No more Redirect page. ram@zedat.fu-berlin.de (Stefan Ram) - 2026-07-25 16:24 +0000
        Re: No more Redirect page. Gilmeh Serda <gilmeh.serda@nothing.here.invalid> - 2026-07-26 18:32 +0000
      Re: No more Redirect page. Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-07-29 23:36 +0000
        Re: No more Redirect page. Jon Ribbens <jon+usenet@unequivocal.eu> - 2026-07-29 23:45 +0000

#197825 — Re: No more Redirect page.

FromLawrence D’Oliveiro <ldo@nz.invalid>
Date2026-07-25 04:33 +0000
SubjectRe: No more Redirect page.
Message-ID<1141eb5$16llq$1@dont-email.me>
On Fri, 24 Jul 2026 11:03:09 -0400, micky wrote:

> You may have noticed by now that clicking on Google search links
> sometimes or always makes you stop at a page to "permit" redirection
> to another page, even though you clicked on it in the first place
> and even though it never did this before. "The "redirect notice"
> page usually appears because Google wraps outbound links with a
> tracking parameter (google.com/url) to gather analytics and scan for
> malicious URLs".

I often copy and paste links into the following script for this reason.

Feel free to experiment with this as appropriate. Note the “--unquote”
option, because sometimes I find the wrapped URL has an extra
(needless) level of quoting.
----
#!/usr/bin/python3
#+
# Given a URL from various sites that like to include redirection
# capture cruft, extract the URL and discard the cruft.
#-

import sys
import re
from urllib import \
    parse as urlparse
import getopt

start_marker = r"\=(https?(\:\/\/|\%3a\%2f\%2f))"

show_original = False
force_unquote = False
opts, args = getopt.getopt \
  (
    sys.argv[1:],
    "",
    ["original", "unquote"]
  )
for keyword, value in opts :
    if keyword == "--original" :
        show_original = True
    elif keyword == "--unquote" :
        force_unquote = True
    #end if
#end for
urls = args

if len(urls) == 0 :
    raise getopt.GetoptError("pass at least one URL to unredirect")
#end if
for url in urls :
    matches = list(re.compile(start_marker, re.IGNORECASE).finditer(url))
    if len(matches) != 0 :
        match = matches[-1]
        startpos = match.start(1)
        endpos = url.find("&", match.end(1))
        if endpos < 0 :
            endpos = len(url)
        #end if
        actual = url[startpos : endpos]
        if match.group(2).startswith("%") or force_unquote :
            actual = urlparse.unquote(actual)
        #end if
        if show_original :
            sys.stdout.write("%s => " % url)
        #end if
        sys.stdout.write("%s\n" % actual)
    else :
        sys.stdout.write("# Cannot understand: %s\n" % url)
    #end if
#end for

[toc] | [next] | [standalone]


#197826

FromGilmeh Serda <gilmeh.serda@nothing.here.invalid>
Date2026-07-25 15:59 +0000
Message-ID<b359S.248240$5lP1.187364@fx12.ams4>
In reply to#197825
On Sat, 25 Jul 2026 04:33:42 -0000 (UTC), Lawrence D’Oliveiro wrote:

> I often copy and paste links into the following script for this reason.

Why not use pyperclip? Then you can just use clipboard:

1. Copy original URL
2. Run script (+ options)
3. Paste resulting URL

https://pypi.org/project/pyperclip/

You may also want to read this:
https://github.com/asweigart/pyperclip/blob/master/src/pyperclip/
__init__.py  row 19.


If there are only a few options you might even wanna create an alias for 
each version, like scra and scrb or something and put it in your .bashrc:

    alias scra='scriptname -option1';
    alias scrb='scriptname -option2';

Your use of shebang suggests *nix, so...


If I think ArgumentParser is too bulky for a small script, I usually just 
use sys module, which you already have imported.

def main(opt):
    URL = pyperclip.paste()

    if opt == 1:
	[...]
    elif opt == 2:
        [...]

    pyperclip.copy(resulting_URL)

if __name__ == '__main__':
    args = sys.argv[1:]
    if '-option1' in args:
	main(1)
    elif '-option2' in args:
        main(2)
    else:
        print(usage)

But each to their own... It's your script.

-- 
Gilmeh

Everybody likes a kidder, but nobody lends him money. -- Arthur Miller

[toc] | [prev] | [next] | [standalone]


#197827

Fromram@zedat.fu-berlin.de (Stefan Ram)
Date2026-07-25 16:24 +0000
Message-ID<standard-20260725172340@ram.dialup.fu-berlin.de>
In reply to#197826
Gilmeh Serda <gilmeh.serda@nothing.here.invalid> wrote or quoted:
>URL = pyperclip.paste()

  I read from the clipboard all the time, but prefer to 
  use the standard library, so I,

import tkinter as tk
. . .
content = tk.Tk().clipboard_get()

  .

[toc] | [prev] | [next] | [standalone]


#197828

FromGilmeh Serda <gilmeh.serda@nothing.here.invalid>
Date2026-07-26 18:32 +0000
Message-ID<Dos9S.436808$GiO1.202949@fx15.ams4>
In reply to#197827
On 25 Jul 2026 16:24:37 GMT, Stefan Ram wrote:

>  I read from the clipboard all the time, but prefer to
>   use the standard library, so I,

You do you.

-- 
Gilmeh

This is the theory that Jack built. This is the flaw that lay in the 
theory that Jack built. This is the palpable verbal haze that hid the flaw 
that lay in...

[toc] | [prev] | [next] | [standalone]


#197832

FromLawrence D’Oliveiro <ldo@nz.invalid>
Date2026-07-29 23:36 +0000
Message-ID<114e2qd$18m2n$7@dont-email.me>
In reply to#197826
On Sat, 25 Jul 2026 15:59:03 GMT, Gilmeh Serda wrote:

> On Sat, 25 Jul 2026 04:33:42 -0000 (UTC), Lawrence D’Oliveiro wrote:
>
>> I often copy and paste links into the following script for this
>> reason.
>
> Why not use pyperclip? Then you can just use clipboard:

And then have to install a bunch of extra dependencies. My existing
script relies on nothing other than the standard Python library:

    import sys
    import re
    from urllib import \
        parse as urlparse
    import getopt

That’s it.

[toc] | [prev] | [next] | [standalone]


#197833

FromJon Ribbens <jon+usenet@unequivocal.eu>
Date2026-07-29 23:45 +0000
Message-ID<slrn116l44j.4iq.jon+usenet@raven.unequivocal.eu>
In reply to#197832
On 2026-07-29, Lawrence D’Oliveiro <ldo@nz.invalid> wrote:
> On Sat, 25 Jul 2026 15:59:03 GMT, Gilmeh Serda wrote:
>> On Sat, 25 Jul 2026 04:33:42 -0000 (UTC), Lawrence D’Oliveiro wrote:
>>> I often copy and paste links into the following script for this
>>> reason.
>>
>> Why not use pyperclip? Then you can just use clipboard:
>
> And then have to install a bunch of extra dependencies. My existing
> script relies on nothing other than the standard Python library:
>
>     import sys
>     import re
>     from urllib import \
>         parse as urlparse
>     import getopt
>
> That’s it.

That's a bonus it's true, but the whole '\' business gives me conniptions.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web