rewrite mail.ru config, recreate UniqBy in native JS

This commit is contained in:
theofficialomega
2025-07-12 13:44:19 +02:00
parent 12849d2350
commit 2f5d209f5f
4 changed files with 108 additions and 7 deletions

View File

@@ -0,0 +1,65 @@
{
"status":"OK",
"schedule":[
{
"channel":{
"name":"21TV",
"pic_url":"https://resizer.mail.ru/p/1234c5ac-c19c-5cf2-9c6a-fc0efca920ac/AAACm2w9aDpGPSWXzsH7PBq2X3I6pbxqmrj-yeuVppAKyyBHXE_dH_7pHQ2rOavyKiC4iHIWTab9SeKo7pKgr71lqVA.png",
"pic_url_128":"https://resizer.mail.ru/p/1234c5ac-c19c-5cf2-9c6a-fc0efca920ac/AAACwjJ45j9sTP8fcjPJnJ4xk5e_ILr5iXwjLMhWhzlVnIJkrtT42vEp9walcgpXRKDq9KFoliEPR0xI-LEh96C_izY.png",
"pic_url_64":"https://resizer.mail.ru/p/1234c5ac-c19c-5cf2-9c6a-fc0efca920ac/dpr:200/AAACm2w9aDpGPSWXzsH7PBq2X3I6pbxqmrj-yeuVppAKyyBHXE_dH_7pHQ2rOavyKiC4iHIWTab9SeKo7pKgr71lqVA.png"
},
"event":{
"current":[
{
"channel_id":"2785",
"name":"Պրոֆեսիոնալները",
"category_id":8,
"episode_title":"",
"url":"/moskva/channel/2785/173593246/",
"id":"173593246",
"start":"02:40",
"episode_num":0
},
{
"channel_id":"2785",
"name":"Նոնստոպ․ Տեսահոլովակներ",
"category_id":23,
"episode_title":"",
"url":"/moskva/channel/2785/173593142/",
"id":"173593142",
"start":"03:25",
"episode_num":0
}
],
"past":[
{
"channel_id":"2785",
"name":"Նոնստոպ․ Տեսահոլովակներ",
"category_id":23,
"episode_title":"",
"url":"/moskva/channel/2785/173593328/",
"id":"173593328",
"start":"23:35",
"episode_num":0
},
{
"channel_id":"2785",
"video":{
"currency":"RUB",
"price_min":"249.00",
"price_txt":"249 р."
},
"name":"Վերջին թագավորությունը",
"category_id":2,
"episode_title":"",
"url":"/moskva/channel/2785/173593318/",
"id":"173593318",
"start":"01:40",
"our_event_id":"890224",
"episode_num":0
}
]
}
}
]
}

View File

@@ -0,0 +1,23 @@
{
"status":"OK",
"current_ts":1637788593,
"form":{
"values":[
]
},
"current_offset":10800,
"schedule":[
{
"channel":null,
"event":{
"current":[
],
"past":[
]
}
}
]
}

View File

@@ -1,6 +1,21 @@
const { DateTime } = require('luxon')
const axios = require('axios')
// Remove the big lodash dependency by implementing a simple uniqBy function
// Complexity = O(n)
const uniqBy = (arr, predicate) => {
const cb = typeof predicate === 'function' ? predicate : (o) => o[predicate]
return [...arr.reduce((map, item) => {
const key = (item === null || item === undefined) ?
item : cb(item)
if (!map.has(key)) map.set(key, item)
return map
}, new Map()).values()]
}
module.exports = {
site: 'tv.mail.ru',
days: 2,
@@ -35,8 +50,6 @@ module.exports = {
return programs
},
async channels() {
const _ = require('lodash')
const regions = [5506, 1096, 1125, 285]
let channels = []
@@ -64,7 +77,7 @@ module.exports = {
}
}
return _.uniqBy(channels, 'site_id')
return uniqBy(channels, 'site_id')
}
}

View File

@@ -2,6 +2,8 @@ const { parser, url } = require('./tv.mail.ru.config.js')
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
const customParseFormat = require('dayjs/plugin/customParseFormat')
const fs = require('fs')
const path = require('path')
dayjs.extend(customParseFormat)
dayjs.extend(utc)
@@ -10,8 +12,7 @@ const channel = {
site_id: '2785',
xmltv_id: '21TV.am'
}
const content =
'{"status":"OK","schedule":[{"channel":{"name":"21TV","pic_url":"https://resizer.mail.ru/p/1234c5ac-c19c-5cf2-9c6a-fc0efca920ac/AAACm2w9aDpGPSWXzsH7PBq2X3I6pbxqmrj-yeuVppAKyyBHXE_dH_7pHQ2rOavyKiC4iHIWTab9SeKo7pKgr71lqVA.png","pic_url_128":"https://resizer.mail.ru/p/1234c5ac-c19c-5cf2-9c6a-fc0efca920ac/AAACwjJ45j9sTP8fcjPJnJ4xk5e_ILr5iXwjLMhWhzlVnIJkrtT42vEp9walcgpXRKDq9KFoliEPR0xI-LEh96C_izY.png","pic_url_64":"https://resizer.mail.ru/p/1234c5ac-c19c-5cf2-9c6a-fc0efca920ac/dpr:200/AAACm2w9aDpGPSWXzsH7PBq2X3I6pbxqmrj-yeuVppAKyyBHXE_dH_7pHQ2rOavyKiC4iHIWTab9SeKo7pKgr71lqVA.png"},"event":{"current":[{"channel_id":"2785","name":"Պրոֆեսիոնալները","category_id":8,"episode_title":"","url":"/moskva/channel/2785/173593246/","id":"173593246","start":"02:40","episode_num":0},{"channel_id":"2785","name":"Նոնստոպ․ Տեսահոլովակներ","category_id":23,"episode_title":"","url":"/moskva/channel/2785/173593142/","id":"173593142","start":"03:25","episode_num":0}],"past":[{"channel_id":"2785","name":"Նոնստոպ․ Տեսահոլովակներ","category_id":23,"episode_title":"","url":"/moskva/channel/2785/173593328/","id":"173593328","start":"23:35","episode_num":0},{"channel_id":"2785","video":{"currency":"RUB","price_min":"249.00","price_txt":"249 р."},"name":"Վերջին թագավորությունը","category_id":2,"episode_title":"","url":"/moskva/channel/2785/173593318/","id":"173593318","start":"01:40","our_event_id":"890224","episode_num":0}]}}]}'
const content = fs.readFileSync(path.join(__dirname, '__data__', 'content.json'), 'utf8')
it('can generate valid url', () => {
expect(url({ channel, date })).toBe(
@@ -70,8 +71,7 @@ it('can handle empty guide', () => {
const result = parser({
date,
channel,
content:
'{"status":"OK","current_ts":1637788593,"form":{"values":[]},"current_offset":10800,"schedule":[{"channel":null,"event":{"current":[],"past":[]}}]}'
content: fs.readFileSync(path.join(__dirname, '__data__', 'no_content.json'), 'utf8')
})
expect(result).toMatchObject([])
})