This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.py
151 lines (115 loc) · 4.13 KB
/
feed.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
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
142
143
144
145
146
147
148
149
150
151
from selector import Selector
import requests
import lxml
import logging
class Feed:
_selector_properties = [
'updated',
'title',
'author',
]
_entry_selector_properties = [
'title',
'updated',
'url',
'content',
]
_dates = [
'updated'
]
def __init__(self, config):
self.config = config
self.url = None
if 'url' in config:
self.url = config['url']
self.properties = {}
self.entries = []
def load(self, url=None):
logging.info("Loading data from {}".format(url))
url = url or self.url
headers = {}
if 'user_agent' in self.config:
headers['User-Agent'] = self.config['user_agent']
request = requests.get(url, headers=headers)
request.raise_for_status()
self._raw = request.text
def parse(self, html=None):
html = html or self._raw
parsed = lxml.html.fromstring(html)
self.properties = self._parse_properties(parsed)
self.entries = self._parse_entries(parsed)
def _parse_entries(self, htmltree):
_entries = []
logging.info("Working on entries")
config_entries = self.config['entries']
entries_selector = Selector(config_entries, multiple=True)
logging.info("Using selector \"{}\" with selector type {}".format(
entries_selector.selector,
entries_selector.SelectorType.__class__.__name__,
))
result_entries = entries_selector.result(htmltree)
logging.info('Got {} entries'.format(
len(result_entries)
))
for entry in result_entries:
logging.info('Working on entry {}'.format(entry))
tmp_entry = {}
for property in self._entry_selector_properties:
if property not in config_entries:
logging.info("No config found for {}".format(property))
continue
logging.info('Working on prop {}'.format(property))
args = {
'selector': config_entries[property],
}
if property in self._dates:
args['is_date'] = True
selector = Selector(**args)
logging.info(
"Using selector \"{}\" with selector type {}".format(
selector.selector,
selector.SelectorType.__class__.__name__,
)
)
tmp_entry[property] = selector.result(entry)
_entries.append(tmp_entry)
return _entries
def _parse_properties(self, htmltree):
# Populate properties from selectors
_properties = {}
config_props = self.config['properties']
# Search for each property option
for property in self._selector_properties:
if property not in config_props:
logging.info("No config found for {}".format(property))
continue
logging.info('Working on {}'.format(property))
args = {
'selector': config_props[property],
}
if property in self._dates:
args['is_date'] = True
selector = Selector(**args)
logging.info("Using selector \"{}\" with selector type {}".format(
selector.selector,
selector.SelectorType.__class__.__name__,
))
result_text = selector.result(htmltree)
logging.info('Found prop value {}'.format(result_text))
_properties[property] = result_text
return _properties
if __name__ == '__main__':
import yaml
import pprint
logging.basicConfig(level=logging.DEBUG)
with open('example.yaml', 'r') as f:
config = yaml.load(f, Loader=yaml.BaseLoader)
with open('example.html', 'r') as f:
html = f.read()
feed = Feed(config['feeds']['example'])
feed.parse(html)
print("Properties")
pp = pprint.PrettyPrinter()
pp.pprint(feed.properties)
print("Entries")
pp.pprint(feed.entries)