API Movie với Google Drive Link
Trong bài viết này, tôi sẽ giới thiệu đến các bạn bí mật về link video cho google drive, thủ thuật được rất nhiều trang web phim sử dụng như phimmoi, moviehdkh, kisscartoon or xmovies8. Thủ thuật này giúp các trang web phim lấy link video từ google drive bằng PHP, ASP.net. Ở đây, chúng ta sẽ lấy link bằng Ruby on Rails.

Start Create Project
Chúng ta sẽ bắt đầu với ứng dụng rails đơn giản như dưới đây.
Tạo ứng dụng Rails
Gõ đoạn sau vào command prompt:
| 
 1 
2 
3 
4 
5 
 | 
 $   rails  new   api - gdrive - video 
$   cd  api - gdrive - video 
$   bundle 
 | 
Sau đó mở config/routes.rb và thêm tài nguyên sau:
| 
 1 
2 
3 
4 
5 
6 
 | 
 Rails . application . routes . draw  do 
   root   "movies#index" 
   resources   : movies ,   only :   [ : index ,   : create ] 
end 
 | 
Để lấy link video từ file video trên google drive, chúng ta cần gem tên Mechanize. Gem này sẽ giúp chính ta lấy URL link của video trên google drive. Hãy thêm gem 'mechanize' vào file GemFile và chạy bundle install.
Class logic
Đến đây, chúng ta cần tạo file tên app/logic/google_drive.rb. Sau đó, ta sẽ điền code vào file đó như dưới đây:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
 | 
 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/" 
     SPREADSHEETS   =   "https://spreadsheets.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   =   SPREADSHEETS   +   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 
 | 
Test với logic class bên trên trong rails console:
| 
 1 
2 
3 
4 
5 
 | 
 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 Cho Input URl
Kế tiếp, chúng ta sẽ phải tạo view đến input url cho logic của chúng ta. Tạo file app/views/movies/index.html.erb, mở file vừa tạo và điền như đoạn code dưới đây:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
 | 
 < 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 > 
 | 
Và hãy tạo thêm một file app/views/movies/_result.html.erb nữa để hiển thị kết quả, code như bên dưới.
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
 | 
 < 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 > 
 | 
Giờ thì chúng ta cần điền một đoạn code khác trong controller app/controllers/movies_controller.rb.
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
 | 
 class   MoviesController   <   ApplicationController 
   def  index 
   end 
   def  create 
     url   =   params [ : url ] 
     @ links   =   GoogleDrive . list_link_videos  url 
     render   : index 
   end 
end 
 | 
Ứng dụng đã chạy được rồi đấy.

Ghi chú: URL chỉ hoạt động với “SHARE ANYONE”
- Viết ứng dụng Smartphone và Tablet
 - 5 lý do sở hữu một ứng dụng di động là cần thiết đối với doanh nghiệp vừa và nhỏ
 - Các nền tảng công nghệ hỗ trợ cho khởi nghiệp tiết kiệm, hiệu quả,...
 - Hệ thống điều hành, tìm gọi và quản lý xe sử dụng công nghệ mới
 - Ứng dụng bán hàng trên smartphone, smart TV, mạng xã hội...
 - Hướng dẫn cài ứng dụng, phần mềm cho Android trực tiếp bằng tập tin APK
 - IoT là gì? ứng dụng của IoT trong cuộc sống hiện đại
 - Platform là gì?
 - Cách đổi tên thiết bị Android
 - Hệ thống order chuyên nghiệp cho quán ăn, cafe, nhà hàng...
 - Khắc phục lỗi đăng nhập Windows 10, không thể login vào Windows 10
 - 100 Website đặt backlink miễn phí chất lượng
 
DVMS chuyên:
  - Tư vấn, xây dựng, chuyển giao công nghệ Blockchain, mạng xã hội,...
  - Tư vấn ứng dụng cho smartphone và máy tính bảng, tư vấn ứng dụng vận tải thông minh, thực tế ảo, game mobile,...
  - Tư vấn các hệ thống theo mô hình kinh tế chia sẻ như Uber, Grab, ứng dụng giúp việc,...
  - Xây dựng các giải pháp quản lý vận tải, quản lý xe công vụ, quản lý   xe doanh nghiệp, phần mềm và ứng dụng logistics, kho vận, vé xe điện   tử,...
  - Tư vấn và xây dựng mạng xã hội, tư vấn giải pháp CNTT cho doanh nghiệp, startup,...
Vì sao chọn DVMS?
  - DVMS nắm vững nhiều công nghệ   phần mềm, mạng và viễn thông. Như Payment gateway, SMS gateway, GIS,   VOIP, iOS, Android, Blackberry, Windows Phone, cloud computing,…
  - DVMS có kinh nghiệm triển khai các hệ thống trên các nền tảng điện toán đám mây nổi tiếng như Google, Amazon, Microsoft,…
  - DVMS có kinh nghiệm thực tế tư vấn, xây dựng, triển khai, chuyển giao,   gia công các giải pháp phần mềm cho khách hàng Việt Nam, USA, Singapore,   Germany, France, các tập đoàn của nước ngoài tại Việt Nam,…
  
  Quý khách xem Hồ sơ năng lực của DVMS tại đây >> 
  
  Quý khách gửi yêu cầu tư vấn và báo giá tại đây >>









