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)
)
|