| 1 |
#!/usr/bin/env python3 |
| 2 |
import sys |
| 3 |
from os.path import exists, expanduser |
| 4 |
from argparse import ArgumentParser |
| 5 |
|
| 6 |
|
| 7 |
header = """package {package} |
| 8 |
|
| 9 |
var ( |
| 10 |
\t// UserAgents is a list of browser and bots user agents. |
| 11 |
\tUserAgents = []string{{ |
| 12 |
""" |
| 13 |
|
| 14 |
item = """\t\t"{content}",\n""" |
| 15 |
|
| 16 |
footer = """\t}} |
| 17 |
)\n |
| 18 |
""" |
| 19 |
|
| 20 |
if __name__ == "__main__": |
| 21 |
p = ArgumentParser( |
| 22 |
description=( |
| 23 |
"Expects a list of user agents delimited by new line character " |
| 24 |
"to be passed into STDIN and generates go code with this data." |
| 25 |
) |
| 26 |
) |
| 27 |
p.add_argument( |
| 28 |
"package", |
| 29 |
help="Go package name to use", |
| 30 |
default="uarand" |
| 31 |
) |
| 32 |
args = p.parse_args().__dict__ |
| 33 |
|
| 34 |
params = args.copy() |
| 35 |
sys.stderr.write("Reading stdin...\n") |
| 36 |
sys.stdout.write( |
| 37 |
header.format(**params) |
| 38 |
) |
| 39 |
|
| 40 |
raw_items = [] |
| 41 |
for line in sys.stdin: |
| 42 |
raw_items.append(line.strip()) |
| 43 |
|
| 44 |
for raw_item in sorted(list(set(raw_items))): |
| 45 |
sys.stdout.write( |
| 46 |
item.format( |
| 47 |
content=raw_item, |
| 48 |
**params |
| 49 |
) |
| 50 |
) |
| 51 |
|
| 52 |
sys.stdout.write( |
| 53 |
footer.format(**params) |
| 54 |
) |