<commit_before># encoding: UTF-8
require 'htmlentities'
require 'net/http'

class VideoInfo
  module Providers
    class Vkontakte < Provider
      attr_accessor :video_owner

      def self.usable?(url)
        !!(url =~ /(vk\.com)|(vkontakte\.ru)/)
      end

      def provider
        'Vkontakte'
      end

      def description
        content = data[/"desc":"(.*?)",/, 1]
        HTMLEntities.new.decode(content)
      end
      alias_method :keywords, :description

      def width
        { 240 => 320,
          360 => 480,
          480 => 640,
          720 => 1280
        }[height].to_i
      end

      def height
        data[/url(\d+)/,1].to_i
      end

      def title
        data[/\="mv_title">(.*)<\/div>/, 1].gsub(' | ВКонтакте', '')
      end

      def view_count
        data[/mv_views_count_number\">.*?(\d+)/, 1].to_i
      end

      def embed_url
        youtube = data[
          /iframe\ id=\"video_player\".*src=\"(.*)\"\ frameborder=/, 1
        ]
        if youtube
          VideoInfo::Providers::Youtube.new(
            URI.unescape(youtube.gsub(/\\/, ''))
          ).embed_url
        else
          "//vk.com/video_ext.php?oid=#{video_owner}" +
            "&id=#{video_id}&hash=#{_data_hash}"
        end
      end

      def duration
        data[/"duration":(\d+)/, 1].to_i
      end

      private

      def _make_request(url, options)
        request = Net::HTTP::Post.new(url.path)
        request.body = URI.encode_www_form(options)
        conn = Net::HTTP.new(url.host, url.port)
        conn.use_ssl = true
        resp = conn.request(request)
        resp.body.force_encoding('cp1251').encode('UTF-8', undef: :replace)
      end

      def _set_data_from_api
        url = URI('https://vk.com/al_video.php')
        options['act'] = 'show'
        options['al'] = '1'
        options['video'] = "#{@video_owner}_#{@video_id}"
        _make_request(url, options)
      end

      def _error_found?(response)
        title = response.body[
          /<title>(.*)<\/title>/, 1
        ].force_encoding(
          'cp1251'
        ).encode(
          'UTF-8', undef: :replace
        )
        !!title.index('Ошибка')
      end

      def _response_code
        response = nil
        Net::HTTP.start(_api_base, 80) { |http| response = http.get(_api_path) }
        if _error_found? response
          "403"
        else
          response.code
        end
      end

      def _api_base
        URI.parse(url).host
      end

      def _api_path
        URI.parse(url).path
      end

      def _data_hash
        data[/"hash2":"(\w+)/, 1]<commit_msg>Change double quotes to single quotes<commit_after># encoding: UTF-8
require 'htmlentities'
require 'net/http'

class VideoInfo
  module Providers
    class Vkontakte < Provider
      attr_accessor :video_owner

      def self.usable?(url)
        !!(url =~ /(vk\.com)|(vkontakte\.ru)/)
      end

      def provider
        'Vkontakte'
      end

      def description
        content = data[/"desc":"(.*?)",/, 1]
        HTMLEntities.new.decode(content)
      end
      alias_method :keywords, :description

      def width
        { 240 => 320,
          360 => 480,
          480 => 640,
          720 => 1280
        }[height].to_i
      end

      def height
        data[/url(\d+)/,1].to_i
      end

      def title
        data[/\="mv_title">(.*)<\/div>/, 1].gsub(' | ВКонтакте', '')
      end

      def view_count
        data[/mv_views_count_number\">.*?(\d+)/, 1].to_i
      end

      def embed_url
        youtube = data[
          /iframe\ id=\"video_player\".*src=\"(.*)\"\ frameborder=/, 1
        ]
        if youtube
          VideoInfo::Providers::Youtube.new(
            URI.unescape(youtube.gsub(/\\/, ''))
          ).embed_url
        else
          "//vk.com/video_ext.php?oid=#{video_owner}" +
            "&id=#{video_id}&hash=#{_data_hash}"
        end
      end

      def duration
        data[/"duration":(\d+)/, 1].to_i
      end

      private

      def _make_request(url, options)
        request = Net::HTTP::Post.new(url.path)
        request.body = URI.encode_www_form(options)
        conn = Net::HTTP.new(url.host, url.port)
        conn.use_ssl = true
        resp = conn.request(request)
        resp.body.force_encoding('cp1251').encode('UTF-8', undef: :replace)
      end

      def _set_data_from_api
        url = URI('https://vk.com/al_video.php')
        options['act'] = 'show'
        options['al'] = '1'
        options['video'] = "#{@video_owner}_#{@video_id}"
        _make_request(url, options)
      end

      def _error_found?(response)
        title = response.body[
          /<title>(.*)<\/title>/, 1
        ].force_encoding(
          'cp1251'
        ).encode(
          'UTF-8', undef: :replace
        )
        !!title.index('Ошибка')
      end

      def _response_code
        response = nil
        Net::HTTP.start(_api_base, 80) { |http| response = http.get(_api_path) }
        if _error_found? response
          '403'
        else
          response.code
        end
      end

      def _api_base
        URI.parse(url).host
      end

      def _api_path
        URI.parse(url).path
      end

      def _data_hash
        data[/"hash2":"(\w+)/, 1]
{'fraction_non_alphanumeric': 0.14695752009184845, 'fraction_numerical': 0.01951779563719862, 'mean_word_length': 1.6887860082304527, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '5985255', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1702, 'n_words': 324}