Skip to content

A full-featured download manager.

refactor: fetch bt tracker data

Dr_rOotd56983d45de5
diff --git a/src/renderer/store/modules/preference.js b/src/renderer/store/modules/preference.js
index efad854f..c6a4e91a 100644
--- a/src/renderer/store/modules/preference.js
+++ b/src/renderer/store/modules/preference.js
@@ -1,4 +1,5 @@
import api from '@/api'
+import { fetchBtTrackerFromSource } from '@shared/utils/tracker'
import { isEmpty } from 'lodash'
const state = {
@@ -35,7 +36,7 @@ const actions = {
commit('UPDATE_PREFERENCE_DATA', { theme })
},
fetchBtTracker (_, trackerSource = []) {
- return api.fetchBtTrackerFromGitHub(trackerSource)
+ return fetchBtTrackerFromSource(trackerSource)
},
toggleEngineMode () {
diff --git a/src/shared/utils.js b/src/shared/utils/index.js
similarity index 98%
rename from src/shared/utils.js
rename to src/shared/utils/index.js
index 3ee0e26e..96c525ca 100644
--- a/src/shared/utils.js
+++ b/src/shared/utils/index.js
@@ -12,7 +12,8 @@ import {
pick
} from 'lodash'
import { resolve } from 'path'
-import { userKeys, systemKeys, needRestartKeys } from './configKeys'
+
+import { userKeys, systemKeys, needRestartKeys } from '@shared/configKeys'
import { ENGINE_RPC_HOST } from '@shared/constants'
export function bytesToSize (bytes) {
@@ -590,3 +591,11 @@ export function checkIsNeedRestart (changed = {}) {
return result
}
+
+export const checkIsNeedRun = (enable, lastTime, interval) => {
+ if (!enable) {
+ return false
+ }
+
+ return (Date.now() - lastTime > interval)
+}
diff --git a/src/shared/utils/tracker.js b/src/shared/utils/tracker.js
new file mode 100644
index 00000000..db1e1882
--- /dev/null
+++ b/src/shared/utils/tracker.js
@@ -0,0 +1,30 @@
+import {
+ isEmpty
+} from 'lodash'
+import { EMPTY_STRING } from '@shared/constants'
+import axios from 'axios'
+
+export const fetchBtTrackerFromSource = async (source) => {
+ if (isEmpty(source)) {
+ return EMPTY_STRING
+ }
+
+ const now = Date.now()
+ const promises = source.map((url) => {
+ return axios.get(`${url}?t=${now}`).then((value) => value.data)
+ })
+
+ const resp = await Promise.all(promises)
+ const result = [...new Set(resp)]
+ return result
+}
+
+export function convertTrackerDataToLine (arr = []) {
+ const result = arr.join('\r\n').replace(/^\s*[\r\n]/gm, '')
+ return result
+}
+
+export function convertTrackerDataToComma (arr = []) {
+ const result = convertTrackerDataToLine(arr).trim().replace(/(?:\r\n|\r|\n)/g, ',')
+ return result
+}