iptv-org / epg

Utilities for downloading the EPG (Electronic Program Guide) for thousands of TV channels from hundreds of sources.
https://iptv-org.github.io/
The Unlicense
1.66k stars 156 forks source link

Wrong 'output' path #2342

Open ewwink opened 3 months ago

ewwink commented 3 months ago

Describe your issue

epg directory

/home/epg

command

cd /home/epg
npm run grab -- --site=indihometv.com -o /download/xml/id.xml

the file should be downloaded to /download/xml/id.xml but it saved to /home/epg/download/xml/id.xml

demod-au commented 3 months ago

thanks.. you helped me find where they were going. I have shortened my path to accommodate the problem.

AndMetal commented 3 months ago

I believe this is an issue with how Storage is being used in guide.ts.

In the constructor, Storage is called with no arguments: https://github.com/iptv-org/epg/blob/a1ca6b33354acbe10a97bc404c916c05517cedb5/scripts/core/guide.ts#L21-L28

Looking at the code for @freearhey/core (src/storage.ts), if a root directory path is passed it is used, otherwise it uses the current working directory:

  constructor(rootDir?: string) {
    this._rootDir = rootDir ? path.resolve(rootDir) : process.cwd()
  }

When the save call is made: https://github.com/iptv-org/epg/blob/a1ca6b33354acbe10a97bc404c916c05517cedb5/scripts/core/guide.ts#L45-L55 it gets mapped to _write in Storage which adds the path to the root directory:

  async _write(filepath: string, data: any = ''): Promise<void> {
    const absFilepath = path.join(this._rootDir, filepath)
    const dir = path.dirname(absFilepath)

    await this.createDir(dir)
    await fs.writeFile(absFilepath, data, { encoding: 'utf8', flag: 'w' })
  }

which is the working directory the script was called from.

I believe the fix would be to utilize Path in @freearhey/core to get the dirname of the output option and pass that to the Storage constructor. From what I can tell if it is just a filename/pattern or references a relative path it should default to the current working directory as a base, so in theory it should be mostly backwards compatible.

Edit: as a workaround you might be able to use ../../download/xml/id.xml for the output option.

Edit 2: I confirmed ../../download/xml/id.xml works to put the file into that other directory. I still think absolute paths should be allowable and handled like I mentioned above.