diff --git a/app.py b/app.py index 81c7c4e..bb679a2 100644 --- a/app.py +++ b/app.py @@ -14,23 +14,53 @@ class WikiApp(Flask): self.route('/', methods=['GET'])(self.homepage) self.route('/', methods=['GET'])(self.page_content) self.route('/favicon.ico')(self.favicon) + + 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', {}) - def fetch_pages(self, category): - # Make a request to MediaWiki API using ask action to get all pages in the specified category - response = requests.get(self.MEDIAWIKI_BASE_URL + self.BASE_API, params={'action': 'ask', 'query': '[[Concept:'+category+']]', 'format': 'json', 'formatversion': '2'}) - data = response.json() + 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 = {} - # Extract page titles - page_titles = [page['fulltext'] for page in data['query']['results'].values()] - return page_titles + 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() + page_titles = [page['fulltext'] for page in data['query']['results'].values()] + 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']['*'] diff --git a/templates/homepage.html b/templates/homepage.html index 930b5a9..e55c9c4 100644 --- a/templates/homepage.html +++ b/templates/homepage.html @@ -17,7 +17,7 @@ @@ -27,19 +27,19 @@
articles 
{% for article in articles %} - + {% endfor %}
newsletters
- {% for newsletter in newsletters %} - + {% for newsletter in articles %} + {% endfor %}
projects
- {% for project in projects %} - + {% for project in articles %} + {% endfor %}