Code : Tout sélectionner
InitNetwork()
Structure YoutubDownloadLinks
Title.s;{1024}
Url.s ;{1024}
bitrate.l
Mimetype.s;{1024}
QualityFps.s;{1024}
Resolution.s;{1024}
EndStructure
#PBHTTP_QUERY_VERSION="#VERSION" ; http version
#PBHTTP_QUERY_RESPONSECODE="#RESPONSECODE" ; status code
#PBHTTP_QUERY_RESPONSEREASON="#RESPONSEREASON"; status code description
#PBHTTP_QUERY_DATE="Date:";server date
#PBHTTP_QUERY_CONTENT_TYPE="Content-Type:";mime type ;https://github.com/miguelmota/mime-ext/blob/master/src/data/mime_types.txt
#PBHTTP_QUERY_CONTENT_LENGTH = "Content-Length:";size of content
#PBHTTP_QUERY_COOKIE="Set-Cookie:";cookie
#PBHTTP_QUERY_LAST_MODIFIED="Last-Modified:";creation or last modified file
#PBHTTP_QUERY_ETAG="ETag:";etag
#PBHTTP_QUERY_ACCEPT_RANGES="Accept-Ranges:"; accept Ranges
#PBHTTP_QUERY_LOCATION="Location:"; location to redirect link
#PBHTTP_QUERY_CONTENT_DISPOSITION="Content-Disposition:";name of file
#PBHTTP_QUERY_EXPIRES="Expires:" ;Expires
Global MIMELIST.s="atom=application/atom+xml;woff=application/font-woff;woff2=application/font-woff2;jar=application/java-archive;war=application/java-archive;json=application/json;jsonld=application/ld+json;hqx=application/mac-binhex40;webmanifest=application/manifest+json;doc=application/msword;pdf=application/pdf;ps=application/postscript;rss=application/rss+xml;rtf=application/rtf;geojson=application/vnd.geo+json;kml=application/vnd.google-earth.kml+xml;kmz=application/vnd.google-earth.kmz;xls=application/vnd.ms-excel;eot=application/vnd.ms-fontobject;ppt=application/vnd.ms-powerpoint;pptx=application/vnd.openxmlformats-officedocument.presentationml.presentation;xlsx=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;docx=application/vnd.openxmlformats-officedocument.wordprocessingml.document;wmlc=application/vnd.wap.wmlc;7z=application/x-7z-compressed;bbaw=application/x-bb-appworld;torrent=application/x-bittorrent;crx=application/x-chrome-extension;cco=application/x-cocoa;ttf=application/x-font-ttf;jardiff=application/x-java-archive-diff;jnlp=application/x-java-jnlp-file;run=application/x-makeself;oex=application/x-opera-extension;pl=application/x-perl;rar=application/x-rar-compressed;rpm=application/x-redhat-package-manager;sea=application/x-sea;swf=application/x-shockwave-flash;sit=application/x-stuffit;tcl=application/x-tcl;webapp=application/x-web-app-manifest+json;crt=application/x-x509-ca-cert;xpi=application/x-xpinstall;xhtml=application/xhtml+xml;xml=application/xml;xsl=application/xslt+xml;zip=application/zip;midi=audio/midi;m4a=audio/mp4;mp3=audio/mpeg;ogg=audio/ogg;ra=audio/x-realaudio;wav=audio/x-wav;otf=font/opentype;bmp=image/bmp;gif=image/gif;jpeg=image/jpeg;png=image/png;svg=image/svg+xml;tiff=image/tiff;wbmp=image/vnd.wap.wbmp;webp=image/webp;ico=image/x-icon;jng=image/x-jng;appcache=text/cache-manifest;css=text/css;html=text/html;mml=text/mathml;txt=text/plain;vcard=text/vcard;xloc=text/vnd.rim.location.xloc;jad=text/vnd.sun.j2me.app-descriptor;wml=text/vnd.wap.wml;vtt=text/vtt;3gp=video/3gpp;mp4=video/mp4;mpeg=video/mpeg;ogv=video/ogg;mov=video/quicktime;webm=video/webm;flv=video/x-flv;mng=video/x-mng;asf=video/x-ms-asf;wmv=video/x-ms-wmv"
Procedure.s PBHTTP_HeaderGetQuery(Header.s,QueryType.s)
For f=1 To CountString(Header,#CRLF$)
Hi$= StringField(Header,f,#CRLF$)
If Left(Hi$,5) = "HTTP/"
Select QueryType
Case #PBHTTP_QUERY_VERSION
ProcedureReturn StringField(Hi$,1,Chr(32))
Case #PBHTTP_QUERY_RESPONSECODE
ProcedureReturn StringField(Hi$,2,Chr(32))
Case #PBHTTP_QUERY_RESPONSEREASON
ProcedureReturn StringField(Hi$,3,Chr(32))
EndSelect
ElseIf Left(Hi$,Len(QueryType)) = QueryType
ProcedureReturn Mid(Hi$,Len(QueryType)+2)
EndIf
Next
EndProcedure
Procedure.s String_Between(Txt.s,sStart.s,sEnd.s)
Protected fd.l=FindString(Txt,sStart),ilStr.l=Len(sStart),ilen.l = FindString(Txt,sEnd, fd+ilStr)- (fd+ilStr)
If sEnd = #Null$:ilen=Len(Txt):EndIf
If fd And ilen:ProcedureReturn Mid(Txt,fd+ilStr,ilen):EndIf
EndProcedure
Procedure YoutubGetDownloadLinks(YoutubWebSource.s,List YoutubDownloadLinksList.YoutubDownloadLinks())
ClearList(YoutubDownloadLinksList())
Protected Findytplayer.l=FindString(YoutubWebSource,"<script>var ytplayer"); ytplayer.config.args
If Findytplayer
YoutubWebSource = Mid(YoutubWebSource,Findytplayer,FindString(YoutubWebSource,"</script>",Findytplayer)-Findytplayer)
If YoutubWebSource
Protected Titel.s= String_Between(YoutubWebSource,Chr(34)+"title"+Chr(34)+":"+Chr(34),"|")
Protected linkDownload.s= String_Between(YoutubWebSource,Chr(34)+"url_encoded_fmt_stream_map"+Chr(34)+":"+Chr(34),Chr(34)+","); url_encoded_fmt_stream_map // traditional: contains video and audio streamadaptive_fmts // DASH: contains video Or audio stream
If linkDownload
Protected z.l,add.b,i.l,Ids.s:For i = 2 To CountString(linkDownload,"url=")+1
Ids=StringField(linkDownload,i,"url=")
AddElement(YoutubDownloadLinksList())
YoutubDownloadLinksList()\Title = Titel
Ids=StringField(Ids,1,",")
YoutubDownloadLinksList()\Url =URLDecoder(StringField(Ids,1,"\u0026"))
add=1
Next
ProcedureReturn add
EndIf
EndIf
EndIf
EndProcedure
Procedure.s Mime_Get_ex(mime$)
For t = 1 To CountString(MIMELIST,";")
txt$=StringField(MIMELIST,t,";")
If StringField(txt$,2,"=")=mime$
ProcedureReturn StringField(txt$,1,"=")
EndIf
Next
EndProcedure
*Buffer = ReceiveHTTPMemory("https://www.youtube.com/watch?v=kkGeOWYOFoA")
If *Buffer
Taille = MemorySize(*Buffer)
NewList YoutubDownloadLinksList.YoutubDownloadLinks()
If YoutubGetDownloadLinks(PeekS(*Buffer, Taille, #PB_UTF8),YoutubDownloadLinksList())
ForEach YoutubDownloadLinksList()
;YoutubDownloadLinksList()\Title
While 1
EnTete$ = GetHTTPHeader(YoutubDownloadLinksList()\Url)
Debug YoutubDownloadLinksList()\Url
RedirectCode.s= PBHTTP_HeaderGetQuery(EnTete$,#PBHTTP_QUERY_RESPONSECODE)
Debug RedirectCode
If Left(RedirectCode,1)="3"
Debug "Redirect link: " +PBHTTP_HeaderGetQuery(EnTete$,#PBHTTP_QUERY_LOCATION)
YoutubDownloadLinksList()\Url=PBHTTP_HeaderGetQuery(EnTete$,#PBHTTP_QUERY_LOCATION)
Continue
EndIf
GetExtensionmime$= Mime_Get_ex(PBHTTP_HeaderGetQuery(EnTete$,#PBHTTP_QUERY_CONTENT_TYPE))
GetSizeFile.q= Val(PBHTTP_HeaderGetQuery(EnTete$,#PBHTTP_QUERY_CONTENT_LENGTH))
Telechargement= ReceiveHTTPFile(YoutubDownloadLinksList()\Url, YoutubDownloadLinksList()\Title+"."+GetExtensionmime$, #PB_HTTP_Asynchronous)
If Telechargement
Repeat
Progression = HTTPProgress(Telechargement)
Select Progression
Case #PB_Http_Success
Debug "Téléchargement terminé (Taille: " + Str(GetSizeFile) + ")"
Case #PB_Http_Failed
Debug "Le téléchargement a échoué"
End
Default
Debug "Téléchargement en cours: " + Progression + " octets reçus sur : " + Str(GetSizeFile) +" octets " + Str((Progression*100)/GetSizeFile)+ " %"
EndSelect
Delay(500) ; Libérer le CPU
ForEver
Else
Debug "Erreur de téléchargement"
EndIf
Break
Wend
Next
EndIf
FreeMemory(*Buffer)
Else
Debug "Le téléchargement a échoué"
EndIf