<commit_before>module Suby
  class Downloader::TVSubtitles < Downloader
    SITE = 'www.tvsubtitles.net'
    FORMAT = :zip
    SEARCH_URL = '/search.php'

    # cache
    SHOW_URLS = {}
    SHOW_PAGES = {}

    def show_url
      SHOW_URLS[show] ||= begin
        post = Net::HTTP::Post.new(SEARCH_URL)
        post.form_data = { 'q' => show }
        results = Nokogiri http.request(post).body
        a = results.css('ul li div a').find { |a|
           # "Show (2009-2011)" => "Show"
          a.text.sub(/ \(\d{4}-\d{4}\)$/, '').casecmp(show) == 0
        }
        raise NotFoundError, "show not found" unless a
        url = a[:href]

        raise 'could not find the show' unless /^\/tvshow-(\d+)\.html$/ =~ url
        "/tvshow-#{$1}.html"
      end
    end

    def season_url
      show_url.sub(/\.html$/, "-#{season}.html")
    end

    def episode_url
      @episode_url ||= begin
        SHOW_PAGES[show] ||= Nokogiri get season_url

        season_text = /^Season #{season}$/
        has_season = SHOW_PAGES[show].css('div.left_articles p.description b').find { |b|
          b.text =~ season_text
        }
        raise NotFoundError, "season not found" unless has_season

        url = nil
        row = SHOW_PAGES[show].css('div.left_articles table tr').find { |tr|
          tr.children.find { |td| td.name == 'td' && td.text =~ /\A#{season}x0?#{episode}\z/ }
        }
        raise NotFoundError, "episode not found" unless row
        row.children.find { |td|
          td.children.find { |a|
            a.name == 'a' && a[:href].start_with?('episode') && url = a[:href]
          }
        }
        raise "invalid episode url: #{episode_url}" unless url =~ /^episode-(\d+)\.html$/
        "/episode-#{$1}-#{lang}.html"
      end
    end

    def subtitles_url
      @subtitles_url ||= begin
        subtitles = Nokogiri get episode_url

        # TODO: choose 720p or most downloaded instead of first found
        a = subtitles.css('div.left_articles a').find { |a| a.name == 'a' && a[:href].start_with?('/subtitle') }
        raise NotFoundError, "no subtitle available" unless a
        url = a[:href]
        raise 'invalid subtitle url' unless url =~ /^\/subtitle-(\d+)\.html/
        url
      end
    end

    def download_url
      @download_url ||= URI.escape '/' + get_redirection(subtitles_url.sub('subtitle', 'download'))
    end
  end
end<commit_msg>don't omit parentheses<commit_after>module Suby
  class Downloader::TVSubtitles < Downloader
    SITE = 'www.tvsubtitles.net'
    FORMAT = :zip
    SEARCH_URL = '/search.php'

    # cache
    SHOW_URLS = {}
    SHOW_PAGES = {}

    def show_url
      SHOW_URLS[show] ||= begin
        post = Net::HTTP::Post.new(SEARCH_URL)
        post.form_data = { 'q' => show }
        results = Nokogiri http.request(post).body
        a = results.css('ul li div a').find { |a|
           # "Show (2009-2011)" => "Show"
          a.text.sub(/ \(\d{4}-\d{4}\)$/, '').casecmp(show) == 0
        }
        raise NotFoundError, "show not found" unless a
        url = a[:href]

        raise 'could not find the show' unless /^\/tvshow-(\d+)\.html$/ =~ url
        "/tvshow-#{$1}.html"
      end
    end

    def season_url
      show_url.sub(/\.html$/, "-#{season}.html")
    end

    def episode_url
      @episode_url ||= begin
        SHOW_PAGES[show] ||= Nokogiri(get(season_url))

        season_text = /^Season #{season}$/
        has_season = SHOW_PAGES[show].css('div.left_articles p.description b').find { |b|
          b.text =~ season_text
        }
        raise NotFoundError, "season not found" unless has_season

        url = nil
        row = SHOW_PAGES[show].css('div.left_articles table tr').find { |tr|
          tr.children.find { |td| td.name == 'td' && td.text =~ /\A#{season}x0?#{episode}\z/ }
        }
        raise NotFoundError, "episode not found" unless row
        row.children.find { |td|
          td.children.find { |a|
            a.name == 'a' && a[:href].start_with?('episode') && url = a[:href]
          }
        }
        raise "invalid episode url: #{episode_url}" unless url =~ /^episode-(\d+)\.html$/
        "/episode-#{$1}-#{lang}.html"
      end
    end

    def subtitles_url
      @subtitles_url ||= begin
        subtitles = Nokogiri(get(episode_url))

        # TODO: choose 720p or most downloaded instead of first found
        a = subtitles.css('div.left_articles a').find { |a| a.name == 'a' && a[:href].start_with?('/subtitle') }
        raise NotFoundError, "no subtitle available" unless a
        url = a[:href]
        raise 'invalid subtitle url' unless url =~ /^\/subtitle-(\d+)\.html/
        url
      end
    end

    def download_url
      @download_url ||= URI.escape '/' + get_redirection(subtitles_url.sub('subtitle', 'download'))
    end
  end
end
{'fraction_non_alphanumeric': 0.15800459194322689, 'fraction_numerical': 0.00709663953245669, 'mean_word_length': 2.1340745585349903, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3726988', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1626, 'n_words': 363}