forked from qgis/QGIS-Website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_feeds.py
executable file
·204 lines (179 loc) · 6.08 KB
/
fetch_feeds.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
# Simple script to get sustaining members and
# other feeds and update our web site with them
# (c) Tim Sutton, 2023
import requests
import argparse
import json
import shutil
import os
from rss_parser import Parser
from requests import get
import atoma
from dateutil.parser import parse as date_parse
try:
import urlparse
except ImportError:
from urllib.parse import urlparse
### Funders
def fetch_funders():
response = requests.get("https://changelog.qgis.org/en/qgis/members/json/")
data = json.loads(response.text)
items = data["rss"]["channel"]["item"]
for item in items:
link = item["member_url"]
image_url = item["image_url"]
title = item["title"]
level = item["member_level"]
country = item["member_country"]
start_date = item["start_date"]
end_date = item["end_date"]
start_date = date_parse(start_date, fuzzy_with_tokens=True)[0]
start_date = start_date.strftime("%Y-%m-%d")
end_date = date_parse(end_date, fuzzy_with_tokens=True)[0]
end_date = end_date.strftime("%Y-%m-%d")
path = urlparse(image_url).path
image_ext = os.path.splitext(path)[1]
name = os.path.basename(os.path.normpath(link))
image_name = "%s.%s" % (name, image_ext)
image_name = image_name.replace("..",".")
content = f"""---
level: "{level}"
title: "{title}"
logo: "{image_name}"
startDate: "{start_date}"
endDate: "{end_date}"
link: "{link}"
country: "{country}"
---
"""
markdown_filename = f"content/funders/{name}.md"
with open(markdown_filename , "w", encoding="utf=8") as f:
f.write(content)
print(f"Writing: {markdown_filename}")
response = requests.get(image_url, stream=True)
image_filename = f"content/funders/{image_name}"
with open(image_filename, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
print(f"Writing: {image_filename}")
del response
### Flickr screenshots
def fetch_flickr_screenshots(showcase_type, rss_url):
response = requests.get(rss_url)
feed = atoma.parse_atom_bytes(response.content)
#print(feed.description)
print(feed.title.value)
for entry in feed.entries:
print('----')
#print(dir(entry))
print(entry.title.value)
for author in entry.authors:
print(author.name)
print(author.uri)
image_url = entry.links[len(entry.links)-1].href
print(image_url)
path = urlparse(image_url).path
image_ext = os.path.splitext(path)[1]
name = os.path.basename(os.path.normpath(image_url))
image_name = "%s.%s" % (name, image_ext)
image_name = image_name.replace("..",".")
entry_date = entry.published.strftime("%Y-%m-%d")
print(entry_date)
content = f"""---
source: "flickr"
title: "{entry.title.value}"
image: "{image_name}"
date: "{entry_date}"
link: "{image_url}"
draft: "true"
showcase: "{showcase_type}"
---
"""
markdown_filename = f"content/flickr-images/{name}.md"
with open(markdown_filename , "w", encoding="utf=8") as f:
f.write(content)
print(f"Writing: {markdown_filename}")
response = requests.get(image_url, stream=True)
image_filename = f"content/flickr-images/{image_name}"
with open(image_filename, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
print(f"Writing: {image_filename}")
del response
# Blogs with atom feeds
def fetch_blog_feed(showcase_type, rss_url):
response = requests.get(rss_url)
feed = atoma.parse_atom_bytes(response.content)
#print(feed.description)
print(feed.title.value)
for entry in feed.entries:
print('----')
#print(dir(entry))
print(entry.title.value)
for author in entry.authors:
print(author.name)
print(author.uri)
image_url = entry.links[len(entry.links)-1].href
print(image_url)
path = urlparse(image_url).path
image_ext = os.path.splitext(path)[1]
name = os.path.basename(os.path.normpath(image_url))
image_name = "%s.%s" % (name, image_ext)
image_name = image_name.replace("..",".")
try:
entry_date = entry.published.strftime("%Y-%m-%d")
except:
entry_date = ""
try:
summary = entry.summary.value
except:
summary = ""
content = f"""---
source: "blog"
title: "{entry.title.value}"
image: "{image_name}"
date: "{entry_date}"
link: "{image_url}"
draft: "true"
showcase: "{showcase_type}"
---
{summary}
"""
markdown_filename = f"content/community-blogs/{name}.md"
with open(markdown_filename , "w", encoding="utf=8") as f:
f.write(content)
print(f"Writing: {markdown_filename}")
response = requests.get(image_url, stream=True)
image_filename = f"content/community-blogs/{image_name}"
with open(image_filename, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
print(f"Writing: {image_filename}")
del response
parser = argparse.ArgumentParser(description='Import items from various feeds.')
parser.add_argument(
"--flickr",
help="Import flickr items (defaults to no)",
default="no",
type=bool,
required=False)
parser.parse_args()
args = parser.parse_args()
fetch_funders()
if args.flickr:
fetch_flickr_screenshots(
showcase_type="map",
rss_url = "https://api.flickr.com/services/feeds/groups_pool.gne?id=2244553@N22&lang=en-us&format=atom"
)
fetch_flickr_screenshots(
showcase_type="screenshot",
rss_url = "https://api.flickr.com/services/feeds/groups_pool.gne?id=2327386@N22&lang=en-us&format=atom"
)
# Planet blog aggregator
fetch_blog_feed(
showcase_type="planet",
rss_url="https://plugins.qgis.org/planet/feed/atom/"
)
# QGIS User group feed
fetch_blog_feed(
showcase_type="qug",
rss_url="https://raw.githubusercontent.com/qgis/QGIS-Website/master/source/feeds/qugsnews.atom"
)