| 1 |
#!python3 |
| 2 |
|
| 3 |
""" |
| 4 |
Download all Reids commands from https://redis.io/commands. |
| 5 |
Output to csv format. |
| 6 |
""" |
| 7 |
|
| 8 |
import sys |
| 9 |
import csv |
| 10 |
|
| 11 |
from lxml import etree |
| 12 |
import requests |
| 13 |
|
| 14 |
stdout_writer = csv.writer(sys.stdout) |
| 15 |
|
| 16 |
|
| 17 |
def eprint(*args, **kwargs): |
| 18 |
print(*args, file=sys.stderr, **kwargs) |
| 19 |
|
| 20 |
|
| 21 |
eprint("Download https://redis.io/commands page...") |
| 22 |
page = requests.get("https://redis.io/commands").text |
| 23 |
eprint("Download finished!") |
| 24 |
|
| 25 |
eprint("Start prase page...") |
| 26 |
html = etree.HTML(page) |
| 27 |
commands = html.xpath("//div[@class='container']/ul/li") |
| 28 |
stdout_writer.writerow(["Group", "Command", "Args", "Summary", "Redis.io link"]) |
| 29 |
command_rows = [] |
| 30 |
# parse page |
| 31 |
for command in commands: |
| 32 |
group = command.attrib["data-group"] |
| 33 |
command_name = command.xpath("./a/span[@class='command']/text()")[0].strip() |
| 34 |
command_args = command.xpath( |
| 35 |
"./a/span[@class='command']/span[@class='args']/text()" |
| 36 |
)[0].strip() |
| 37 |
command_summary = command.xpath("./a/span[@class='summary']/text()")[0].strip() |
| 38 |
command_link = "https://redis.io" + command.xpath("./a/@href")[0].strip() |
| 39 |
command_rows.append( |
| 40 |
[ |
| 41 |
group, |
| 42 |
command_name, |
| 43 |
" ".join(command_args.split()), |
| 44 |
command_summary, |
| 45 |
command_link, |
| 46 |
] |
| 47 |
) |
| 48 |
# write to stdout |
| 49 |
for row in sorted(command_rows): |
| 50 |
stdout_writer.writerow(row) |
| 51 |
eprint("Down.") |