|
|
@ -15,22 +15,52 @@ class WikiApp(Flask): |
|
|
|
self.route('/<string:title>', methods=['GET'])(self.page_content) |
|
|
|
self.route('/favicon.ico')(self.favicon) |
|
|
|
|
|
|
|
def fetch_pages(self, category): |
|
|
|
# Make a request to MediaWiki API using ask action to get all pages in the specified category |
|
|
|
def fetch_pages_cat(self, category_page_list): |
|
|
|
all_pages_string = '|'.join(page for pages in category_page_list.values() for page in pages) |
|
|
|
thumb_resp = requests.get(self.MEDIAWIKI_BASE_URL + self.BASE_API, params={ |
|
|
|
'action': 'query', |
|
|
|
'titles': all_pages_string, |
|
|
|
'format': 'json', |
|
|
|
'prop': 'pageimages', |
|
|
|
'pithumbsize': 700 |
|
|
|
}) |
|
|
|
thumb_data = thumb_resp.json() |
|
|
|
pages_thumb_data = thumb_data.get('query', {}).get('pages', {}) |
|
|
|
|
|
|
|
for key, value in pages_thumb_data.items(): |
|
|
|
title = value.get('title') |
|
|
|
pageid = value.get('pageid') |
|
|
|
source = value.get('thumbnail', {}).get('source') |
|
|
|
for category, pages in category_page_list.items(): |
|
|
|
if title in pages: |
|
|
|
|
|
|
|
category_page_list[category][pages.index(title)] = {'title': title, 'pageid': pageid, 'source': source} |
|
|
|
return category_page_list |
|
|
|
|
|
|
|
|
|
|
|
def fetch_all_pages(self, categories): |
|
|
|
category_page_list = {} |
|
|
|
|
|
|
|
for category in categories: |
|
|
|
response = requests.get(self.MEDIAWIKI_BASE_URL + self.BASE_API, params={'action': 'ask', 'query': '[[Concept:'+category+']]', 'format': 'json', 'formatversion': '2'}) |
|
|
|
data = response.json() |
|
|
|
|
|
|
|
# Extract page titles |
|
|
|
page_titles = [page['fulltext'] for page in data['query']['results'].values()] |
|
|
|
return page_titles |
|
|
|
category_page_list[category] = page_titles |
|
|
|
return category_page_list |
|
|
|
|
|
|
|
|
|
|
|
def homepage(self): |
|
|
|
# Fetch pages for articles, projects, and newsletters |
|
|
|
articles = self.fetch_pages('Articles') |
|
|
|
projects = self.fetch_pages('Projects') |
|
|
|
newsletters = self.fetch_pages('Newsletters') |
|
|
|
nav_elements = self.fetch_pages('MainNavigation') |
|
|
|
categories = ['Articles', 'Projects', 'Newsletters', 'MainNavigation'] |
|
|
|
category_page_list = self.fetch_all_pages(categories) |
|
|
|
print(category_page_list) |
|
|
|
updated_cat_list = self.fetch_pages_cat(category_page_list) |
|
|
|
print(updated_cat_list) |
|
|
|
|
|
|
|
articles = updated_cat_list.get('Articles', []) |
|
|
|
projects = updated_cat_list.get('Projects', []) |
|
|
|
newsletters = updated_cat_list.get('Newsletters', []) |
|
|
|
nav_elements = updated_cat_list.get('MainNavigation', []) |
|
|
|
|
|
|
|
return render_template('homepage.html', articles=articles, projects=projects, newsletters=newsletters, nav_elements=nav_elements) |
|
|
|
|
|
|
@ -38,7 +68,6 @@ class WikiApp(Flask): |
|
|
|
# Make a request to MediaWiki API to get content of a specific page |
|
|
|
response = requests.get(self.MEDIAWIKI_BASE_URL + self.BASE_API, params={'action': 'parse', 'page': title, 'format': 'json'}) |
|
|
|
data = response.json() |
|
|
|
print(title) |
|
|
|
# Extract page title and content |
|
|
|
page_title = data['parse']['title'] |
|
|
|
page_content = data['parse']['text']['*'] |
|
|
|