Stream: Zulip

Topic: Overpass Turbo short link bot


view this post on Zulip i-ky (Jul 18 2023 at 12:38):

Neskatoties uz to, ka mums ir overpass un wizard Code Playgrounds, kas ļauj ierakstīt čatā vaicājuma tekstu un dabūt klīkšķināmo pogu, lai pāriet uz Overpass Turbo lapu, ik pa laiku kāds ie'paste'o short link (piemēram, te). Ideja ir uztaisīt Zulip botu, kas skanēs visus ienākošus massage'us, un ja tiek pamanīts tāds short link, bots atbildēs ar vaicājuma tekstu koda blokā.

You can write snippets of code, code blocks, and other text in a fixed-width font using standard Markdown formatting. Zulip also has syntax highlighting and supports configuring custom code playgrounds. | Tagging a code block with a language enables syntax highlighting and (if configured) code playgrounds. Zulip supports syntax highlighting for hundreds of languages. | A code block can be tagged by typing the language name after the fence (```) that begins a code block, as shown here. Typeahead will help you enter the name for the language. The Short names values from the Pygments lexer documentation are the complete set of values that support syntax highlighting.
Collection of scripts to update and maintain OpenStreetMap data in Latvia (currently only addresses). See https://wiki.openstreetmap.org/wiki/Automated_edits/Latvia-bot. - GitHub - Davis-Klavins/os...

view this post on Zulip i-ky (Oct 26 2023 at 14:19):

#!/usr/bin/env python3

from base64 import urlsafe_b64decode
from urllib.parse import urlparse, parse_qsl, unquote
from urllib.request import build_opener, HTTPRedirectHandler

# https://github.com/tyrasd/overpass-turbo/blob/c16d92d43a68091b56d4ec65b2ef326950d7c586/js/misc.ts#L239-L262
def decompress(data: str):
    d: dict[int, str] = {}
    currChar, *rest = data
    oldPhrase = currChar
    yield currChar
    code = 256
    phrase = None
    for i in rest:
        currCode = ord(i)
        if currCode < 256:
            phrase = i
        else:
            phrase = d[currCode] if currCode in d else oldPhrase + currChar
        yield phrase
        currChar = phrase[0]
        d[code] = oldPhrase + currChar
        code += 1
        oldPhrase = phrase

# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape
def escape(s):
    def transform(c):
        if "A" <= c and c <= "Z" or "a" <= c and c <= "z" or "0" <= c and c <= "9" or c in r"@\*_+-./":
            return c
        o = ord(c)
        return f"%{o:02x}" if o < 256 else f"%u{o:04x}"
    return "".join(map(transform, s))

class My(HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, hdrs, newurl):
        for k, v in parse_qsl(urlparse(newurl).query, strict_parsing=True, encoding="ASCII", errors="strict"):
            if k == "q":
                v += "=" * ((4 - len(v) % 4) % 4)
                print(unquote(escape("".join(decompress(urlsafe_b64decode(v).decode(encoding="UTF-8"))))))
        exit(0)

build_opener(My).open("https://overpass-turbo.eu/s/1pUv")

Last updated: May 19 2024 at 23:42 UTC