aboutsummaryrefslogtreecommitdiff
path: root/doc/filters/donnons.py
blob: a29eed9b15f2df741602388c9efdb5483b59a4c5 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: ft=python et softtabstop=4 cinoptions=4 shiftwidth=4 ts=4 ai
"""
USAGE
    curl URL | donnons.py [OPTION]

OPTIONS
    -t, --title=TEXT
        Updates feed title.
        Default is "Donnons.org"
    -d, --description=TEXT
        Updates feed description.
        Default is "Dons"
    -v, --version
        Print version and exit.
    -h, --help
        Show this help and exit.
"""

from __future__ import print_function
from bs4 import BeautifulSoup
import PyRSS2Gen
import sys
import getopt
import logging
import re
import datetime

__version__ = "0.1"

# Defaults
feed_title = None
feed_description = None
base_url = "http://donnons.org"

# Logging
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
logging.disable(logging.INFO - 1)
logger = logging.getLogger('donnons')

# Parse args
try:
    optlist, args = getopt.getopt(
        sys.argv[1:],
        'u:t:d:hv',
        ['help', 'version', 'title=', 'description=']
    )
except getopt.GetoptError as msg:
    logger.error("%s\n\n%s", msg, __doc__)
    sys.exit(2)

for opt in optlist:
    if opt[0] in ('-t', '--title'):
        feed_title = opt[1]
    if opt[0] in ('-d', '--description'):
        feed_description = opt[1]
    elif opt[0] in ('-v', '--version'):
        print(__version__)
        sys.exit(0)
    elif opt[0] in ('-h', '--help'):
        print(__doc__)
        sys.exit(0)

html = sys.stdin.read()

# Find entries
soup = BeautifulSoup(html, "lxml")

entries = soup.body.find(
    "table",
    {"class": "lstTbl"}
).find_all(
    "tbody",
    {"class": ["lnPair", "lnImp"]}
)

# Build rss items based on entries
items = []

for entry in entries:
    colphoto = entry.find("td", {"class": "colPhoto"})
    photo = ""
    if colphoto.a:
        if colphoto.a.img:
            photo = base_url + colphoto.a.img["src"]
    coltitre = entry.find("td", {"class": "colTitre"}).a
    title = coltitre.string
    link = base_url + coltitre["href"]
    entryid = coltitre["name"]
    colcat = entry.find("td", {"class": "colCat"}).a
    cat = colcat["title"]
    catlink = base_url + colcat["href"]
    situations = entry.find("td", {"class": "colSituation"}).find_all("a")
    city_a = situations[0]
    departement_a = situations[1]
    city = city_a.string
    citylink = base_url + city_a["href"]
    citylinktitle = base_url + city_a["title"]
    departement = departement_a.string
    departementlink = departement_a["href"]
    departementlinktitle = departement_a["title"]
    coluser = entry.find("td", {"class": "colUser"})
    imguser = ""
    if coluser.img:
        imguser = coluser.img
    if imguser:
        user = imguser["alt"]
    else:
        user = coluser.string.strip()
    colstat = entry.find("td", {"class": "colStat"})
    info = colstat.font.string
    today = datetime.datetime.today()
    rawdate = [today.day, today.month, today.year]
    redate = re.compile(r"(\d+).(\d+).(\d+)")
    rematch = redate.findall(colstat.font.string)
    if rematch:
        rawdate = rematch[0]
    items.append(
        PyRSS2Gen.RSSItem(
            title=title + " (" + city + ")",
            link=link,
            categories=[cat],
            description=user + " (" + city + ") " + info,  # TODO test info!!
            pubDate=datetime.datetime(int(rawdate[2]), int(rawdate[1]),
                                      int(rawdate[0])),
        )
    )

# Build rss feed
rss = PyRSS2Gen.RSS2(
    title=feed_title or soup.body.find(class_="sTitrePage").text or "",
    link=None,
    description=feed_description or "",
    # lastBuildDate = datetime.datetime.now(),
    items=items)

# Return rss feed
print(rss.to_xml())
# souprss = BeautifulSoup(rss.to_xml())