<commit_before>require 'estore_conventions'
require 'active_record_content_blob'

module YoutubeEstore
  class Video < ActiveRecord::Base
    include EstoreConventions
    include ActiveRecordContentBlob::Blobable
    

    attr_datetime :published_at

    before_save :calculate_approval_rating

    belongs_to :channel, primary_key: :t_id

    attr_accessible :duration_seconds, :category_id, :description, :title, :published_at, :view_count, :favorite_count, :is_embeddable, :likes, :dislikes, :approval_rating, :t_id, 
      :channel_id, :username, :default_thumbnail, :category_id, :comment_count

    def link
      "http://www.youtube.com/watch?v=#{t_id}"
    end

    def thumbnail
      self.default_thumbnail.gsub('default', 'hqdefault')
    end

    def source_link
      "https://www.youtube.com/videos/#{t_id}"
    end

################### Class Methods #######################


    # returns float
    def self.overall_approval_rating  # channel.overall_approval_rating_of_videos
      v = (self.sum(:likes) + self.sum(:dislikes)).to_f

      if v == 0
        return 0
      else
        self.sum(:likes) / v
      end
    end



    def self.likes_count
      self.sum(:likes)
    end

    def self.likes_count_past_month
      self.past_month.sum(:likes)
    end

    def self.dislikes_count
      self.sum(:dislikes)
    end

    def self.dislikes_count_past_month
      self.past_month.sum(:dislikes)
    end

    def self.favorites_count
      self.sum(:favorite_count)
    end

    def self.favorites_count_past_month
      self.past_month.sum(:favorite_count)
    end

    def self.views_count
      self.sum(:view_count)
    end

    def self.views_count_past_month
      self.past_month.sum(:view_count)
    end



   # returns integer
    def self.average_duration   # channel.     average_duration   _of_videos
       self.average(:duration_seconds).to_i
    end

    # untested, separate from self.longest
    # returns seconds(Integer)
    def self.longest_duration
      if v = self.longest
        return v.duration_seconds
      end
    end


    # returns single video, untested
    def self.least_approved
      self.order('approval_rating ASC').first
    end

    # returns single video, untested
    def self.most_approved
      self.order('approval_rating DESC').first
    end




    # untested, just returns one
    def self.longest
      self.order('duration_seconds DESC').first
    end

    # untested, just returns one
    def self.shortest
      self.order('duration_seconds ASC').first
    end
    
    # untested, seperate from self.most_viewed
    # returns number of views
    def self.highest_view_count
      if v = highest_viewed
        return v.view_count
      end
    end

    # untested, different implementation than most_viewed<commit_msg>approval ratings<commit_after>require 'estore_conventions'
require 'active_record_content_blob'

module YoutubeEstore
  class Video < ActiveRecord::Base
    include EstoreConventions
    include ActiveRecordContentBlob::Blobable
    

    attr_datetime :published_at

    before_save :calculate_approval_rating

    belongs_to :channel, primary_key: :t_id

    attr_accessible :duration_seconds, :category_id, :description, :title, :published_at, :view_count, :favorite_count, :is_embeddable, :likes, :dislikes, :approval_rating, :t_id, 
      :channel_id, :username, :default_thumbnail, :category_id, :comment_count

    def link
      "http://www.youtube.com/watch?v=#{t_id}"
    end

    def thumbnail
      self.default_thumbnail.gsub('default', 'hqdefault')
    end

    def source_link
      "https://www.youtube.com/videos/#{t_id}"
    end

################### Class Methods #######################


    # returns float
    def self.overall_approval_rating  # channel.overall_approval_rating_of_videos
      v = (self.sum(:likes) + self.sum(:dislikes)).to_f

      if v == 0
        return 0
      else
        self.sum(:likes) / v
      end
    end



    def self.likes_count
      self.sum(:likes)
    end

    def self.likes_count_past_month
      self.past_month.sum(:likes)
    end

    def self.dislikes_count
      self.sum(:dislikes)
    end

    def self.dislikes_count_past_month
      self.past_month.sum(:dislikes)
    end

    def self.favorites_count
      self.sum(:favorite_count)
    end

    def self.favorites_count_past_month
      self.past_month.sum(:favorite_count)
    end

    def self.views_count
      self.sum(:view_count)
    end

    def self.views_count_past_month
      self.past_month.sum(:view_count)
    end



   # returns integer
    def self.average_duration   # channel.     average_duration   _of_videos
       self.average(:duration_seconds).to_i
    end

    # untested, separate from self.longest
    # returns seconds(Integer)
    def self.longest_duration
      if v = self.longest
        return v.duration_seconds
      end
    end


    # returns single video, untested
    def self.least_approved
      self.enough_evaluations.order('approval_rating ASC').first
    end

    # returns single video, untested
    def self.most_approved
      self.enough_evaluations.order('approval_rating DESC').first
    end

    # returns Hash of keys, from 0 to 100, with values of videos by approval rating

    def self.approval_rating_distribution
      self.enough_evaluations.group('CAST( ROUND(approval_rating * 100) AS UNSIGNED )').count
    end

    # untested
    scope :enough_views, where('view_count > 1000')
    scope :enough_ratings, where('likes + dislikes > 10')
    scope :enough_evaluations, ->(){ enough_views.enough_ratings }

    # untested, just returns one
    def self.longest
      self.order('duration_seconds DESC').first
    end

    # untested, just returns one
    def self.shortest
      self.order('duration_seconds ASC').first
    end
    
    # untested, seperate from self.most_viewed
    # returns number of views
    def self.highest_view_count
      if v = highest_viewed
        return v.view_count
      end
    end

    # untested, different implementation than most_viewed
{'fraction_non_alphanumeric': 0.09265070189925681, 'fraction_numerical': 0.0028075970272502066, 'mean_word_length': 2.914673561732385, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2744011', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1883, 'n_words': 409}