Move logic to utils store and load country data on boot

This commit is contained in:
Preston 2020-10-22 14:56:49 -04:00
parent 3bb7be9ddb
commit f75df62c90
7 changed files with 71 additions and 532 deletions

6
package-lock.json generated
View File

@ -8552,7 +8552,7 @@
},
"humanize-plus": {
"version": "1.8.2",
"resolved": "http://registry.npmjs.org/humanize-plus/-/humanize-plus-1.8.2.tgz",
"resolved": "https://registry.npmjs.org/humanize-plus/-/humanize-plus-1.8.2.tgz",
"integrity": "sha1-pls0RZrWNnrbs3B6gqPJ+RYWcDA=",
"dev": true
},
@ -13899,7 +13899,7 @@
},
"safe-regex": {
"version": "1.1.0",
"resolved": "http://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz",
"resolved": "https://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz",
"integrity": "sha1-QKNmnzsHfR6UPURinhV91IAjvy4=",
"dev": true,
"requires": {
@ -15070,7 +15070,7 @@
},
"strip-eof": {
"version": "1.0.0",
"resolved": "http://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz",
"resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz",
"integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=",
"dev": true
},

View File

@ -53,6 +53,9 @@ export default Vue.extend({
}
},
computed: {
isDev: function () {
return process.env.NODE_ENV === 'development'
},
isOpen: function () {
return this.$store.getters.getIsSideNavOpen
},
@ -112,6 +115,12 @@ export default Vue.extend({
} else {
this.$i18n.locale = locale
}
const payload = {
isDev: this.isDev,
locale: this.$i18n.locale
}
this.$store.dispatch('getRegionData', payload)
},
checkThemeSettings: function () {

View File

@ -1,6 +1,5 @@
import Vue from 'vue'
import $ from 'jquery'
import FS from 'fs'
import { mapActions } from 'vuex'
import FtCard from '../ft-card/ft-card.vue'
import FtSelect from '../ft-select/ft-select.vue'
@ -25,8 +24,6 @@ export default Vue.extend({
instanceNames: [],
instanceValues: [],
currentLocale: '',
currentGeoLocation: '',
geoLocationArray: [],
backendValues: [
'invidious',
'local'
@ -54,486 +51,6 @@ export default Vue.extend({
'start',
'middle',
'end'
],
regionNames: [
'Afghanistan',
'Albania',
'Algeria',
'American Samoa',
'Andorra',
'Angola',
'Antarctica',
'Antigua And Barbuda',
'Argentina',
'Armenia',
'Aruba',
'Australia',
'Austria',
'Azerbaijan',
'Bahamas',
'Bahrain',
'Bangladesh',
'Barbados',
'Belarus',
'Belgium',
'Belize',
'Benin',
'Bermuda',
'Bhutan',
'Bolivia',
'Bosnia And Herzegovina',
'Botswana',
'Bouvet Island',
'Brazil',
'British Indian Ocean Territory',
'Brunei Darussalam',
'Bulgaria',
'Burkina Faso',
'Burundi',
'Cambodia',
'Cameroon',
'Canada',
'Cape Verde',
'Cayman Islands',
'Central African Republic',
'Chad',
'Chile',
'China',
'Christmas Island',
'Cocos (Keeling) Islands',
'Colombia',
'Comoros',
'Congo',
'Congo, The Democratic Republic Of The',
'Cook Islands',
'Costa Rica',
"Cote D'Ivoire",
'Croatia',
'Cuba',
'Cyprus',
'Czech Republic',
'Denmark',
'Djibouti',
'Dominica',
'Dominican Republic',
'Ecuador',
'Egypt',
'El Salvador',
'Equatorial Guinea',
'Eritrea',
'Estonia',
'Ethiopia',
'Falkland Islands (Malvinas)',
'Faroe Islands',
'Fiji',
'Finland',
'France',
'French Guiana',
'French Polynesia',
'French Southern Territories',
'Gabon',
'Gambia',
'Georgia',
'Germany',
'Ghana',
'Gibraltar',
'Greece',
'Greenland',
'Grenada',
'Guadeloupe',
'Guam',
'Guatamala',
'Guinea',
'Guinea-Bissau',
'Guyana',
'Haiti',
'Heard Island And McDonald Islands',
'Honduras',
'Hong Kong',
'Hungary',
'Iceland',
'India',
'Indonesia',
'Iran, Islamic Republic Of',
'Iraq',
'Ireland',
'Israel',
'Italy',
'Jamaica',
'Japan',
'Jordan',
'Kazakhstan',
'Kenya',
'Kiribati',
"Korea, Democratic People's Republic Of",
'Korea, Republic Of',
'Kuwait',
'Kyrgyzstan',
"Lao People's Democratic Republic (LAOS)",
'Latvia',
'Lebonon',
'Lesotho',
'Liberia',
'Libyan Arab Jamahiriya',
'Liechtenstein',
'Lithuania',
'Luxembourg',
'Macao',
'Macedonia, The Former Yugoslav Republic Of',
'Madagascar',
'Malawi',
'Malaysia',
'Maldives',
'Mali',
'Malta',
'Marshall Islands',
'Martinique',
'Mauritania',
'Mauritius',
'Mayotte',
'Mexico',
'Micronesia, Federated States Of',
'Moldova, Republic Of',
'Monaco',
'Mongolia',
'Montenegro',
'Montserrat',
'Morocco',
'Mozambique',
'Myanmar',
'Namibia',
'Nauru',
'Nepal',
'Netherlands',
'Netherlands Antilles',
'New Caledonia',
'New Zealand',
'Nicaragua',
'Niger',
'Nigeria',
'Niue',
'Norfolk Island',
'Northern Mariana Islands',
'Norway',
'Oman',
'Pakistan',
'Palau',
'Palestinian Territory, Occupied',
'Panama',
'Papua New Guinea',
'Paraguay',
'Peru',
'Philippines',
'Pitcair',
'Poland',
'Portugal',
'Puerto Rico',
'Qatar',
'Reunion',
'Romania',
'Russian Federation',
'Rwanda',
'Saint Helena',
'Saint Kitts And Nevis',
'Saint Lucia',
'Saint Pierre And Miquelon',
'Saint Vincent And The Grenadines',
'Samoa',
'San Marina',
'Sao Tome And Principe',
'Saudi Arabia',
'Senegal',
'Serbia',
'Seychelles',
'Sierra Leone',
'Singapore',
'Slovakia',
'Slovenia',
'Solomon Islands',
'Somalia',
'South Africa',
'South Georgia And The South Sandwich Islands',
'Spain',
'Sri Lanka',
'Sudan',
'Suriname',
'Svalbard And Jan Mayen',
'Swaziland',
'Sweden',
'Switzerland',
'Syrian Arab Republic',
'Taiwan',
'Tajikistan',
'Tanzania, United Republic Of',
'Thailand',
'Timor-Leste',
'Togo',
'Tokelau',
'Tonga',
'Trinidad And Tobago',
'Tunisia',
'Turkey',
'Turkenistan',
'Turks And Caicos Islands',
'Tuvalu',
'Uganda',
'Ukraine',
'United Arab Emirates',
'United Kingdom',
'United States',
'United States Minor Outlying Islands',
'Uruguay',
'Uzbekistan',
'Vanuatu',
'Venezuela',
'Viet Nam',
'Virgin Islands, British',
'Virgin Islands, U.S.',
'Wallis And Futuna',
'Western Sahara',
'Yemen',
'Zambia',
'Zimbabwe'
],
regionValues: [
'AF',
'AL',
'DZ',
'AS',
'AD',
'AO',
'AQ',
'AG',
'AR',
'AM',
'AW',
'AU',
'AT',
'AZ',
'BS',
'BH',
'BD',
'BB',
'BY',
'BE',
'BZ',
'BJ',
'BM',
'BT',
'BO',
'BA',
'BW',
'BV',
'BR',
'IO',
'BN',
'BG',
'BF',
'BI',
'KH',
'CM',
'CA',
'CV',
'KY',
'CF',
'TD',
'CL',
'CN',
'CX',
'CC',
'CO',
'KM',
'CG',
'CD',
'CK',
'CR',
'CI',
'HR',
'CU',
'CY',
'CZ',
'DK',
'DJ',
'DM',
'DO',
'EC',
'EG',
'SV',
'GQ',
'ER',
'EE',
'ET',
'FK',
'FO',
'FJ',
'FI',
'FR',
'GF',
'PF',
'TF',
'GA',
'GM',
'GE',
'DE',
'GH',
'GI',
'GR',
'GL',
'GD',
'GP',
'GU',
'GT',
'GN',
'GW',
'GY',
'HT',
'HM',
'HN',
'HK',
'HU',
'IS',
'IN',
'ID',
'IR',
'IQ',
'IE',
'IL',
'IT',
'JM',
'JP',
'JO',
'KZ',
'KE',
'KI',
'KP',
'KR',
'KW',
'KG',
'LA',
'LV',
'LB',
'LS',
'LR',
'LY',
'LI',
'LT',
'LU',
'MO',
'MK',
'MG',
'MW',
'MY',
'MV',
'ML',
'MT',
'MH',
'MQ',
'MR',
'MU',
'YT',
'MX',
'FM',
'MD',
'MC',
'MN',
'ME',
'MS',
'MA',
'MZ',
'MM',
'NA',
'NR',
'NP',
'NL',
'AN',
'NC',
'NZ',
'NI',
'NE',
'NG',
'NU',
'NF',
'MP',
'NO',
'OM',
'PK',
'PW',
'PS',
'PA',
'PG',
'PY',
'PE',
'PH',
'PN',
'PL',
'PT',
'PR',
'QA',
'RE',
'RO',
'RU',
'RW',
'SH',
'KN',
'LC',
'PM',
'VC',
'WS',
'SM',
'ST',
'SA',
'SN',
'RS',
'SC',
'SL',
'SG',
'SK',
'SI',
'SB',
'SO',
'ZA',
'GS',
'ES',
'LK',
'SD',
'SR',
'SJ',
'SZ',
'SE',
'CH',
'SY',
'TW',
'TJ',
'TZ',
'TH',
'TL',
'TG',
'TK',
'TO',
'TT',
'TN',
'TR',
'TM',
'TC',
'TV',
'UG',
'UA',
'AE',
'GB',
'US',
'UM',
'UY',
'UZ',
'VU',
'VE',
'VN',
'VG',
'VI',
'WF',
'EH',
'YE',
'ZM',
'ZW'
]
}
},
@ -571,6 +88,12 @@ export default Vue.extend({
thumbnailPreference: function () {
return this.$store.getters.getThumbnailPreference
},
regionNames: function () {
return this.$store.getters.getRegionNames
},
regionValues: function () {
return this.$store.getters.getRegionValues
},
localeOptions: function () {
return Object.keys(this.$i18n.messages)
@ -590,12 +113,6 @@ export default Vue.extend({
return names
},
geoLocationOptions: function () {
return this.geoLocationArray.map((entry) => { return entry.code })
},
geoLocationNames: function () {
return this.geoLocationArray.map((entry) => { return entry.name })
},
backendNames: function () {
return [
@ -651,11 +168,6 @@ export default Vue.extend({
this.updateInvidiousInstanceBounce = debounce(this.updateInvidiousInstance, 500)
this.currentLocale = this.$i18n.locale
this.currentGeoLocation = this.$i18n.geoLocation
this.updateGeoLocationNames(this.currentLocale)
if (this.currentGeoLocation === undefined || this.currentGeoLocation === null) {
this.currentGeoLocation = 'us'
}
},
beforeDestroy: function () {
if (this.invidiousInstance === '') {
@ -681,26 +193,12 @@ export default Vue.extend({
this.$i18n.locale = locale
this.currentLocale = locale
localStorage.setItem('locale', locale)
this.updateGeoLocationNames(locale)
},
updateGeoLocation: function (location) {
this.$i18n.geoLocation = location
this.currentGeoLocation = location
localStorage.setItem('geoLocation', location)
},
updateGeoLocationNames: function (locale) {
let fileData
const fileLocation = this.isDev ? '.' : `${__dirname}`
if (FS.existsSync(`${fileLocation}/static/geolocations/${locale}`)) {
fileData = FS.readFileSync(`${fileLocation}/static/geolocations/${locale}/countries.json`)
} else {
fileData = FS.readFileSync(`${fileLocation}/static/geolocations/en-US/countries.json`)
const payload = {
isDev: this.isDev,
locale: locale
}
const countries = JSON.parse(fileData).map((entry) => { return { id: entry.id, name: entry.name, code: entry.alpha2 } })
countries.sort((a, b) => { return a.id - b.id })
this.geoLocationArray = countries
this.getRegionData(payload)
},
...mapActions([
@ -715,7 +213,8 @@ export default Vue.extend({
'updateListType',
'updateThumbnailPreference',
'updateInvidiousInstance',
'updateForceLocalBackendForLegacy'
'updateForceLocalBackendForLegacy',
'getRegionData'
])
}
})

View File

@ -55,14 +55,6 @@
:select-values="defaultPageValues"
@change="updateLandingPage"
/>
<ft-select
v-if="false"
:placeholder="$t('Settings.General Settings.Region for Trending')"
:value="region"
:select-names="regionNames"
:select-values="regionValues"
@change="updateRegion"
/>
<ft-select
:placeholder="$t('Settings.General Settings.Video View Type.Video View Type')"
:value="listType"
@ -87,11 +79,11 @@
/>
<ft-select
:placeholder="$t('Settings.General Settings.Region for Trending')"
:value="currentGeoLocation"
:select-names="geoLocationNames"
:select-values="geoLocationOptions"
:tooltip="$t('Tooltips.General Settings.Geo Location')"
@change="updateGeoLocation"
:value="region"
:select-names="regionNames"
:select-values="regionValues"
:tooltip="$t('Tooltips.General Settings.Region for Trending')"
@change="updateRegion"
/>
</div>
<ft-flex-box class="generalSettingsFlexBox">

View File

@ -1,5 +1,6 @@
import IsEqual from 'lodash.isequal'
import FtToastEvents from '../../components/ft-toast/ft-toast-events'
import fs from 'fs'
const state = {
isSideNavOpen: false,
sessionSearchHistory: [],
@ -7,6 +8,8 @@ const state = {
trendingCache: null,
showProgressBar: false,
progressBarPercentage: 0,
regionNames: [],
regionValues: [],
recentBlogPosts: [],
searchSettings: {
sortBy: 'relevance',
@ -89,6 +92,14 @@ const getters = {
return state.progressBarPercentage
},
getRegionNames () {
return state.regionNames
},
getRegionValues () {
return state.regionValues
},
getRecentBlogPosts () {
return state.recentBlogPosts
}
@ -109,6 +120,24 @@ const actions = {
return state.colorValues[randomInt]
},
getRegionData ({ commit }, payload) {
let fileData
const fileLocation = payload.isDev ? './static/geolocations/' : `${__dirname}/static/geolocations/`
if (fs.existsSync(`${fileLocation}${payload.locale}`)) {
fileData = fs.readFileSync(`${fileLocation}${payload.locale}/countries.json`)
} else {
fileData = fs.readFileSync(`${fileLocation}en-US/countries.json`)
}
const countries = JSON.parse(fileData).map((entry) => { return { id: entry.id, name: entry.name, code: entry.alpha2 } })
countries.sort((a, b) => { return a.id - b.id })
const regionNames = countries.map((entry) => { return entry.name })
const regionValues = countries.map((entry) => { return entry.code })
commit('setRegionNames', regionNames)
commit('setRegionValues', regionValues)
},
calculateColorLuminance (_, colorValue) {
const cutHex = colorValue.substring(1, 7)
const colorValueR = parseInt(cutHex.substring(0, 2), 16)
@ -406,6 +435,14 @@ const mutations = {
state.searchSettings.duration = value
},
setRegionNames (state, value) {
state.regionNames = value
},
setRegionValues (state, value) {
state.regionValues = value
},
setRecentBlogPosts (state, value) {
state.recentBlogPosts = value
}

View File

@ -34,6 +34,9 @@ export default Vue.extend({
invidiousInstance: function () {
return this.$store.getters.getInvidiousInstance
},
region: function () {
return this.$store.getters.getRegion.toUpperCase()
},
trendingCache () {
return this.$store.getters.getTrendingCache
}
@ -65,8 +68,7 @@ export default Vue.extend({
this.isLoading = true
console.log('getting local trending')
console.log(localStorage.geoLocation)
ytrend.scrape_trending_page(localStorage.geoLocation.toUpperCase()).then((result) => {
ytrend.scrape_trending_page(this.region).then((result) => {
const returnData = result.filter((item) => {
return item.type === 'video' || item.type === 'channel' || item.type === 'playlist'
})

View File

@ -537,7 +537,7 @@ Tooltips:
Fallback to Non-Preferred Backend on Failure: When your preferred API has a problem, FreeTube will automatically attempt to use your non-preferred API as a fallback method when enabled
Thumbnail Preference: All thumbnails throughout FreeTube will be replaced with a frame of the video instead of the default thumbnail
Invidious Instance: The Invidious instance that FreeTube will connect to for API calls. Clear the current instance to see a list of public instances to choose from
Geo Location: The region of trends allows you to pick which country's trending videos you want to have displayed. Not all countries displayed are actually supported by YouTube.
Region for Trending: The region of trends allows you to pick which country's trending videos you want to have displayed. Not all countries displayed are actually supported by YouTube
Player Settings:
Force Local Backend for Legacy Formats: Only works when the Invidious API is your default. When enabled, the local API will run and use the legacy formats returned by that instead of the ones returned by Invidious. Helps when the videos returned by Invidious don't play due to country restrictions
Proxy Videos Through Invidious: Will connect to Invidious to serve videos instead of making a direct connection to YouTube. Overrides API preference