the-convocation / twitter-scraper

A port of n0madic/twitter-scraper to Node.js.
https://the-convocation.github.io/twitter-scraper/
MIT License
190 stars 42 forks source link

setCookies doesn't work with new version tough-cookie #110

Open cogor opened 3 weeks ago

cogor commented 3 weeks ago

I saving cookies via getCookies() function When I try to restore then, I getting an error "Error: First argument to setCookie must be a Cookie object or string" I downgraded version of tough-cookie to 4.1.2(was v5.0.0) and now it works well

karashiiro commented 3 weeks ago

Can you share how you're storing and loading cookies? I'm seeing this comment under the branch for that error in v5 of the library:

    } else if (!(cookie instanceof Cookie)) {
      // If you're seeing this error, and are passing in a Cookie object,
      // it *might* be a Cookie object from another loaded version of tough-cookie.
      const err = new Error(
        'First argument to setCookie must be a Cookie object or string',
      )

      return options?.ignoreError
        ? promiseCallback.resolve(undefined)
        : promiseCallback.reject(err)
    }
cogor commented 3 weeks ago

I storing cookies in json file

// Saving
const cookies = this.scraper.getCookies();
const serializedCookies = JSON.stringify(cookies, null, 2);
writeFileSync(this.COOKIE_FILE, serializedCookies, 'utf8');

// Restoring
const fileContent = readFileSync(this.COOKIE_FILE, 'utf-8');
const cookies = JSON.parse(fileContent).reduce((acc: Cookie[], c) => {
  const cookie = Cookie.fromJSON(c);
  return cookie ? [...acc, cookie] : acc;
}, []);
this.scraper.setCookies(cookies);