<commit_before>require 'restclient'
require 'nokogiri'
require 'date'

module TVRage
  API_URL = 'http://services.tvrage.com/feeds'

  class Episode
    attr_accessor :season, :episode_number, :title, :airdate

    def initialize(season, epno, title, airdate)
      @season = season
      @episode_number = epno
      @title = title
      @airdate = airdate
    end

    def to_s
      "#{@season}x#{@episode_number} - #{@title}"
    end
  end

  class Show
    attr_accessor :sid, :name, :startyear, :endyear, :episodelist

    # TODO: get episode list only when needed

    def initialize(sid, name = nil, startyear = nil, endyear = nil)
      @sid = sid
      if name.nil? && startyear.nil? && endyear.nil?
        show_info = get_show_info
        @name = show_info[:name]
        @startyear = show_info[:started]
        @endyear = show_info[:ended]
      else
        @name = name
        @startyear = startyear
        @endyear = if endyear != '0' then endyear else nil end
      end
    end

    def to_h
      { 'sid': @sid, 'name': @name, 'started': @startyear, 'ended': @endyear,
        'episodes': @episodelist }
    end

    def fetch_episodes
      @episodelist = filter_episode_list get_episode_list
    end

    def random_episode
      fetch_episodes if @episodelist.nil? || @episodelist.empty?
      begin
        random_index = rand(0..@episodelist.length - 1)
        ep = @episodelist[random_index]
      end while ep.airdate > Date.today
      ep
    end<commit_msg>add JSON serialisation in preparation of caching<commit_after>require 'restclient'
require 'nokogiri'
require 'date'

module TVRage
  API_URL = 'http://services.tvrage.com/feeds'

  class JSONable
    def to_json
      hash = {}
      self.instance_variables.each do |var|
        hash[var] = self.instance_variable_get var
      end
      hash.to_json
    end
    def from_json! string
      JSON.load(string).each do |var, val|
        self.instance_variable_set var, val
      end
    end
  end

  class Episode < JSONable
    attr_accessor :season, :episode_number, :title, :airdate

    def initialize(season, epno, title, airdate)
      @season = season
      @episode_number = epno
      @title = title
      @airdate = airdate
    end

    def to_s
      "#{@season}x#{@episode_number} - #{@title}"
    end
  end

  class Show < JSONable
    attr_accessor :sid, :name, :startyear, :endyear, :episodelist

    # TODO: get episode list only when needed

    def initialize(sid, name = nil, startyear = nil, endyear = nil)
      @sid = sid unless sid.nil?
      if !sid.nil? && name.nil? && startyear.nil? && endyear.nil?
        show_info = get_show_info
        @name = show_info[:name]
        @startyear = show_info[:started]
        @endyear = show_info[:ended]
      else
        @name = name
        @startyear = startyear
        @endyear = if endyear != '0' then endyear else nil end
      end
    end

    def to_h
      { 'sid': @sid, 'name': @name, 'started': @startyear, 'ended': @endyear,
        'episodes': @episodelist }
    end

    def fetch_episodes
      @episodelist = filter_episode_list get_episode_list
    end

    def random_episode
      fetch_episodes if @episodelist.nil? || @episodelist.empty?
      begin
        random_index = rand(0..@episodelist.length - 1)
        ep = @episodelist[random_index]
      end while ep.airdate > Date.today
      ep
    end
{'fraction_non_alphanumeric': 0.10434525569021579, 'fraction_numerical': 0.0017735737511084836, 'mean_word_length': 2.4994829369183043, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7500051', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1131, 'n_words': 291}