Add Trending Scraper for Local API

This commit is contained in:
Preston 2020-08-09 16:14:51 -04:00
parent 99524f3556
commit b6e2e9f73e
3 changed files with 90 additions and 4 deletions

8
package-lock.json generated
View File

@ -20088,6 +20088,14 @@
} }
} }
}, },
"yt-trending-scraper": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/yt-trending-scraper/-/yt-trending-scraper-1.0.2.tgz",
"integrity": "sha512-dRX7MZ00TM1q/lKE5cCUqmcdeSsrQ25n+OvBfVKeP3INsuW/myJyLZcJHbAG2u/Cz6Nfq4FIqzuzaEs4Lzhkbw==",
"requires": {
"axios": "^0.19.2"
}
},
"yt-xml2vtt": { "yt-xml2vtt": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/yt-xml2vtt/-/yt-xml2vtt-1.1.1.tgz", "resolved": "https://registry.npmjs.org/yt-xml2vtt/-/yt-xml2vtt-1.1.1.tgz",

View File

@ -41,6 +41,7 @@
"youtube-comments-task": "^1.3.15", "youtube-comments-task": "^1.3.15",
"youtube-suggest": "^1.1.0", "youtube-suggest": "^1.1.0",
"yt-channel-info": "^1.0.3", "yt-channel-info": "^1.0.3",
"yt-trending-scraper": "^1.0.2",
"yt-xml2vtt": "^1.1.1", "yt-xml2vtt": "^1.1.1",
"ytdl-core": "^3.2.0", "ytdl-core": "^3.2.0",
"ytpl": "^0.2.4", "ytpl": "^0.2.4",

View File

@ -3,6 +3,8 @@ import FtCard from '../../components/ft-card/ft-card.vue'
import FtLoader from '../../components/ft-loader/ft-loader.vue' import FtLoader from '../../components/ft-loader/ft-loader.vue'
import FtElementList from '../../components/ft-element-list/ft-element-list.vue' import FtElementList from '../../components/ft-element-list/ft-element-list.vue'
import ytrend from 'yt-trending-scraper'
export default Vue.extend({ export default Vue.extend({
name: 'Trending', name: 'Trending',
components: { components: {
@ -16,20 +18,78 @@ export default Vue.extend({
shownResults: [] shownResults: []
} }
}, },
computed: {
usingElectron: function () {
return this.$store.getters.getUsingElectron
},
backendPreference: function () {
return this.$store.getters.getBackendPreference
},
backendFallback: function () {
return this.$store.getters.getBackendFallback
},
invidiousInstance: function () {
return this.$store.getters.getInvidiousInstance
}
},
mounted: function () { mounted: function () {
this.getTrendingInfo() if (!this.usingElectron) {
this.getVideoInformationInvidious()
} else {
switch (this.backendPreference) {
case 'local':
this.getTrendingInfoLocal()
break
case 'invidious':
this.getTrendingInfoInvidious()
break
}
}
}, },
methods: { methods: {
getTrendingInfo: function () { getTrendingInfoLocal: function () {
this.isLoading = true this.isLoading = true
const searchPayload = { console.log('getting local trending')
ytrend.scrape_trending_page().then((result) => {
const returnData = result.filter((item) => {
return item.type === 'video' || item.type === 'channel' || item.type === 'playlist'
})
this.shownResults = this.shownResults.concat(returnData)
this.isLoading = false
}).catch((err) => {
console.log(err)
const errorMessage = this.$t('Local API Error (Click to copy)')
this.showToast({
message: `${errorMessage}: ${err}`,
time: 10000,
action: () => {
navigator.clipboard.writeText(err)
}
})
if (!this.usingElectron || (this.backendPreference === 'local' && this.backendFallback)) {
this.showToast({
message: this.$t('Falling back to Invidious API')
})
this.getTrendingInfoInvidious()
} else {
this.isLoading = false
}
})
},
getTrendingInfoInvidious: function () {
this.isLoading = true
const trendingPayload = {
resource: 'trending', resource: 'trending',
id: '', id: '',
params: {} params: {}
} }
this.$store.dispatch('invidiousAPICall', searchPayload).then((result) => { this.$store.dispatch('invidiousAPICall', trendingPayload).then((result) => {
if (!result) { if (!result) {
return return
} }
@ -44,6 +104,23 @@ export default Vue.extend({
this.isLoading = false this.isLoading = false
}).catch((err) => { }).catch((err) => {
console.log(err) console.log(err)
const errorMessage = this.$t('Invidious API Error (Click to copy)')
this.showToast({
message: `${errorMessage}: ${err}`,
time: 10000,
action: () => {
navigator.clipboard.writeText(err)
}
})
if (!this.usingElectron || (this.backendPreference === 'invidious' && this.backendFallback)) {
this.showToast({
message: this.$t('Falling back to Local API')
})
this.getTrendingInfoLocal()
} else {
this.isLoading = false
}
}) })
} }
} }