forked from allanlepp/te_rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_lounaeestlane.py
44 lines (33 loc) · 2.53 KB
/
parser_lounaeestlane.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
import parsers_common
import parsers_datetime
def fill_article_dict(articleDataDict, pageTree, domain):
articleDataDict["images"] = parsers_common.xpath_to("list", pageTree, '//div[@class="col-sm-6"]/div[@class="post-item"]/a/div/img/@src')
articleDataDict["titles"] = parsers_common.xpath_to("list", pageTree, '//div[@class="col-sm-6"]/div[@class="post-item"]/a/h3/text()')
articleDataDict["urls"] = parsers_common.xpath_to("list", pageTree, '//div[@class="col-sm-6"]/div[@class="post-item"]/a/@href')
for i in parsers_common.article_urls_range(articleDataDict["urls"]):
# images
curArtImage = parsers_common.get(articleDataDict["images"], i)
curArtImage = parsers_common.split_failsafe(curArtImage, "?", 0)
curArtImage = curArtImage.replace("http://", "https://")
articleDataDict["images"] = parsers_common.list_add_or_assign(articleDataDict["images"], i, curArtImage)
# titles
curArtTitle = parsers_common.get(articleDataDict["titles"], i)
curArtTitle = parsers_common.str_remove_clickbait(curArtTitle)
articleDataDict["titles"] = parsers_common.list_add_or_assign(articleDataDict["titles"], i, curArtTitle)
if parsers_common.should_get_article_body(i):
curArtUrl = parsers_common.get(articleDataDict["urls"], i)
# load article into tree
pageTree = parsers_common.get_article_tree(domain, curArtUrl, cache='cacheAll')
# descriptions
curArtDesc = parsers_common.xpath_to("single", pageTree, '//div[@class="lead"]', parent=True)
if not curArtDesc:
curArtDesc = parsers_common.xpath_to("single", pageTree, '//div[@class="col-sm-9"]/p', multi=True)
curArtDesc2 = parsers_common.xpath_to("single", pageTree, '//div[@class="text flex-row"]/p', multi=True)
curArtDesc = curArtDesc + '<br>' + curArtDesc2
articleDataDict["descriptions"] = parsers_common.list_add_or_assign(articleDataDict["descriptions"], i, curArtDesc)
# timeformat magic from "Avaldatud: 14 detsember, 2017" to datetime()
curArtPubDate = parsers_common.xpath_to("single", pageTree, '//div[@class="col-sm-9"]/div[@class="page-header"]/em/text()')
curArtPubDate = parsers_datetime.months_to_int(curArtPubDate.split(':')[1])
curArtPubDate = parsers_datetime.raw_to_datetime(curArtPubDate, "%d %m, %Y")
articleDataDict["pubDates"] = parsers_common.list_add_or_assign(articleDataDict["pubDates"], i, curArtPubDate)
return articleDataDict