-
Notifications
You must be signed in to change notification settings - Fork 0
/
abclinuxu-blog-readcount.py
executable file
·53 lines (45 loc) · 1.49 KB
/
abclinuxu-blog-readcount.py
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
#!/usr/bin/env python3
# -*- coding: utf8 -*-
import argparse
import sys
import lxml.html
def get_meta(meta_str):
"""
Get metadata from meta-vypis string.
"""
meta_list = [i.strip() for i in meta_str.split("|")]
timestamp = meta_list[0]
read_count = 0
comments = 0
for item in meta_list:
if "Přečteno" in item:
read_count = int(item[10:-1])
if "Komentářů" in item:
count, last = item.split(",")
comments = int(count[11:])
return timestamp, read_count, comments
ap = argparse.ArgumentParser(description="abclinuxu.cz blog read counter")
ap.add_argument(
"file",
nargs='*',
type=argparse.FileType('r'),
default=[sys.stdin],
help="input html file with blog overview")
ap.add_argument("-t", action='store_true', help="show time stamp")
ap.add_argument("-r", action='store_true', help="show read count")
ap.add_argument("-c", action='store_true', help="show comment count")
args = ap.parse_args()
for fo in args.file:
html = lxml.html.fromstring(fo.read())
for post_div in html.xpath("//div[@class='cl']"):
post_path = post_div.xpath("./h2/a/@href")[0]
meta_p = post_div.xpath("./p[@class='meta-vypis']")[0]
ts, read_count, comments = get_meta(meta_p.text_content())
print(post_path, end=" ")
if args.t:
print(ts, end=" ")
if args.r:
print(read_count, end=" ")
if args.c:
print(comments, end=" ")
print()