#57 Add a static file for magazine
Merged 2 months ago by pfrields. Opened 2 months ago by misc.
fedora-web/ misc/websites add_static  into  master

file modified
+1

@@ -6,3 +6,4 @@ 

  *.po

  *.pot

  translations

+ sites/getfedora.org/static/magazine.json

file modified
+5

@@ -38,6 +38,11 @@ 

  		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.

@@ -223,6 +223,11 @@ 

  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')

@@ -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))

@@ -0,0 +1,2 @@ 

+ #!/bin/bash

+ python $(dirname $0)/pull-magazine.py

file modified
+5 -6

@@ -3,17 +3,16 @@ 

    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;

-        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("<div class='card'><a href='"+postlink+"'><img src='"+imagedata.media_details.sizes.medium_large.source_url+"' class='card-img-top'></a><div class='card-body font-weight-bold '><a class='text-dark' href='"+postlink+"'>"+posttitle+"</a><div><small>"+postdate+"</small></div></div></div>")

-       });

-     })

+        const image_url = item.image_url;

+        $("#magazineposts").append("<div class='card'><a href='"+postlink+"'><img src='"+image_url+"' class='card-img-top'></a><div class='card-body font-weight-bold '><a class='text-dark' href='"+postlink+"'>"+posttitle+"</a><div><small>"+postdate+"</small></div></div></div>");

+     });

    });

  });

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

I didn't test yet, but going to work now the code is out there.

rebased onto 06aa2ab

2 months ago

Ok, so I tested, it seems to work. It only solve the issue for the main API call, so this should remove ~180 000 API calls. I need to do something for the 2nd one, likely the same trick.

1 new commit added

  • Pregenerate a json file instead of doing API calls for image
2 months ago

So, now, the website is using 1 json call with all information. I kept the javascript for the widget, due to date conversion and because it was easier than redo everything, but that's likely something to see too.

I think the documentation should also be fixed, but after that PR is merged

Pull-Request has been merged by pfrields

2 months ago

Seems the CSP policy blocked the patch, I have merged a fix, waiting on deployment for now