API Movie With Google Drive Link
With this article, I want to show you guy about the secret of google drive link video, It has been used by many website movies include phimmoi, moviehdkh, kisscartoon or xmovies8. They already use that technology to get link video from google drive by PHP, ASP.net. Now I will show you guy how ...
With this article, I want to show you guy about the secret of google drive link video, It has been used by many website movies include phimmoi, moviehdkh, kisscartoon or xmovies8. They already use that technology to get link video from google drive by PHP, ASP.net. Now I will show you guy how to use Ruby on Rails to get that links.
We will start with a simple rails application as below.
Create the Rails App
Type the following at the command prompt:
$ rails new api-gdrive-video $ cd api-gdrive-video $ bundle
Create the Movies Controller
Create the movies controller using the Rails generator, add routes to config/routes.rb, and add methods for showing, creating, and listing movies.
$ rails g controller movies
Then open config/routes.rb and add this resource:
Rails.application.routes.draw do root "movies#index" resources :movies, only: [:index, :create] end
To get links video from google drive video file we need to use one gem that it is called Mechanize. It allows us to get file from url of links video google drive. Let's add gem 'mechanize' into file GemFile and run bundle install.
Class logic
Now, we need to create a file name app/logic/google_drive.rb. After that we fill the codes into that file as below:
class GoogleDrive class << self REDIRECT_URL = "https://redirector.googlevideo.com/videoplayback?" VIDEO_PLAYBACK = "com/videoplayback?" FMT_STREAM_MAP = "fmt_stream_map=" FMT_LIST = "&fmt_list" DOC_GOOGLE = "docs.google.com/file/d/" MAIL = "https://mail.google.com/e/get_video_info?docid=" ITAGS = [{label: "360", itag: "itag=18"}, {label: "480", itag: "itag=59"}, {label: "720", itag: "itag=22"}, {label: "1080", itag: "itag=37"}] DRIVEID = "&driveid" # format url: # https://docs.google.com/file/d/{file_id}/view # https://docs.google.com/file/d/{file_id}/preview # https://docs.google.com/file/d/{file_id}/edit def list_link_videos url file_id = get_file_id url spreadsheet_url = MAIL + file_id body = page_body spreadsheet_url body = decode_body body url_arr = split_body body find_link_mp4(url_arr).flatten rescue "" end private def get_file_id url url.split(DOC_GOOGLE)[1].remove("/view", "/preview", "/edit") end def page_body url agent = Mechanize.new page = agent.get url page.body end # We need to decode 2 times of body def decode_body body URI.unescape (URI.unescape body) end def split_body body result = [] remove_fmt_stream = body.split(FMT_STREAM_MAP)[1] remove_fmt_list = remove_fmt_stream.split(FMT_LIST)[0] remove_fmt_list.split("|").each do |link| if link.include? "https" result << link end end result end # Our links that include mp4 and flv. But we only need to use mp4 def find_link_mp4 links links.inject([]) do |result, link| if link.present? result << link.split(DRIVEID)[0] end end end def check_link_video link result = "" ITAGS.each do |itag| if link.include? itag[:itag] link = add_link_redirect_google link result = {file: link, type: "mp4", label: itag[:label]} end end result end def add_link_redirect_google url REDIRECT_URL + url.split(VIDEO_PLAYBACK)[1] end end end
You test with your logic class above in rails console:
url = "https://docs.google.com/file/d/0B6VYU2mjTdy0WVRjb1BJUU1hYXM/view" test = GoogleDrive.list_link_videos url => [{:file=>"https://redirector.googlevideo.com/videoplayback?id=19e3c7fd6fd45f15&itag=18&source=webdrive&requiressl=yes&ttl=transient&mm=30&mn=sn-i3b7kn7y&ms=nxu&mv=m&pl=24&mime=video/mp4&lmt=1480990883861520&mt=1486433662&ip=1.55.242.188&ipbits=24&expire=1486448107&sparams=ip,ipbits,expire,id,itag,source,requiressl,ttl,mm,mn,ms,mv,pl,mime,lmt&signature=85587FE51A03DB230F0C6AB405F13B740F6ACD2E.07DAFA05888E9B7E72166698A570440DD7F0048A&key=ck2&app=explorer", :type=>"mp4", :label=>"360"}, {:file=>"https://redirector.googlevideo.com/videoplayback?id=19e3c7fd6fd45f15&itag=22&source=webdrive&requiressl=yes&ttl=transient&mm=30&mn=sn-i3b7kn7y&ms=nxu&mv=m&pl=24&mime=video/mp4&lmt=1480991186991194&mt=1486433662&ip=1.55.242.188&ipbits=24&expire=1486448107&sparams=ip,ipbits,expire,id,itag,source,requiressl,ttl,mm,mn,ms,mv,pl,mime,lmt&signature=B03D9642C17081DD62C7044FF20FF820BD4AC034.9F3D2F3517C478C5A4C4C8BA2E0B25E9A2B89BB6&key=ck2&app=explorer", :type=>"mp4", :label=>"720"}, {:file=>"https://redirector.googlevideo.com/videoplayback?id=19e3c7fd6fd45f15&itag=59&source=webdrive&requiressl=yes&ttl=transient&mm=30&mn=sn-i3b7kn7y&ms=nxu&mv=m&pl=24&mime=video/mp4&lmt=1480991172772703&mt=1486433662&ip=1.55.242.188&ipbits=24&expire=1486448107&sparams=ip,ipbits,expire,id,itag,source,requiressl,ttl,mm,mn,ms,mv,pl,mime,lmt&signature=6A1F51D66474BDF6CDEE76F8E15A7821A811B935.19A71F08DE4DBCD7D4A4C1AA6E6C237E4F4DEC83&key=ck2&app=explorer", :type=>"mp4", :label=>"480"}]
View For Input URl
Now let's start to create view to input url for our logic. Create a file app/views/movies/index.html.erb, open file that currently created and fill it as code below:
<div class="container"> <div class="panel panel-info"> <div class="panel-heading"> <h1>API Video Google Drive</h1> </div> <div class="panel-body"> <%= form_tag movies_path, method: :post do %> <div class="form-group"> <%= url_field_tag "url", nil, class: "form-control", required: true, placeholder: "https://docs.google.com/file/d/xxxxxxxxxxxxxxxx/view" %> </div> <div class="form-group"> <%= submit_tag "Get Video Links", class: "btn btn-success" %> </div> <% end %> </div> </div> <%= @links.present? ? render("result") : Your url have a problem %> </div>
And we create one more file app/views/movies/_result.html.erb to show about result with codes below.
<div class="panel panel-info"> <div class="panel-body"> <div class="result"> <% @links.each do |link| %> <dl> <dt>File:</dt> <dd><%= link[:file] %></dd> <dt>Type:</dt> <dd><%= link[:type] %></dd> <dt>Label:</dt> <dd><%= link[:label] %></dd> </dl> <% end %> </div> </div> </div>
Now we need to fill other code in our controller app/controllers/movies_controller.rb.
class MoviesController < ApplicationController def index end def create url = params[:url] @links = GoogleDrive.list_link_videos url render :index end end
Now you can run our application:
Note: THAT URL JUST WORKS WITH SHARE ANYONE, IF YOU WANT TO WANT WORK WITH URL PUBLIC OR PRIVATE URL, PLEASE CONTACT TO ME
code project: Api Gdrive Video