<commit_before>class ShowController < ApplicationController
  include TrendingHelper
  include ShowHelper

  def id
    require 'open-uri'
    require 'crack/xml'

    xml_data = HTTParty.get(tvdb_get_show_url(params[:tvdb_id]))

    xml_to_json = Crack::XML.parse(xml_data.body)

    base_obj = xml_to_json['Data']['Series']

    all_seasons = Array.new
    default_season_no = 0

    single_season = Array.new
    xml_to_json['Data']['Episode'].each do |episode|
      if episode['filename'].nil?
        image_url = nil
      else
        image_url = 'http://www.thetvdb.com/banners/' + episode['filename']
      end
      if episode['EpisodeName'].nil?
        episode_name = 'TBA'
      else
        episode_name = episode['EpisodeName']
      end

      #puts episode

      #puts episode['FirstAired']

      if !episode['FirstAired'].nil?
        show_air_time = episode['FirstAired'] + ' ' + base_obj['Airs_Time']
        #puts show_air_time
        begin
          new_time = Time.strptime(show_air_time, '%Y-%m-%d %I:%M %p')
        rescue
          new_time = Time.strptime(show_air_time, '%Y-%m-%d %I:%M%p')
        end
        new_time = new_time.strftime('%d-%m-%Y %I:%M %p')
      else
        new_time = nil
      end

      #puts new_time

      if episode['Combined_season'].to_i == default_season_no
        single_season << {
            title: episode_name,
            air_date_time: new_time,
            overview: episode['Overview'],
            image: image_url,
            rating: episode['Rating'],
            rating_count: episode['RatingCount'],
            writer: episode['Writer'],
            watched: false,
            episode: episode['EpisodeNumber']
        }
      else
        all_seasons <<  {
            episodes: single_season,
            'season': default_season_no
        }
        default_season_no += 1
        single_season = []
        single_season << {
            title: episode_name,
            air_date_time: new_time,
            overview: episode['Overview'],
            image: image_url,
            rating: episode['Rating'],
            rating_count: episode['RatingCount'],
            writer: episode['Writer'],
            watched: false,
            episode: episode['EpisodeNumber']
        }
      end
    end
    all_seasons <<  {
        episodes: single_season,
        'season': default_season_no
    }

    series_object = {
        id: base_obj['id'],
        title: base_obj['SeriesName'],
        actors: base_obj['Actors'],
        genre: base_obj['Genre'],
        first_aired: base_obj['FirstAired'],
        air_time: base_obj['Airs_Time'],
        network: base_obj['Network'],
        overview: base_obj['Overview'],
        rating: base_obj['Rating'],
        rating_count: base_obj['RatingCount'],
        runtime: base_obj['Runtime'],
        status: base_obj['Status'].titleize,
        banner: 'http://www.thetvdb.com/banners/' + base_obj['banner'],
        fanart: 'http://www.thetvdb.com/banners/' + base_obj['fanart'],
        image: 'http://www.thetvdb.com/banners/' + base_obj['poster'],
        seasons: all_seasons
    }<commit_msg>exclude if no season 0<commit_after>class ShowController < ApplicationController
  include TrendingHelper
  include ShowHelper

  def id
    require 'open-uri'
    require 'crack/xml'

    xml_data = HTTParty.get(tvdb_get_show_url(params[:tvdb_id]))

    xml_to_json = Crack::XML.parse(xml_data.body)

    base_obj = xml_to_json['Data']['Series']

    all_seasons = Array.new
    default_season_no = 0

    single_season = Array.new
    xml_to_json['Data']['Episode'].each do |episode|
      if episode['filename'].nil?
        image_url = nil
      else
        image_url = 'http://www.thetvdb.com/banners/' + episode['filename']
      end
      if episode['EpisodeName'].nil?
        episode_name = 'TBA'
      else
        episode_name = episode['EpisodeName']
      end

      #puts episode

      #puts episode['FirstAired']

      if !episode['FirstAired'].nil?
        show_air_time = episode['FirstAired'] + ' ' + base_obj['Airs_Time']
        #puts show_air_time
        begin
          new_time = Time.strptime(show_air_time, '%Y-%m-%d %I:%M %p')
        rescue
          new_time = Time.strptime(show_air_time, '%Y-%m-%d %I:%M%p')
        end
        new_time = new_time.strftime('%d-%m-%Y %I:%M %p')
      else
        new_time = nil
      end

      #puts new_time

      if episode['Combined_season'].to_i == default_season_no
        single_season << {
            title: episode_name,
            air_date_time: new_time,
            overview: episode['Overview'],
            image: image_url,
            rating: episode['Rating'],
            rating_count: episode['RatingCount'],
            writer: episode['Writer'],
            watched: false,
            episode: episode['EpisodeNumber']
        }
      else
        if single_season.any?
          all_seasons << {
              episodes: single_season,
              'season': default_season_no
          }
        end
        default_season_no += 1
        single_season = []
        single_season << {
            title: episode_name,
            air_date_time: new_time,
            overview: episode['Overview'],
            image: image_url,
            rating: episode['Rating'],
            rating_count: episode['RatingCount'],
            writer: episode['Writer'],
            watched: false,
            episode: episode['EpisodeNumber']
        }
      end
    end
    all_seasons << {
        episodes: single_season,
        'season': default_season_no
    }

    series_object = {
        id: base_obj['id'],
        title: base_obj['SeriesName'],
        actors: base_obj['Actors'],
        genre: base_obj['Genre'],
        first_aired: base_obj['FirstAired'],
        air_time: base_obj['Airs_Time'],
        network: base_obj['Network'],
        overview: base_obj['Overview'],
        rating: base_obj['Rating'],
        rating_count: base_obj['RatingCount'],
        runtime: base_obj['Runtime'],
        status: base_obj['Status'].titleize,
        banner: 'http://www.thetvdb.com/banners/' + base_obj['banner'],
        fanart: 'http://www.thetvdb.com/banners/' + base_obj['fanart'],
        image: 'http://www.thetvdb.com/banners/' + base_obj['poster'],
        seasons: all_seasons
    }
{'fraction_non_alphanumeric': 0.12687865844012022, 'fraction_numerical': 0.0007910140800506249, 'mean_word_length': 2.1721023582538885, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1988777', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1939, 'n_words': 349}