Skip to content

Commit

Permalink
feat: download phet labs in requested language
Browse files Browse the repository at this point in the history
closes #78
  • Loading branch information
juancarlosfarah committed May 13, 2019
1 parent a4c3adf commit 3f2c432
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 37 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@sentry/browser": "5.1.1",
"@sentry/electron": "0.17.1",
"archiver": "3.0.0",
"cheerio": "1.0.0-rc.3",
"classnames": "2.2.6",
"connected-react-router": "6.4.0",
"electron-devtools-installer": "2.2.4",
Expand Down Expand Up @@ -83,6 +84,7 @@
"redux-devtools-extension": "2.13.8",
"redux-promise": "0.6.0",
"redux-thunk": "2.3.0",
"request-promise": "4.2.4",
"rimraf": "2.6.3",
"universal-analytics": "0.4.20",
"wait-on": "3.2.0"
Expand Down
5 changes: 1 addition & 4 deletions public/app/config/mapping.js
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
module.exports = {
'https://gateway.golabz.eu/os/pub/phet/http%25253A%25252F%25252Fphet.colorado.edu%25252Fen%25252Fsimulation%25252Fwave-on-a-string/w_default.html':
'https://apps.graasp.eu/5acb589d0d5d9464081c2d46/5ccae1f6203454853b0aabcb/latest/index.html',
};
module.exports = {};
1 change: 1 addition & 0 deletions public/app/config/providers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = ['gateway.golabz.eu'];
27 changes: 27 additions & 0 deletions public/app/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const request = require('request-promise');
const cheerio = require('cheerio');
const providers = require('./config/providers');

const getDownloadUrl = async ({ url, lang }) => {
let proxied = false;
providers.forEach(provider => {
if (url.includes(provider)) {
proxied = true;
}
});
if (!proxied) {
return false;
}
const res = await request(url);
const $ = cheerio.load(res);
const elem = $(`meta[name="download"][language="${lang}"]`);
if (elem) {
return elem.attr('value');
}
// todo: fall back on another language if requested is not available?
return false;
};

module.exports = {
getDownloadUrl,
};
9 changes: 9 additions & 0 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const Sentry = require('@sentry/electron');
const ua = require('universal-analytics');
const { machineIdSync } = require('node-machine-id');
const logger = require('./app/logger');
const { getDownloadUrl } = require('./app/download');
const {
getExtension,
isDownloadable,
Expand Down Expand Up @@ -252,6 +253,7 @@ app.on('ready', async () => {
try {
// get handle to spaces collection
const spaces = db.get(SPACES_COLLECTION);
const lang = db.get('user.lang').value();
const { id } = space;
const existingSpace = spaces.find({ id }).value();

Expand Down Expand Up @@ -323,6 +325,13 @@ app.on('ready', async () => {
url = mapping[url];
}

// download from proxy url if available
// eslint-disable-next-line no-await-in-loop
const downloadUrl = await getDownloadUrl({ url, lang });
if (downloadUrl) {
url = downloadUrl;
}

// generate hash and get extension to save file
const hash = generateHash(resource);
const ext = getExtension(resource);
Expand Down
62 changes: 31 additions & 31 deletions src/config/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import fr from '../langs/fr';

i18n.use(initReactI18next).init({
resources: {
en_all: en,
fr_all: fr,
en_ALL: en,
fr_ALL: fr,
},
fallbackLng: 'en_all',
fallbackLng: 'en_ALL',
// debug only when not in production
debug: process.env.NODE_ENV !== 'production',
ns: ['translations'],
Expand All @@ -24,34 +24,34 @@ i18n.use(initReactI18next).init({
});

const langs = {
// bg_all: "български",
// ca_all: "Català",
// cs_all: "čeština",
// de_all: "Deutsch",
// el_all: "Ελληνικά",
en_all: 'English',
// es_all: "Español",
// et_all: "Eesti",
// fi_all: "Suomi",
fr_all: 'Français',
// hu_all: "Magyar",
// it_all: "Italiano",
// ja_all: "日本語",
// ka_all: "ქართული",
// lt_all: "lietuvių kalba",
// lv_all: "Latviešu",
// nl_all: "Nederlands",
// pt_all: "Português",
// ro_all: "Română",
// ru_all: "Русский",
// sk_all: "Slovenský",
// sl_all: "Slovenščina",
// sr_all: "српски језик",
// sw_all: "Kiswahili",
// tr_all: "Türkçe",
// uk_all: "Українська",
// vi_all: "Tiếng Việt",
// zh_all: "简体中文",
// bg_ALL: "български",
// ca_ALL: "Català",
// cs_ALL: "čeština",
// de_ALL: "Deutsch",
// el_ALL: "Ελληνικά",
en_ALL: 'English',
// es_ALL: "Español",
// et_ALL: "Eesti",
// fi_ALL: "Suomi",
fr_ALL: 'Français',
// hu_ALL: "Magyar",
// it_ALL: "Italiano",
// ja_ALL: "日本語",
// ka_ALL: "ქართული",
// lt_ALL: "lietuvių kalba",
// lv_ALL: "Latviešu",
// nl_ALL: "Nederlands",
// pt_ALL: "Português",
// ro_ALL: "Română",
// ru_ALL: "Русский",
// sk_ALL: "Slovenský",
// sl_ALL: "Slovenščina",
// sr_ALL: "српски језик",
// sw_ALL: "Kiswahili",
// tr_ALL: "Türkçe",
// uk_ALL: "Українська",
// vi_ALL: "Tiếng Việt",
// zh_ALL: "简体中文",
// zh_tw: "繁體中文",
};

Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3205,7 +3205,7 @@ charenc@~0.0.1:
resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667"
integrity sha1-wKHS86cJLgN3S/qD8UwPxXkKhmc=

cheerio@^1.0.0-rc.2:
cheerio@1.0.0-rc.3, cheerio@^1.0.0-rc.2:
version "1.0.0-rc.3"
resolved "https://registry.yarnpkg.com/cheerio/-/cheerio-1.0.0-rc.3.tgz#094636d425b2e9c0f4eb91a46c05630c9a1a8bf6"
integrity sha512-0td5ijfUPuubwLUu0OBoe98gZj8C/AA+RW3v67GPlGOrvxWjZmBXiBCRU+I8VEiNyJzjth40POfHiz2RB3gImA==
Expand Down Expand Up @@ -11317,7 +11317,7 @@ request-promise-native@^1.0.5:
stealthy-require "^1.1.1"
tough-cookie "^2.3.3"

request-promise@^4.x:
request-promise@4.2.4, request-promise@^4.x:
version "4.2.4"
resolved "https://registry.yarnpkg.com/request-promise/-/request-promise-4.2.4.tgz#1c5ed0d71441e38ad58c7ce4ea4ea5b06d54b310"
integrity sha512-8wgMrvE546PzbR5WbYxUQogUnUDfM0S7QIFZMID+J73vdFARkFy+HElj4T+MWYhpXwlLp0EQ8Zoj8xUA0he4Vg==
Expand Down

0 comments on commit 3f2c432

Please sign in to comment.