Advertisement
Borrisholt

YoutubeU

Jun 1st, 2015
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.21 KB | None | 0 0
  1. unit YoutubeU;
  2.  
  3. interface
  4.  
  5. uses
  6.   WinAPI.Windows, System.Classes, Generics.Collections, Generics.Defaults;
  7.  
  8. {$M+}
  9.  
  10. type
  11.   TInformation = record
  12.     Size: TSize;
  13.     VideoLink: String;
  14.     MimeType: String;
  15.     Quality: String;
  16.     YoutubeID: String;
  17.     YoutubeURL: String;
  18.     procedure Clear;
  19.     function VideoTag: String;
  20.   private
  21.     function IsEmnpty: Boolean;
  22.     function CalcSize: Integer;
  23.   end;
  24.  
  25.   TYoutube = class
  26.   private
  27.     FYoutubeURL: String;
  28.     FYoutubeID: String;
  29.     FTitle: String;
  30.     FLength: string;
  31.     FInformations: TList<TInformation>;
  32.   protected
  33.     function GetSourceCode: string;
  34.     function GetJSON: string;
  35.   published
  36.     property Title: String read FTitle;
  37.     property Length: string read FLength;
  38.     property Informations: TList<TInformation> read FInformations;
  39.     property YoutubeURL: String read FYoutubeURL;
  40.     property YoutubeID: String read FYoutubeID;
  41.   public
  42.     constructor Create(const aYoutubeUrl: String; const aAutoParse: Boolean = True); reintroduce;
  43.     destructor Destroy; override;
  44.     procedure Parse;
  45.   end;
  46.  
  47.   TYoutubeThread = class(TThread)
  48.   private
  49.     FYoutube: TYoutube;
  50.   protected
  51.     procedure Execute; override;
  52.   public
  53.     constructor Create(aYoutubeUrl: String); reintroduce;
  54.     destructor Destroy; override;
  55.     property Youtube: TYoutube read FYoutube;
  56.   end;
  57.  
  58. implementation
  59.  
  60. uses
  61.   System.Sysutils, Web.HTTPApp, YoutubeURLParserU,
  62.   IdHTTP, IdComponent, IdSSLOpenSSL, XSuperObject;
  63.  
  64. function WorkingDir: String;
  65. begin
  66.   Result := ExtractFilePath(ParamStr(0)) + 'Tmp\';
  67.   ForceDirectories(Result)
  68. end;
  69.  
  70. { TYoutube }
  71.  
  72. constructor TYoutube.Create(const aYoutubeUrl: String; const aAutoParse: Boolean = True);
  73. begin
  74.   inherited Create;
  75.  
  76.   FInformations := TList<TInformation>.Create(TComparer<TInformation>.Construct(
  77.     function(const Left, Right: TInformation): Integer
  78.     begin
  79.       Result := Left.CalcSize - Right.CalcSize;
  80.     end));
  81.  
  82.   FYoutubeURL := aYoutubeUrl.Replace('https', 'http');
  83.   FYoutubeID := FYoutubeURL.Replace('http://www.youtube.com/watch?v=', '').Replace('http://youtu.be/', '');
  84.  
  85.   if aAutoParse then
  86.     Parse;
  87. end;
  88.  
  89. destructor TYoutube.Destroy;
  90. begin
  91.   FreeAndNil(FInformations);
  92.   inherited;
  93. end;
  94.  
  95. function TYoutube.GetJSON: string;
  96. var
  97.   p: Integer;
  98. begin
  99.   Result := GetSourceCode;
  100.   p := Result.IndexOf('ytplayer.config = ') + 'ytplayer.config = '.Length;
  101.  
  102.   if p < 0 then
  103.     Exit('');
  104.  
  105.   Result := Result.Substring(p);
  106.  
  107.   p := Result.IndexOf('</script>');
  108.  
  109.   if p < 0 then
  110.     Exit('');
  111.  
  112.   Result := Result.Substring(0, p);
  113. end;
  114.  
  115. function TYoutube.GetSourceCode: string;
  116. var
  117.   HTTP: TIdHTTP;
  118.   SSL: TIdSSLIOHandlerSocketOpenSSL;
  119.   Url: String;
  120. begin
  121.   Url := 'https://www.youtube.com/watch?v=' + FYoutubeID;
  122.   HTTP := TIdHTTP.Create(nil);
  123.   SSL := TIdSSLIOHandlerSocketOpenSSL.Create(nIL);
  124.   HTTP.IOHandler := SSL;
  125.   HTTP.Request.Accept := '*/*';
  126.   HTTP.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1) Gecko/20130101 Firefox/21.0';
  127.   HTTP.Request.Host := 'www.youtube.com';
  128.   HTTP.HandleRedirects := True;
  129.   HTTP.Request.Referer := Url;
  130.   try
  131.     try
  132.       Result := HTTP.Get(Url);
  133.     except
  134.       on e: Exception do
  135.         Result := e.Message;
  136.     end;
  137.   finally
  138.     FreeAndNil(HTTP);
  139.     FreeAndNil(SSL);
  140.   end;
  141. end;
  142.  
  143. procedure TYoutube.Parse;
  144. var
  145.   Buf: TArray<String>;
  146.   Tmp: string;
  147.   Videos, Videos2, Videos3, Formats, Formats2: TArray<string>;
  148.   SuperObject: ISuperObject;
  149.   i: Integer;
  150.   Information: TInformation;
  151. begin
  152.   Informations.Clear;
  153.  
  154.   SuperObject := SO(GetJSON);
  155.   try
  156.     FTitle := SuperObject['args."title"'].AsString;
  157.     Formats := SuperObject['args."fmt_list"'].AsString.Split([','], TStringSplitOptions.ExcludeEmpty);
  158.     FLength := SuperObject['args."length_seconds"'].AsString;
  159.     Videos := SuperObject['args."url_encoded_fmt_stream_map"'].AsString.Split([','], TStringSplitOptions.ExcludeEmpty);
  160.   finally
  161.     SuperObject := nil;
  162.   end;
  163.  
  164.   for i := 0 to System.Length(Videos) - 1 do
  165.   begin
  166.     Videos2 := Videos[i].Split(['&'], TStringSplitOptions.ExcludeEmpty);
  167.     Formats2 := Formats[i].Split(['/'], TStringSplitOptions.ExcludeEmpty);
  168.     Information.Clear;
  169.  
  170.     if System.Length(Formats2) > 1 then
  171.     begin
  172.       Buf := Formats2[1].Split(['x'], TStringSplitOptions.ExcludeEmpty);
  173.       if System.Length(Buf) = 2 then
  174.       begin
  175.         Information.Size.cx := StrToInt(Buf[0].Trim);
  176.         Information.Size.cy := StrToInt(Buf[1].Trim);
  177.       end;
  178.  
  179.     end;
  180.  
  181.     for Tmp in Videos2 do
  182.     begin
  183.       Videos3 := Tmp.Split(['='], TStringSplitOptions.ExcludeEmpty);
  184.       if Videos3[0] = 'url' then
  185.         Information.VideoLink := string(HTTPDecode(AnsiString(Videos3[1])))
  186.       else if Videos3[0] = 'quality' then
  187.         Information.Quality := Videos3[1]
  188.       else if Videos3[0] = 'type' then
  189.         Information.MimeType := string(HTTPDecode(AnsiString(Videos3[1])))
  190.     end;
  191.  
  192.     Information.YoutubeID := YoutubeID;
  193.     Information.YoutubeURL := YoutubeURL;
  194.  
  195.     if not Information.IsEmnpty then
  196.       Informations.Add(Information);
  197.   end;
  198.  
  199.   FInformations.Sort;
  200. end;
  201.  
  202. { TInformation }
  203.  
  204. function TInformation.CalcSize: Integer;
  205. begin
  206.   Result := Size.cx * Size.cy;
  207. end;
  208.  
  209. procedure TInformation.Clear;
  210. begin
  211.   Size.cx := 0;;
  212.   Size.cy := 0;;
  213.   VideoLink := '';
  214.   MimeType := '';
  215.   Quality := '';
  216. end;
  217.  
  218. function TInformation.IsEmnpty: Boolean;
  219. begin
  220.   Result := VideoLink = EmptyStr;
  221. end;
  222.  
  223. function TInformation.VideoTag: String;
  224. begin
  225.   Result := '<embed src="' + VideoLink + '"  ' + Format('width="%d" height="%d">', [Size.cx, Size.cy]);
  226. end;
  227.  
  228. { TYoutubeThread }
  229.  
  230. constructor TYoutubeThread.Create(aYoutubeUrl: String);
  231. begin
  232.   // Transform the yuotube url to the http://www.youtube.com/watch?v=<YoutubeID> format
  233.   with TYoutubeURLParser.Create(aYoutubeUrl) do
  234.     try
  235.       aYoutubeUrl := 'http://www.youtube.com/watch?v=' + YoutubeID;
  236.     finally
  237.       free;
  238.     end;
  239.  
  240.   FYoutube := TYoutube.Create(aYoutubeUrl, False);
  241.   inherited Create(False);
  242. end;
  243.  
  244. destructor TYoutubeThread.Destroy;
  245. begin
  246.   FreeAndNil(FYoutube);
  247.   inherited;
  248. end;
  249.  
  250. procedure TYoutubeThread.Execute;
  251. begin
  252.   inherited;
  253.   FreeOnTerminate := True;
  254.   FYoutube.Parse;
  255. end;
  256.  
  257. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement