From 06aa2abdf122da935045e604505436e2d3c2650f Mon Sep 17 00:00:00 2001 From: Michael Scherer Date: Oct 15 2019 21:18:59 +0000 Subject: [PATCH 1/2] Add a static file for magazine Rather than doing a request every time for Fedora magazine, we are just going to get a snapshot from the website. Since the website is rebuilt every hour, this shouldn't cause problem. Fix https://pagure.io/fedora-web/websites/issue/56 --- diff --git a/.gitignore b/.gitignore index a8c2012..d0501ae 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ bundle.css *.po *.pot translations +sites/getfedora.org/static/magazine.json diff --git a/build-prod.sh b/build-prod.sh index b79601b..745a784 100755 --- a/build-prod.sh +++ b/build-prod.sh @@ -38,6 +38,11 @@ do then ./scripts/pull-translations.sh fi + if [ -x ./scripts/pull-static.sh ]; + then + ./scripts/pull-static.sh + fi + ${PYBINARY} main.py # This intermediate step is to make sure the final mv is atomic. # This means that syncs can happen at any point in time and they have a larger chance to be fine. diff --git a/sites/getfedora.org/main.py b/sites/getfedora.org/main.py index f12f896..1884ceb 100644 --- a/sites/getfedora.org/main.py +++ b/sites/getfedora.org/main.py @@ -223,6 +223,11 @@ export_route('sponsors', '/sponsors/') def releases_json(): return send_from_directory('static', 'releases.json') + +@app.route('/magazine.json') +def magazine_json(): + return send_from_directory('static', 'magazine.json') + @app.route('/static/fedora.gpg') def gpgkey(): return send_from_directory('static', 'fedora.gpg') diff --git a/sites/getfedora.org/scripts/pull-static.sh b/sites/getfedora.org/scripts/pull-static.sh new file mode 100755 index 0000000..40c0e87 --- /dev/null +++ b/sites/getfedora.org/scripts/pull-static.sh @@ -0,0 +1,2 @@ +#!/bin/bash +curl -s 'https://fedoramagazine.org/wp-json/wp/v2/posts?per_page=3' -o static/magazine.json diff --git a/sites/static/js/magazine.js b/sites/static/js/magazine.js index 680d10a..8281ed2 100644 --- a/sites/static/js/magazine.js +++ b/sites/static/js/magazine.js @@ -3,7 +3,7 @@ $( document ).ready(function() { if(!$('#magazineposts').length) return; - $.get( "https://fedoramagazine.org/wp-json/wp/v2/posts", { per_page: 3 } ) + $.get( "/magazine.json") .done(function( data ) { $.each(data, function(i, item){ const postlink = item.link; From ea42648d0b89f4622b26d23cc076869366e21d7b Mon Sep 17 00:00:00 2001 From: Michael Scherer Date: Oct 15 2019 23:27:22 +0000 Subject: [PATCH 2/2] Pregenerate a json file instead of doing API calls for image --- diff --git a/sites/getfedora.org/scripts/pull-magazine.py b/sites/getfedora.org/scripts/pull-magazine.py new file mode 100644 index 0000000..93c9bde --- /dev/null +++ b/sites/getfedora.org/scripts/pull-magazine.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +import json +import requests +# do not remove the line, it avoid triggering the IDS from wpengine +headers = {'user-agent': 'getfedora-builder/0.0.1'} +params = {'per_page': '3'} + +r = requests.get('https://fedoramagazine.org/wp-json/wp/v2/posts', params=params, headers=headers) +f = open('static/magazine.json', 'w') +posts = [] +for i in r.json()[0:3]: + p = {} + p['link'] = i['link'] + p['title'] = i['title']['rendered'] + p['date'] = i['date'] + + #['wp:featuredmedia'][0].href: + image_url = i['_links']['wp:featuredmedia'][0]['href'] + r2 = requests.get(image_url,headers=headers) + p['image_url'] = r2.json()['media_details']['sizes']['medium_large']['source_url'] + posts.append(p) + +f.write(json.dumps(posts)) diff --git a/sites/getfedora.org/scripts/pull-static.sh b/sites/getfedora.org/scripts/pull-static.sh index 40c0e87..23de0a7 100755 --- a/sites/getfedora.org/scripts/pull-static.sh +++ b/sites/getfedora.org/scripts/pull-static.sh @@ -1,2 +1,2 @@ #!/bin/bash -curl -s 'https://fedoramagazine.org/wp-json/wp/v2/posts?per_page=3' -o static/magazine.json +python $(dirname $0)/pull-magazine.py diff --git a/sites/static/js/magazine.js b/sites/static/js/magazine.js index 8281ed2..f348938 100644 --- a/sites/static/js/magazine.js +++ b/sites/static/js/magazine.js @@ -7,13 +7,12 @@ $( document ).ready(function() { .done(function( data ) { $.each(data, function(i, item){ const postlink = item.link; - const posttitle = item.title.rendered; + const posttitle = item.title; const date = new Date(item.date); const month = date.toLocaleString('en-us', { month: 'long' }); const postdate = ( month + ' ' + date.getDate()+', ' +date.getFullYear()); - $.get(item._links['wp:featuredmedia'][0].href).done(function(imagedata){ - $("#magazineposts").append("
"+posttitle+"
"+postdate+"
") - }); - }) + const image_url = item.image_url; + $("#magazineposts").append("
"+posttitle+"
"+postdate+"
"); + }); }); });