<commit_before># Part of get-flash-videos. See get_flash_videos for copyright.
package FlashVideo::Site::Theonion; # horrible casing :(

use strict;
use FlashVideo::Utils;

sub find_video {
  my ($self, $browser) = @_;

  # Get the video ID
  my $video_id;

  if ($browser->content =~ /<meta name="nid" content="(\d+)"/) {
    $video_id = $1;
  }
  elsif ($browser->content =~ /var videoid = ["'](\d+)["'];/) {
    $video_id = $1;
  }
  
  die "Couldn't find Onion video ID" unless $video_id;

  # Get XML with FLV details
  # TODO - support "bookend" (online exclusive videos).
  $browser->get("http://www.theonion.com/content/xml/$video_id/video");

  die "Couldn't download Onion XML: " . $browser->response->status_line
    if !$browser->success;

  # Generic can handle this XML so just pass it over to that.
  my ($url, @filenames) = FlashVideo::Generic->find_video($browser);

  # We can probably make a better guess for the filename than generic can
  # though.
  my $has_xml_simple = eval { require XML::Simple };
  
  # Don't die if XML::Simple isn't installed, as strictly speaking it's not
  # needed for this video -- it just lets us have a better title.
  if ($has_xml_simple) {
    my $video_rss = eval {
      XML::Simple::XMLin($browser->content);
    };

    die "Couldn't parse The Onion XML: $@" if $@;

    if (my $title_from_rss = $video_rss->{channel}->{item}->[0]->{title}) {
      $title_from_rss = title_to_filename($title_from_rss); 

      # Likely to be a better filename so put it first for people who are
      # using --yes
      unshift @filenames, $title_from_rss;
    }
  }

  return $url, @filenames;
}

1;<commit_msg>Fix Onion support (issue 89).<commit_after># Part of get-flash-videos. See get_flash_videos for copyright.
package FlashVideo::Site::Theonion; # horrible casing :(

use strict;
use FlashVideo::Utils;

sub find_video {
  my ($self, $browser) = @_;

  if ($browser->response->is_redirect) {
    $browser->get( $browser->response->header('Location') );

    if (!$browser->success) {
      die "Couldn't follow Onion redirect: " .
        $browser->response->status_line;
    }
  }

  my $title;
  if ($browser->content =~ /var video_title = "([^"]+)"/) {
    $title = $1;
  }
  else {
    $title = extract_info($browser)->{meta_title};
  }

  my $filename = title_to_filename($title);

  # They now pass the URL as a param, so the generic code can extract it.
  my $url = (FlashVideo::Generic->find_video($browser, $browser->uri))[0];

  return $url, $filename;
}

1;
{'fraction_non_alphanumeric': 0.15442635966653434, 'fraction_numerical': 0.003572846367606193, 'mean_word_length': 3.4601769911504423, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 3}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6929981', 'n_tokens_mistral': 883, 'n_tokens_neox': 843, 'n_words': 242}