Update preview download to get link from GitHub releases (#344)

No longer downloads from tachiyomi.kanade.eu
This commit is contained in:
Andreas E 2020-09-13 14:26:42 +02:00 committed by GitHub
parent 2773ddf63c
commit cc37f7f7b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 135 additions and 46 deletions

View File

@ -15,7 +15,7 @@
<script> <script>
import CloudDownloadIcon from "vue-material-design-icons/CloudDownload.vue"; import CloudDownloadIcon from "vue-material-design-icons/CloudDownload.vue";
import BugIcon from "vue-material-design-icons/Bug.vue"; import BugIcon from "vue-material-design-icons/Bug.vue";
import { GITHUB_LATEST_RELEASE, KANADE_LATEST } from "../constants"; import { GITHUB_LATEST_RELEASE, GITHUB_PREVIEW_RELEASE } from "../constants";
export default { export default {
components: { components: {
@ -27,6 +27,8 @@ export default {
return { return {
tagName: "", tagName: "",
browserDownloadUrl: "", browserDownloadUrl: "",
previewTagName: "",
previewbrowserDownloadUrl: "",
}; };
}, },
@ -39,6 +41,14 @@ export default {
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }
try {
const { data } = await this.$store.dispatch("getPreviewReleaseData");
const apkAsset = data.assets.find((a) => a.name.includes(".apk"));
this.$data.previewTagName = data.tag_name.slice(1);
this.$data.previewbrowserDownloadUrl = apkAsset.browser_download_url;
} catch (e) {
console.error(e);
}
}, },
methods: { methods: {
@ -95,7 +105,7 @@ export default {
popup: "animate__animated animate__faster animate__zoomOut", popup: "animate__animated animate__faster animate__zoomOut",
}, },
}); });
window.location.assign(KANADE_LATEST); window.location.assign(this.$data.previewbrowserDownloadUrl || GITHUB_PREVIEW_RELEASE);
window.ga("send", "event", "Action", "Download", "Tachiyomi Preview"); window.ga("send", "event", "Action", "Download", "Tachiyomi Preview");
} }
}); });

View File

@ -1,4 +1,5 @@
export const GITHUB_EXTENSION_JSON = "https://raw.githubusercontent.com/inorichi/tachiyomi-extensions/repo/index.json"; export const GITHUB_EXTENSION_JSON = "https://raw.githubusercontent.com/inorichi/tachiyomi-extensions/repo/index.json";
export const GITHUB_LATEST_RELEASE = "https://github.com/inorichi/tachiyomi/releases/latest"; export const GITHUB_LATEST_RELEASE = "https://github.com/inorichi/tachiyomi/releases/latest";
export const GITHUB_LATEST_API = "https://api.github.com/repos/inorichi/tachiyomi/releases/latest"; export const GITHUB_LATEST_API = "https://api.github.com/repos/inorichi/tachiyomi/releases/latest";
export const KANADE_LATEST = "https://tachiyomi.kanade.eu/latest"; export const GITHUB_PREVIEW_API = "https://api.github.com/repos/tachiyomiorg/android-app-preview/releases/latest";
export const GITHUB_PREVIEW_RELEASE = "https://github.com/tachiyomiorg/android-app-preview/releases/latest";

View File

@ -2,67 +2,145 @@ import Vue from "vue";
import Vuex from "vuex"; import Vuex from "vuex";
import axios from "axios"; import axios from "axios";
import { GITHUB_LATEST_API } from "../constants"; import { GITHUB_LATEST_API, GITHUB_PREVIEW_API } from "../constants";
let call = null; const worker = (function () {
const networkMap = new Map();
// Makes sure there is only one network call to API
// Everyone else wait for promise
function _getDataFromGithub(name, url) {
if (networkMap.has(name)) {
return networkMap.get(name);
}
const call = axios
.get(url)
.then((value) => {
networkMap.delete(name);
return Promise.resolve(value);
})
.catch((reason) => {
networkMap.delete(name);
return Promise.reject(reason);
});
networkMap.set(name, call);
return call;
}
const dataMap = new Map();
const now = new Date().getTime();
// Makes sure there is only one mutation
// Everyone else wait for promise
function _getData(store, name, type, url) {
if (dataMap.has(name)) {
return dataMap.get(name);
}
const promise = _getDataFromGithub(name, url)
.then(({ data }) => {
const object = {
updated: now,
data,
};
store.commit({
type,
object,
});
dataMap.delete(name);
return Promise.resolve();
})
.catch((reason) => {
const object = {
updated: null,
data: null,
};
store.commit({
type,
object,
});
dataMap.delete(name);
return Promise.reject(reason);
});
dataMap.set(name, promise);
return promise;
}
return {
getPreviewData(store, name) {
return new Promise((resolve, reject) => {
_getData(store, name, "setPreviewReleaseData", GITHUB_PREVIEW_API)
.then(() => {
resolve(store.state.preview);
})
.catch((reason) => {
reject(reason);
});
});
},
getStableData(store, name) {
return new Promise((resolve, reject) => {
_getData(store, name, "setStableReleaseData", GITHUB_LATEST_API)
.then(() => {
resolve(store.state.stable);
})
.catch((reason) => {
reject(reason);
});
});
},
};
})();
Vue.use(Vuex); Vue.use(Vuex);
export default new Vuex.Store({ export default new Vuex.Store({
state: { state: {
stable: { stable: {
updated: null, updated: null,
data: null, data: null,
}, },
preview: {
updated: null,
data: null,
},
}, },
mutations: { mutations: {
setStableReleaseData(state, stable) { setStableReleaseData(state, { object }) {
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
state.stable = stable; state.stable = object;
},
setPreviewReleaseData(state, { object }) {
// eslint-disable-next-line no-param-reassign
state.preview = object;
}, },
}, },
actions: { actions: {
getStableReleaseData({ commit }) { getStableReleaseData() {
return new Promise((resolve, reject) => { const { updated } = this.state.stable;
const { updated } = this.state.stable; const now = new Date().getTime();
const now = new Date().getTime();
if (updated != null && now - updated <= 60 * 60 * 24 * 1000) { if (updated != null && now - updated <= 60 * 60 * 24 * 1000) {
return resolve(this.state.stable); return Promise.resolve(this.state.stable);
} }
// Makes sure only one network call and one mutation happens return worker.getStableData(this, "stable");
if (call == null) { },
call = axios getPreviewReleaseData() {
.get(GITHUB_LATEST_API) const { updated } = this.state.preview;
.then(({ data }) => { const now = new Date().getTime();
const object = {
updated: now,
data,
};
commit("setStableReleaseData", object);
call = null;
return Promise.resolve(this.state.stable);
})
.catch((reason) => {
const object = {
updated: null,
data: null,
};
commit("setStableReleaseData", object);
call = null;
return Promise.reject(reason);
});
}
// Waits for network call and mutation to be done if (updated != null && now - updated <= 60 * 60 * 24 * 1000) {
return call return Promise.resolve(this.state.preview);
.then((value) => { }
return resolve(value);
}) return worker.getPreviewData(this, "preview");
.catch((reason) => {
return reject(reason);
});
});
}, },
}, },
}); });