# smell code and idea for clear gem :) # todo: wrap to gem, parse m3u8, ts downloader to file require 'httparty' class RutubeList APIS = { login: 'https://pass.rutube.ru/api/accounts/phone/login/', login_social: 'https://rutube.ru/social/auth/rupass/?callback_path=/social/login/rupass/', visitor: 'https://rutube.ru/api/accounts/visitor/', ad: 'https://mtr.rutube.ru/api/v3/interactive/', hls: 'https://rutube.ru/api/play/options/' } include HTTParty def initialize(phone:, password:, video_id:) @phone = phone @password = password @video_id = video_id @cookies = nil @response = nil end def call login! social_login! club_params_encrypted = grab_club_params! club_token = grab_club_token!(club_params_encrypted: club_params_encrypted) grab_hls!(club_token: club_token) end private def login! @response = self.class.post( APIS[:login], body: { phone: @phone, password: @password } ) update_cookie! end def social_login! @response = self.class.get( APIS[:login_social], headers: { Cookie: @cookies.to_cookie_string } ) update_cookie! end def grab_club_params! @response = self.class.get( APIS[:visitor], headers: { Cookie: @cookies.to_cookie_string } ) update_cookie! @response["club_params_encrypted"] end def grab_club_token!(club_params_encrypted:) @response = self.class.get( "#{APIS[:ad]}?#{club_params_encrypted}&video_id=#{@video_id}", headers: { Cookie: @cookies.to_cookie_string } ) @response["award"] end def grab_hls!(club_token:) @response = self.class.get( "#{APIS[:hls]}#{@video_id}?club_token=#{club_token}", headers: { Cookie: @cookies.to_cookie_string } ) self.class.get(@response["video_balancer"]["m3u8"]) end def update_cookie! @cookies = parse_cookie(@response) end def parse_cookie(response) cookie_hash = HTTParty::CookieHash.new response.get_fields('Set-Cookie').each { |c| cookie_hash.add_cookies(c) } cookie_hash end end response = RutubeList.new( phone: "", password: , video_id: "" ).call puts response