1 | #!/usr/bin/env python |
||
2 | # -*- coding: utf8 -*- |
||
3 | # |
||
4 | # common.py : common tools used by versions.py modules |
||
5 | # |
||
6 | # (C) Copyright 2016 - 2018 Olivier Delhomme |
||
7 | # e-mail : [email protected] |
||
8 | # |
||
9 | # This program is free software; you can redistribute it and/or modify |
||
10 | # it under the terms of the GNU General Public License as published by |
||
11 | # the Free Software Foundation; either version 3, or (at your option) |
||
12 | # any later version. |
||
13 | # |
||
14 | # This program is distributed in the hope that it will be useful, |
||
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
17 | # GNU General Public License for more details. |
||
18 | # |
||
19 | # You should have received a copy of the GNU General Public License |
||
20 | # along with this program; if not, write to the Free Software Foundation, |
||
21 | # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
||
22 | # |
||
23 | 1 | import feedparser |
|
24 | 1 | import time |
|
25 | |||
26 | |||
27 | 1 | def get_entry_published_date(entry): |
|
28 | """ |
||
29 | Returns the published date of an entry. |
||
30 | Selects the right field to do so |
||
31 | """ |
||
32 | |||
33 | 1 | if 'published_parsed' in entry: |
|
34 | 1 | published_date = entry.published_parsed |
|
35 | 1 | elif 'updated_parsed' in entry: |
|
36 | 1 | published_date = entry.updated_parsed |
|
37 | elif 'pubDate' in entry: # rss-0.91.dtd (netscape) |
||
38 | published_date = entry.pubDate |
||
39 | |||
40 | 1 | return published_date |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
41 | |||
42 | # End of get_entry_published_date() function |
||
43 | |||
44 | |||
45 | 1 | def make_list_of_newer_feeds(feed, feed_info, debug): |
|
46 | """ |
||
47 | Compares feed entries and keep those that are newer than the latest |
||
48 | check we've done and inserting the newer ones in reverse order in |
||
49 | a list to be returned |
||
50 | """ |
||
51 | |||
52 | 1 | feed_list = [] |
|
53 | |||
54 | # inserting into a list in reverse order to keep the most recent |
||
55 | # version in case of multiple release of the same project in the |
||
56 | # feeds |
||
57 | 1 | for a_feed in feed.entries: |
|
58 | |||
59 | 1 | if a_feed: |
|
60 | 1 | published_date = get_entry_published_date(a_feed) |
|
61 | |||
62 | 1 | print_debug(debug, u'\tFeed entry ({0}): Feed title: "{1:16}"'.format(time.strftime('%x %X', published_date), a_feed.title)) |
|
63 | |||
64 | 1 | if feed_info.is_newer(published_date): |
|
65 | 1 | feed_list.insert(0, a_feed) |
|
66 | else: |
||
67 | 1 | print(u'Warning: empty feed in {}'.format(feed)) |
|
68 | |||
69 | 1 | return feed_list |
|
70 | |||
71 | # End of make_list_of_newer_feeds() function |
||
72 | |||
73 | |||
74 | 1 | def manage_http_status(feed, url): |
|
75 | """ |
||
76 | Manages http status code present in feed and prints |
||
77 | an error in case of a 3xx, 4xx or 5xx and stops |
||
78 | doing anything for the feed by returning None. |
||
79 | """ |
||
80 | |||
81 | 1 | err = feed.status / 100 |
|
82 | |||
83 | 1 | if err > 2: |
|
84 | 1 | print(u'Error {} while fetching "{}".'.format(feed.status, url)) |
|
85 | 1 | feed = None |
|
86 | |||
87 | 1 | return feed |
|
88 | |||
89 | # End of manage_http_status() function |
||
90 | |||
91 | |||
92 | 1 | def manage_non_http_errors(feed, url): |
|
93 | """ |
||
94 | Tries to manage non http errors and gives |
||
95 | a message to the user. |
||
96 | """ |
||
97 | |||
98 | 1 | if feed.bozo: |
|
99 | 1 | if feed.bozo_exception: |
|
100 | 1 | exc = feed.bozo_exception |
|
101 | 1 | if hasattr(exc, 'reason'): |
|
102 | 1 | message = exc.reason |
|
103 | else: |
||
104 | message = 'unaddressed' |
||
105 | |||
106 | 1 | print(u'Error {} while fetching "{}".'.format(message, url)) |
|
107 | |||
108 | else: |
||
109 | print(u'Error while fetching url "{}".'.format(url)) |
||
110 | |||
111 | # End of manage_non_http_errors() function |
||
112 | |||
113 | |||
114 | 1 | def get_feed_entries_from_url(url): |
|
115 | """ |
||
116 | Gets feed entries from an url that should be an |
||
117 | RSS or Atom feed. |
||
118 | >>> get_feed_entries_from_url("http://delhomme.org/notfound.html") |
||
119 | Error 404 while fetching "http://delhomme.org/notfound.html". |
||
120 | >>> feed = get_feed_entries_from_url("http://blog.delhomme.org/index.php?feed/atom") |
||
121 | >>> feed.status |
||
122 | 200 |
||
123 | """ |
||
124 | |||
125 | 1 | feed = feedparser.parse(url) |
|
126 | |||
127 | 1 | if 'status' in feed: |
|
128 | 1 | feed = manage_http_status(feed, url) |
|
129 | else: |
||
130 | # An error happened such that the feed does not contain an HTTP response |
||
131 | 1 | manage_non_http_errors(feed, url) |
|
132 | 1 | feed = None |
|
133 | |||
134 | 1 | return feed |
|
135 | |||
136 | # End of get_feed_entries_from_url() function |
||
137 | |||
138 | |||
139 | 1 | def print_project_version(project, version): |
|
140 | """ |
||
141 | Prints to the standard output project name and it's version. |
||
142 | """ |
||
143 | |||
144 | 1 | print(u'{} {}'.format(project, version)) |
|
145 | |||
146 | # End of print_project_version() function |
||
147 | |||
148 | |||
149 | 1 | def print_debug(debug, message): |
|
150 | """ |
||
151 | Prints 'message' if debug mode is True |
||
152 | """ |
||
153 | |||
154 | 1 | if debug: |
|
155 | 1 | print(u'{}'.format(message)) |
|
156 | |||
157 | # End of print_debug() function |
||
158 |