3

I want to pass a cookie file to youtube-dl coming from a browser extension. The extension sends cookie in raw format i.e. like VISITOR_INFO1_LIVE=_SebbjYciU0; YSC=tSqadPjjfd8; PREF=f4=4000000 But youtube-dl takes netscape jar format cookies (as far as I know). If I put the raw text cookie in a file and pass it to --cookies=file.txt argument youtube-dl raises an exception.

I cannot manage to to convert my raw cookies to jar cookies and save to a file in the disk. I have searched for a solution but did not find any acceptable solution.

phrogg
  • 888
  • 1
  • 13
  • 28
Chitholian
  • 432
  • 1
  • 10
  • 19
  • Possible duplicate of [Converting from standard cookie format to LibCurl cookie jar format](https://stackoverflow.com/questions/19560722/converting-from-standard-cookie-format-to-libcurl-cookie-jar-format) – phrogg Sep 11 '18 at 15:50

1 Answers1

0

I had very similar problem to convert "curl" cookies into wget one ... Netscape CookieJar format is straight forward see *

so I took few minutes to write a quick perl script and generate a cookiejar

here is the code (downloadable here)

#!/usr/bin/perl

# usage
# perl cookiejar.pl {{url}} '{{cookie-string}}'


use YAML::Syck qw(Dump);
my $expires = $^T + 86400;
my $path = '/';

my $url = shift;
my $cookies = shift;

printf "--- # %s at %u\n",__FILE__,$^T;
my $domain;
my $p = index($url,'://')+3;
my $l = index($url,'/',$p);
$domain = substr($url,$p,$l-$p);
my $dots = () = $domain =~ /\./g;
printf "dots: %s\n",$dots;
if ($dots > 1) {
   $domain = substr($domain,index($domain,'.'));
} else {
   $domain = '.'.$domain;
}
printf "domain: %s\n",$domain;
printf "url: %s\n",$url;

local *F; open F,'>','cookiejar.txt' or warn $!;
print F <<EOT;
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This is a generated file!  Do not edit.
# domain: $domain
# url: $url

EOT
my @cookies = split'; ',$cookies;
printf "--- %s...\n",Dump(\@cookies);
foreach my $cookie (@cookies) {
   my ($key,$value) = split('=',$cookie);
   if (! $seen{$key}++) {
      # domain access path sec expire cookie value
      printf F "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",$domain,'TRUE',$path,'FALSE',$expires,$key,$value;
   }
 }
close F;
print "info: cookiejar.txt created\n";
printf "cmd: wget --load-cookie-file cookiejar.txt --referer=%s -p %s\n",$domain,$url;
printf "cmd: youtube-dl --cookie cookiejar.txt -referer %s %s\n",$domain,$url;

exit $?;

1; # $Source: /my/perl/scripts/cookiejar.pl $

you run it as followed :

perl cookiejar.pl https://example.com/ 'cookie1=value1; cookie2=value2'

note:

You might need to install YAML::Syck with

cpan install YAML::Syck

or just comment out the Dump() call and the use YAML::Syck line.