File: generate-useragents-go

package info (click to toggle)
golang-github-corpix-uarand 0.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 320 kB
  • sloc: python: 55; makefile: 27; sh: 7
file content (54 lines) | stat: -rwxr-xr-x 1,144 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
import sys
from os.path import exists, expanduser
from argparse import ArgumentParser


header = """package {package}

var (
\t// UserAgents is a list of browser and bots user agents.
\tUserAgents = []string{{
"""

item = """\t\t"{content}",\n"""

footer = """\t}}
)\n
"""

if __name__ == "__main__":
    p = ArgumentParser(
        description=(
            "Expects a list of user agents delimited by new line character "
            "to be passed into STDIN and generates go code with this data."
        )
    )
    p.add_argument(
        "package",
        help="Go package name to use",
        default="uarand"
    )
    args = p.parse_args().__dict__

    params = args.copy()
    sys.stderr.write("Reading stdin...\n")
    sys.stdout.write(
        header.format(**params)
    )

    raw_items = []
    for line in sys.stdin:
        raw_items.append(line.strip())

    for raw_item in sorted(list(set(raw_items))):
        sys.stdout.write(
            item.format(
                content=raw_item,
                **params
            )
        )

    sys.stdout.write(
        footer.format(**params)
    )