Advertisement
Ya-go

YouTube Video Downloader Delphi Source

Dec 29th, 2011
3,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.59 KB | None | 0 0
  1. unit uDownload;
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes,
  7.   SysUtils,
  8.   IDHttp,
  9.   WinInet,
  10.   Windows;
  11.  
  12. const
  13.   c_YouTubeHeader = 'http://www.youtube.com/watch?v=';
  14.   c_YouTubeHost = 'http://www.youtube.com/';
  15.  
  16. procedure TryDownloadYouTubeVideo(UrlPath: string; Dir: string);
  17.  
  18. implementation
  19.  
  20. procedure TryDownloadYouTubeVideo(UrlPath: string; Dir: string);
  21. var
  22.   http: TIdHttp;
  23.   strm: TStringStream;
  24.   FName: string;
  25.   FUrl: string;
  26.  
  27.   function fnc_GetSourcePath: string;
  28.   const
  29.     c_sBeginPath = 'img.src = "http:\/\/';
  30.     c_sMiddlePath = '\/';
  31.     c_dMiddlePath = '/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2C';
  32.     c_sEndPath = '"';
  33.     c_dBeginPath = 'http://tc.';
  34.     c_dEndPath = 'redirect_counter=1';
  35.  
  36.     c_sBreakChar1 = '%2C';
  37.     c_sBreakChar2 = '\u0026';
  38.  
  39.     c_dBreakChar = '&';
  40.  
  41.   var
  42.     tmpstr: string;
  43.     tmpstrlst: TStringList;
  44.     tmpRes: string;
  45.  
  46.     function fnc_IsTrash: Boolean;
  47.     begin
  48.       Result := (Pos(c_sBreakChar1, tmpstr) <> 0) and
  49.                 (Pos(c_sBreakChar2, tmpstr) <> 0) and
  50.                 (Pos(c_sBreakChar1, tmpstr) < Pos(c_sBreakChar2, tmpstr));
  51.     end;
  52.  
  53.   begin
  54.     Result := '';
  55.     tmpstrlst := TStringList.Create;
  56.     try
  57.       tmpstr := strm.DataString;
  58.       Delete(tmpstr, 1, Pos(c_sBeginPath, tmpstr) + Length(c_sBeginPath) - 1);
  59.       Delete(tmpstr, Pos(c_sEndPath, tmpstr), Length(tmpstr));
  60.  
  61.       tmpstrlst.Delimiter := '.';
  62.       tmpstrlst.DelimitedText := Copy(tmpstr, 1, Pos(c_sMiddlePath, tmpstr) - 1);
  63.  
  64.       tmpRes := Concat(c_dBeginPath,
  65.                        tmpstrlst.Strings[3],
  66.                        '.',
  67.                        Copy(tmpstrlst.Strings[4], 3, Length(tmpstrlst.Strings[4])),
  68.                        '.',
  69.                        tmpstrlst.Strings[5],
  70.                        '.',
  71.                        tmpstrlst.Strings[6],
  72.                        '.',
  73.                        tmpstrlst.Strings[7],
  74.                        c_dMiddlePath);
  75.  
  76.       while fnc_IsTrash do
  77.         Delete(tmpstr, 1, Pos(c_sBreakChar1, tmpstr) + Length(c_sBreakChar1) - 1);
  78.  
  79.       tmpstrlst.Delimiter := c_dBreakChar;
  80.       tmpstrlst.Clear;
  81.  
  82.       while Pos(c_sBreakChar2, tmpstr) <> 0 do
  83.       begin
  84.         tmpstrlst.Add(Copy(tmpstr, 1, Pos(c_sBreakChar2, tmpstr) - 1));
  85.         Delete(tmpstr, 1, Pos(c_sBreakChar2, tmpstr) + Length(c_sBreakChar2) - 1);
  86.       end;
  87.  
  88.       tmpstrlst.Add(tmpstr);
  89.       tmpstrlst.Add(c_dEndPath);
  90.  
  91.       tmpRes := Concat(tmpRes,
  92.                        tmpstrlst.DelimitedText);
  93.     finally
  94.       FreeAndNil(tmpstrlst);
  95.     end;
  96.  
  97.     Result := tmpRes;
  98.   end;
  99.  
  100.   function fnc_GetFileName: string;
  101.  
  102.     function fnc_GetDir: string;
  103.     begin
  104.       Result := Dir;
  105.       if Result[Length(Result)] <> '\' then
  106.         Result := Concat(Result, '\');
  107.     end;
  108.  
  109.   const
  110.     c_TitleBegin = '<meta name="title" content="';
  111.     c_TitleEnd = '"';
  112.   var
  113.     tmpstr: string;
  114.   begin
  115.     tmpstr := strm.DataString;
  116.     Delete(tmpstr, 1, Pos(c_TitleBegin, tmpstr) + Length(c_TitleBegin) - 1);
  117.     Delete(tmpstr, Pos(c_TitleEnd, tmpstr), Length(tmpstr));
  118.     tmpstr := UTF8ToUnicodeString(Trim(tmpstr));
  119.     Result := Concat(fnc_GetDir, tmpstr, '.avi');
  120.   end;
  121.  
  122. begin
  123.   http := TIdHTTP.Create(nil);
  124.   strm := TStringStream.Create;
  125.   try
  126.     http.Request.Host := c_YouTubeHost;
  127.     http.Get(UrlPath, strm);
  128.  
  129.     FName := fnc_GetFileName;
  130.     FUrl := fnc_GetSourcePath;
  131.  
  132.     http.Disconnect;
  133.     strm.Clear;
  134.     http.Get(FUrl, strm);
  135.  
  136.     strm.SaveToFile(FName);
  137.   finally
  138.     FreeAndNil(strm);
  139.     FreeAndNil(http);
  140.   end;
  141.  end;
  142.  
  143. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement