import { spawn } from 'child_process';

import { getArg } from './args.js';
import { error, log } from './log.js';

const ctx = 'getUser.js';

export const getUser = (user, path) => {
  const url = `https://twitter.com/${user}/media`;
  let args;
  try {
    args = getArg('args');
  } catch (err) {
    log(ctx, 'No args being provided to gallery-dl');
  }

  log(ctx, `python3 ~/.local/bin/gallery-dl -c ./config.json${!!args ? ' ' + args + ' ' : ' '}-d ${path} ${url}`);
  const proc = spawn(`python3 ~/.local/bin/gallery-dl -c ./config.json${!!args ? ' ' + args + ' ' : ' '}-d ${path} ${url}`, { shell: true });

  proc.stdout.on('data', data => {
    //log(ctx, data);
  });
  proc.stderr.on('data', data => {
    error(ctx, data);
  });
  proc.on('error', err => {
    error(ctx, err);
  });
  proc.on('close', code => {
    log(ctx, `child process exited with code ${code}`);
  });

  return proc;
};