Browse Source

fixed resp api speed

main
Cailean Finn 9 months ago
parent
commit
768b2f0036
  1. 53
      app.py
  2. 12
      templates/homepage.html

53
app.py

@ -15,22 +15,52 @@ class WikiApp(Flask):
self.route('/<string:title>', methods=['GET'])(self.page_content) self.route('/<string:title>', methods=['GET'])(self.page_content)
self.route('/favicon.ico')(self.favicon) self.route('/favicon.ico')(self.favicon)
def fetch_pages(self, category): def fetch_pages_cat(self, category_page_list):
# Make a request to MediaWiki API using ask action to get all pages in the specified category all_pages_string = '|'.join(page for pages in category_page_list.values() for page in pages)
response = requests.get(self.MEDIAWIKI_BASE_URL + self.BASE_API, params={'action': 'ask', 'query': '[[Concept:'+category+']]', 'format': 'json', 'formatversion': '2'}) thumb_resp = requests.get(self.MEDIAWIKI_BASE_URL + self.BASE_API, params={
data = response.json() '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()
page_titles = [page['fulltext'] for page in data['query']['results'].values()]
category_page_list[category] = page_titles
return category_page_list
# Extract page titles
page_titles = [page['fulltext'] for page in data['query']['results'].values()]
return page_titles
def homepage(self): def homepage(self):
# Fetch pages for articles, projects, and newsletters # Fetch pages for articles, projects, and newsletters
articles = self.fetch_pages('Articles') categories = ['Articles', 'Projects', 'Newsletters', 'MainNavigation']
projects = self.fetch_pages('Projects') category_page_list = self.fetch_all_pages(categories)
newsletters = self.fetch_pages('Newsletters') print(category_page_list)
nav_elements = self.fetch_pages('MainNavigation') 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) 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 # 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'}) response = requests.get(self.MEDIAWIKI_BASE_URL + self.BASE_API, params={'action': 'parse', 'page': title, 'format': 'json'})
data = response.json() data = response.json()
print(title)
# Extract page title and content # Extract page title and content
page_title = data['parse']['title'] page_title = data['parse']['title']
page_content = data['parse']['text']['*'] page_content = data['parse']['text']['*']

12
templates/homepage.html

@ -17,7 +17,7 @@
</div> </div>
<div class='nav-element-cont'> <div class='nav-element-cont'>
{% for nav_element in nav_elements %} {% for nav_element in nav_elements %}
<div class='nav-element'><a href="{{ url_for('page_content', title=nav_element) }}">{{ nav_element }}</a><div class='spinning-star'>🞱</div></div> <div class='nav-element'><a href="{{ url_for('page_content', title=nav_element.title) }}">{{ nav_element.title }}</a><div class='spinning-star'>🞱</div></div>
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
@ -27,19 +27,19 @@
<div class='section'> <div class='section'>
<div class='section-title'><div style="display: inline-flex; text-align: center; justify-content: center; align-items: center; line-height: 100%; padding: 3.3px; font-size: 20px; font-style: italic; object-fit: contain; font-family: monospace; color: white; text-shadow: white 0px 5px 5px; text-decoration: none;">articles&nbsp;<img src="https://i-love-everything.com/buttons/img/7.gif" style="max-height: 32px;"></div></div> <div class='section-title'><div style="display: inline-flex; text-align: center; justify-content: center; align-items: center; line-height: 100%; padding: 3.3px; font-size: 20px; font-style: italic; object-fit: contain; font-family: monospace; color: white; text-shadow: white 0px 5px 5px; text-decoration: none;">articles&nbsp;<img src="https://i-love-everything.com/buttons/img/7.gif" style="max-height: 32px;"></div></div>
{% for article in articles %} {% for article in articles %}
<div class='section-element'><a href="{{ url_for('page_content', title=article) }}">{{ article }} ↗</a></div> <div class='section-element'><a href="{{ url_for('page_content', title=article.title) }}">{{ article.title }} ↗</a></div>
{% endfor %} {% endfor %}
</div> </div>
<div class='section'> <div class='section'>
<div class='section-title'><div style="display: inline-flex; text-align: center; justify-content: center; align-items: center; line-height: 100%; padding: 2.2px; font-size: 20px; font-style: oblique; object-fit: contain; font-family: monospace; color: white; text-shadow: red 0px 2px 5px;">newsletters<br><img src="https://i-love-everything.com/buttons/img/34.gif" style="max-height: 32px;"></div></div> <div class='section-title'><div style="display: inline-flex; text-align: center; justify-content: center; align-items: center; line-height: 100%; padding: 2.2px; font-size: 20px; font-style: oblique; object-fit: contain; font-family: monospace; color: white; text-shadow: red 0px 2px 5px;">newsletters<br><img src="https://i-love-everything.com/buttons/img/34.gif" style="max-height: 32px;"></div></div>
{% for newsletter in newsletters %} {% for newsletter in articles %}
<div class='section-element'><a href="{{ url_for('page_content', title=newsletter) }}">{{ newsletter }} ↗</a></div> <div class='section-element'><a href="{{ url_for('page_content', title=newsletter.title) }}">{{ newsletter.title }} ↗</a></div>
{% endfor %} {% endfor %}
</div> </div>
<div class='section'> <div class='section'>
<div class='section-title'><div style="display: inline-flex; text-align: center; justify-content: center; align-items: center; line-height: 100%; padding: 3.3px; font-size: 20px; font-style: normal; object-fit: contain; font-family: monospace; color: white; border: 1px solid white;">projects<img src="https://i-love-everything.com/buttons/img/26.gif" style="max-height: 32px;"></div></div> <div class='section-title'><div style="display: inline-flex; text-align: center; justify-content: center; align-items: center; line-height: 100%; padding: 3.3px; font-size: 20px; font-style: normal; object-fit: contain; font-family: monospace; color: white; border: 1px solid white;">projects<img src="https://i-love-everything.com/buttons/img/26.gif" style="max-height: 32px;"></div></div>
{% for project in projects %} {% for project in articles %}
<div class='section-element'><a href="{{ url_for('page_content', title=project) }}">{{ project }} ↗</a></div> <div class='section-element'><a href="{{ url_for('page_content', title=project.title) }}">{{ project.title }} ↗</a></div>
{% endfor %} {% endfor %}
</div> </div>
</div> </div>

Loading…
Cancel
Save