Add image object
This commit is contained in:
		
							parent
							
								
									22b2e9c19e
								
							
						
					
					
						commit
						ec3372ecd3
					
				
							
								
								
									
										93
									
								
								src/utils.js
								
								
								
								
							
							
						
						
									
										93
									
								
								src/utils.js
								
								
								
								
							| 
						 | 
					@ -182,7 +182,7 @@ utils.convertToXMLTV = function ({ channels, programs }) {
 | 
				
			||||||
    const credits = createCredits({'director':program.director,'actor':program.actor, 'writer':program.writer,'adapter':program.adapter,'producer':program.producer, 'composer':program.composer, 'editor':program.editor, 'presenter':program.presenter, 'commentator':program.commentator, 'guest':program.guest})
 | 
					    const credits = createCredits({'director':program.director,'actor':program.actor, 'writer':program.writer,'adapter':program.adapter,'producer':program.producer, 'composer':program.composer, 'editor':program.editor, 'presenter':program.presenter, 'commentator':program.commentator, 'guest':program.guest})
 | 
				
			||||||
    const icon = utils.escapeString(program.icon)
 | 
					    const icon = utils.escapeString(program.icon)
 | 
				
			||||||
    const sub_title = program.sub_title || ''
 | 
					    const sub_title = program.sub_title || ''
 | 
				
			||||||
    const url = program.url || ''
 | 
					    const url = program.url? createURL(program.url, channel) : ''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (start && stop && title) {
 | 
					    if (start && stop && title) {
 | 
				
			||||||
      output += `<programme start="${start}" stop="${stop}" channel="${channel}"><title lang="${lang}">${title}</title>`
 | 
					      output += `<programme start="${start}" stop="${stop}" channel="${channel}"><title lang="${lang}">${title}</title>`
 | 
				
			||||||
| 
						 | 
					@ -204,14 +204,7 @@ utils.convertToXMLTV = function ({ channels, programs }) {
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (url) {
 | 
					      if (url) {
 | 
				
			||||||
        if(url.includes('http')){
 | 
					        output += url
 | 
				
			||||||
          output += `<url>${url}</url>`
 | 
					 | 
				
			||||||
        } else{
 | 
					 | 
				
			||||||
          let chan =  channels.find((c) => c.xmltv_id.localeCompare(channel) === 0)
 | 
					 | 
				
			||||||
          if (chan && chan.site){
 | 
					 | 
				
			||||||
            output += `<url>https://${chan.site}${url}</url>`
 | 
					 | 
				
			||||||
          }          
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (xmltv_ns) {
 | 
					      if (xmltv_ns) {
 | 
				
			||||||
| 
						 | 
					@ -256,10 +249,76 @@ utils.convertToXMLTV = function ({ channels, programs }) {
 | 
				
			||||||
    return `S${s}E${e}`
 | 
					    return `S${s}E${e}`
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  function createURL(urlObj, channel = '') {
 | 
				
			||||||
 | 
					    const urls = Array.isArray(urlObj) ? urlObj : [urlObj]
 | 
				
			||||||
 | 
					    let output = ''
 | 
				
			||||||
 | 
					    for (let url of urls){
 | 
				
			||||||
 | 
					      if (typeof url === 'string' || url instanceof String){
 | 
				
			||||||
 | 
					        url = {value: url}
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      let attr = url.system ? ` system="${url.system}"` : ''
 | 
				
			||||||
 | 
					      if(url.value.includes('http')){
 | 
				
			||||||
 | 
					        output += `<url${attr}>${url.value}</url>`
 | 
				
			||||||
 | 
					      } else if(channel){
 | 
				
			||||||
 | 
					        let chan =  channels.find((c) => c.xmltv_id.localeCompare(channel) === 0)
 | 
				
			||||||
 | 
					        if (chan && chan.site){
 | 
				
			||||||
 | 
					          output += `<url${attr}>https://${chan.site}${url.value}</url>`
 | 
				
			||||||
 | 
					        }          
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return output
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  function createImage(imgObj, channel='') {
 | 
				
			||||||
 | 
					    const imgs = Array.isArray(imgObj) ? imgObj : [imgObj]
 | 
				
			||||||
 | 
					    let output = ''
 | 
				
			||||||
 | 
					    for (let img of imgs){
 | 
				
			||||||
 | 
					      if (typeof img === 'string' || img instanceof String){
 | 
				
			||||||
 | 
					        img = {value: img}
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      const imageTypes = ['poster', 'backdrop', 'still', 'person', 'character'];
 | 
				
			||||||
 | 
					      const imageSizes = ['1', '2', '3'];
 | 
				
			||||||
 | 
					      const imageOrients = ['P', 'L'];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      let attr = ''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if(img.type && imageTypes.some(el => img.type.includes(el))){
 | 
				
			||||||
 | 
					        attr += ` type="${img.type}"`
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if(img.size && imageSizes.some(el => img.size.includes(el))){
 | 
				
			||||||
 | 
					        attr += ` size="${img.size}"`
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if(img.orient && imageOrients.some(el => img.orient.includes(el))){
 | 
				
			||||||
 | 
					        attr += ` orient="${img.orient}"`
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if(img.system){
 | 
				
			||||||
 | 
					        attr += ` system="${img.system}"`
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if(img.value.includes('http')){
 | 
				
			||||||
 | 
					        output += `<image${attr}>${img.value}</image>`
 | 
				
			||||||
 | 
					      } else if(channel){
 | 
				
			||||||
 | 
					        let chan =  channels.find((c) => c.xmltv_id.localeCompare(channel) === 0)
 | 
				
			||||||
 | 
					        if (chan && chan.site){
 | 
				
			||||||
 | 
					          output += `<image${attr}>https://${chan.site}${img.value}</image>`
 | 
				
			||||||
 | 
					        }          
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return output
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  function createCredits(obj) {
 | 
					  function createCredits(obj) {
 | 
				
			||||||
  let cast = Object.entries(obj)
 | 
					  let cast = Object.entries(obj)
 | 
				
			||||||
                .filter(([name, value]) => value)
 | 
					                .filter(([name, value]) => value)
 | 
				
			||||||
                .map(([name, value]) => ({name, value}));
 | 
					                .map(([name, value]) => ({name, value}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  let output = ''
 | 
					  let output = ''
 | 
				
			||||||
  for(let type of cast){
 | 
					  for(let type of cast){
 | 
				
			||||||
    const r = Array.isArray(type.value) ? type.value: [type.value]
 | 
					    const r = Array.isArray(type.value) ? type.value: [type.value]
 | 
				
			||||||
| 
						 | 
					@ -278,18 +337,12 @@ utils.convertToXMLTV = function ({ channels, programs }) {
 | 
				
			||||||
      output += `<${type.name}${attr}>${person.value}`
 | 
					      output += `<${type.name}${attr}>${person.value}`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if(person.url){
 | 
					      if(person.url){
 | 
				
			||||||
        const r = Array.isArray(person.url) ? person.url: [person.url]
 | 
					        output += createURL(person.url)
 | 
				
			||||||
        for (let x of r){
 | 
					      }
 | 
				
			||||||
          let attr = x.system ? ` system="${x.system}"` : ''
 | 
					      if (person.image){
 | 
				
			||||||
          output += `<url${attr}>${x.value}</url>`
 | 
					        output += createImage(person.image)
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      /*if (person.image){
 | 
					 | 
				
			||||||
        for (let x of person.image){
 | 
					 | 
				
			||||||
      
 | 
					      
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      */
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      output += `</${type.name}>`
 | 
					      output += `</${type.name}>`
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -171,6 +171,51 @@ it('can convert object to xmltv string with multiple categories', () => {
 | 
				
			||||||
  )
 | 
					  )
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					it('can convert object to xmltv string with multiple urls', () => {
 | 
				
			||||||
 | 
					  const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
 | 
				
			||||||
 | 
					  const { channels } = utils.parseChannels(file)
 | 
				
			||||||
 | 
					  const programs = [
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      title: 'Program 1',
 | 
				
			||||||
 | 
					      description: 'Description for Program 1',
 | 
				
			||||||
 | 
					      start: 1616133600,
 | 
				
			||||||
 | 
					      stop: 1616135400,
 | 
				
			||||||
 | 
					      category: ['Test1', 'Test2'],
 | 
				
			||||||
 | 
					      url:['https://example.com/noattr.html',{value:'https://example.com/attr.html', system:'TestSystem'}],
 | 
				
			||||||
 | 
					      icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
 | 
				
			||||||
 | 
					      channel: '1TV.com',
 | 
				
			||||||
 | 
					      lang: 'it'
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  ]
 | 
				
			||||||
 | 
					  const output = utils.convertToXMLTV({ channels, programs })
 | 
				
			||||||
 | 
					  expect(output).toBe(
 | 
				
			||||||
 | 
					    '<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><url>https://example.com/noattr.html</url><url system="TestSystem">https://example.com/attr.html</url><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/></programme>\r\n</tv>'
 | 
				
			||||||
 | 
					  )
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					it('can convert object to xmltv string with multiple images', () => {
 | 
				
			||||||
 | 
					  const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
 | 
				
			||||||
 | 
					  const { channels } = utils.parseChannels(file)
 | 
				
			||||||
 | 
					  const programs = [
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      title: 'Program 1',
 | 
				
			||||||
 | 
					      description: 'Description for Program 1',
 | 
				
			||||||
 | 
					      start: 1616133600,
 | 
				
			||||||
 | 
					      stop: 1616135400,
 | 
				
			||||||
 | 
					      category: ['Test1', 'Test2'],
 | 
				
			||||||
 | 
					      url:['https://example.com/noattr.html',{value:'https://example.com/attr.html', system:'TestSystem'}],
 | 
				
			||||||
 | 
					      actor:{value:'Actor 1', image:['https://example.com/image1.jpg',{value:'https://example.com/image2.jpg',type:'person',size:'2',system:'TestSystem',orient:'P'}]},
 | 
				
			||||||
 | 
					      icon: 'https://example.com/images/Program1.png?x=шеллы&sid=777',
 | 
				
			||||||
 | 
					      channel: '1TV.com',
 | 
				
			||||||
 | 
					      lang: 'it'
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  ]
 | 
				
			||||||
 | 
					  const output = utils.convertToXMLTV({ channels, programs })
 | 
				
			||||||
 | 
					  expect(output).toBe(
 | 
				
			||||||
 | 
					    '<?xml version="1.0" encoding="UTF-8" ?><tv date="20220505">\r\n<channel id="1TV.com"><display-name>1 TV</display-name><icon src="https://example.com/logos/1TV.png"/><url>https://example.com</url></channel>\r\n<channel id="2TV.com"><display-name>2 TV</display-name><url>https://example.com</url></channel>\r\n<programme start="20210319060000 +0000" stop="20210319063000 +0000" channel="1TV.com"><title lang="it">Program 1</title><desc lang="it">Description for Program 1</desc><category lang="it">Test1</category><category lang="it">Test2</category><url>https://example.com/noattr.html</url><url system="TestSystem">https://example.com/attr.html</url><icon src="https://example.com/images/Program1.png?x=шеллы&sid=777"/><credits><actor>Actor 1<image>https://example.com/image1.jpg</image><image type="person" size="2" orient="P" system="TestSystem">https://example.com/image2.jpg</image></actor></credits></programme>\r\n</tv>'
 | 
				
			||||||
 | 
					  )
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
it('can convert object to xmltv string with multiple credits member', () => {
 | 
					it('can convert object to xmltv string with multiple credits member', () => {
 | 
				
			||||||
  const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
 | 
					  const file = fs.readFileSync('./tests/input/example.com.channels.xml', { encoding: 'utf-8' })
 | 
				
			||||||
  const { channels } = utils.parseChannels(file)
 | 
					  const { channels } = utils.parseChannels(file)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue