Source & Exe -->YouTube.zip

Code : Tout sélectionner
;/ PureBasic 4.10b2
; Version 2.0
; Added : ProgressBar + Cancel Download + Size Downloaded / File Size / rate
; Added : Cancel Button / Resize / Translation in English
#Title="YouTube Download at "
Global VideoTitle.s,Path.s
Global DownloadURL.s, DownloadFilename.s, StopDownload, IsDownloading
;{/ dataSection
DataSection
Back:IncludeBinary "back.ico"
Forward:IncludeBinary "forward.ico"
Home:IncludeBinary "home.ico"
Save:IncludeBinary "Download.ico"
Stop:IncludeBinary "Cancel.ico"
Chemin:IncludeBinary "Path.ico"
EndDataSection
;}
Enumeration
#Web
#ToolBar
#Home
#Back
#Forward
#Path
#Download
#Cancel
#StatusBar
#Progress
EndEnumeration
Procedure.s FileNameConformation(File.s) ; Renvoie un nom de fichier sans les caractères interdits
For n=1 To Len(File)
c.s=Mid(File,n,1)
If FindString(c,"\",1)
Out.s+" "
ElseIf FindString(c,"/",1)
Out.s+" "
ElseIf FindString(c,":",1)
Out.s+" "
ElseIf FindString(c,"?",1)
Out.s+" "
ElseIf FindString(c,"*",1)
Out.s+" "
ElseIf FindString(c,"<",1)
Out.s+" "
ElseIf FindString(c,">",1)
Out.s+" "
ElseIf FindString(c,"|",1)
Out.s+" "
ElseIf FindString(c,Chr(34),1)
Out.s+" "
Else
Out.s+c
EndIf
Next
ProcedureReturn Out
EndProcedure
Procedure SaveYoutubeVideo(VideoId.s)
;/ ( Downloader With progress And Status By Joakim L. Christiansen )
DisableToolBarButton(#ToolBar,#Cancel,#False)
IsDownloading+#True
DownloadURL.s = "http://cache.googlevideo.com/get_video?video_id="+VideoId
DownloadFilename.s= Path+VideoTitle+".flv"
Protected hInet, hURL, Bytes
Protected BufferLength = 2048, Buffer.s = Space(BufferLength)
Protected Url.s = DownloadURL.s
Protected Filename.s = DownloadFilename.s
Protected File
Protected CurrentSize, PreviousSize, FileSize, time, BytesPerSecond
Protected Domain.s, String.s, i, BufferLengthWas = BufferLength
Protected hInetCon, hHttpOpenRequest, iretval
hInet = InternetOpen_("Downloader",0,0,0,0)
hURL = InternetOpenUrl_(hInet,Url.s,0,0,$80000000,0)
;Get filesize
Domain.s = StringField(Url.s,3,"/")
hInetCon = InternetConnect_(hInet,Domain.s,80,#Null,#Null,3,0,0)
If hInetCon
hHttpOpenRequest = HttpOpenRequest_(hInetCon,"HEAD",ReplaceString(Url.s,"http://"+Domain.s+"/",""),#Null,#Null,0,$80000000,0)
If hHttpOpenRequest
iretval = HttpSendRequest_(hHttpOpenRequest,#Null,0,0,0)
If iretval
HttpQueryInfo_(hHttpOpenRequest,19,@Buffer.s,@BufferLength,0) ;changes the buffer length
String.s = PeekS(@Buffer.s,BufferLength): BufferLength = BufferLengthWas
If Trim(String.s) = "200"
HttpQueryInfo_(hHttpOpenRequest,22,@Buffer.s,@BufferLength,0)
String.s = PeekS(@Buffer.s,BufferLength): BufferLength = BufferLengthWas
If FindString(String.s,"Content-Length:",1)
i = FindString(String.s,"Content-Length:",1) + Len("Content-Length:")
String.s = Mid(String.s,i,Len(String.s)-i)
FileSize = Val(Trim(String.s))
EndIf
EndIf
EndIf
EndIf
EndIf
;Download file and update status
If hURL
File = CreateFile(#PB_Any,Filename.s)
If File
time = ElapsedMilliseconds()
SetGadgetAttribute(#Progress,#PB_ProgressBar_Maximum,FileSize)
While InternetReadFile_(hURL,@Buffer.s,BufferLength,@Bytes) And Bytes > 0
If StopDownload
Break
EndIf
WriteData(File,@Buffer.s,Bytes)
CurrentSize + Bytes
SetGadgetState(#Progress,CurrentSize)
StatusBarText(#StatusBar,0," "+Str(CurrentSize/1024)+"/"+Str(FileSize/1024)+"kb - "+Str(BytesPerSecond/1024)+"kb/s")
If time < ElapsedMilliseconds() - 1000
time = ElapsedMilliseconds()
BytesPerSecond = CurrentSize - PreviousSize
PreviousSize = CurrentSize
EndIf
Wend
CloseFile(File)
Else
MessageRequester("Warning!","Error creating file!",#MB_ICONWARNING)
EndIf
StopDownload = #False
IsDownloading = #False
SetGadgetState(#Progress,0)
If CurrentSize < FileSize
DeleteFile(Filename.s)
EndIf
Else
MessageRequester("Warning!","Download failed!",#MB_ICONWARNING)
EndIf
InternetCloseHandle_(hURL)
InternetCloseHandle_(hInetCon)
InternetCloseHandle_(hInet)
DisableToolBarButton(#ToolBar,#Cancel,#True)
StatusBarText(#StatusBar,0,"")
EndProcedure
Procedure ToolBarImageButtonEx(ToolBarId,ButtonId,ImageId,ToolTip.s)
ToolBarImageButton(ButtonId,ImageId)
ToolBarToolTip(ToolBarId,ButtonId,ToolTip)
EndProcedure
Ini.s=GetPathPart(ProgramFilename())+StringField(GetFilePart(ProgramFilename()),1,".")+".ini"
OpenPreferences(Ini)
Path.s=ReadPreferenceString("Path","c:\")
;{ Visual Design
OpenWindow(0,0,0,1024,768,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget)
SetWindowTitle(0,#Title+Path)
CreateGadgetList(WindowID(0))
WebGadget(#Web,10,30,1024-20,768-60,"http://www.youtube.com")
CreateToolBar(#ToolBar,WindowID(0))
ToolBarImageButtonEx(#ToolBar,#Home,ImageID(CatchImage(#PB_Any,?Home)),"www.Youtube.com")
ToolBarImageButtonEx(#ToolBar,#Back,ImageID(CatchImage(#PB_Any,?Back)),"Back")
ToolBarImageButtonEx(#ToolBar,#Forward,ImageID(CatchImage(#PB_Any,?Forward)),"Forward")
ToolBarSeparator()
ToolBarImageButtonEx(#ToolBar,#Path,ImageID(CatchImage(#PB_Any,?Chemin)),"Backup Path")
ToolBarSeparator()
ToolBarImageButtonEx(#ToolBar,#Download,ImageID(CatchImage(#PB_Any,?Save)),"Download")
ToolBarImageButtonEx(#ToolBar,#Cancel,ImageID(CatchImage(#PB_Any,?Stop)),"Cancel Download")
CreateStatusBar(#StatusBar,WindowID(0))
AddStatusBarField(150)
AddStatusBarField(3000)
ProgressBarGadget(#Progress,170,5,1024-181,20,0,100, #PB_ProgressBar_Smooth)
DisableToolBarButton(#ToolBar,#Cancel,#True)
DisableToolBarButton(#ToolBar,#Download,#True)
;}
;{ Event Management
Repeat
evt=WaitWindowEvent()
If IsThread(Tid)=0 ; If not Downloading
StatusBarText(#StatusBar,0," "+VideoId.s) ; Show VideoId
If Len(VideoId.s)=11
DisableToolBarButton(#ToolBar,#Download,#False) ; Enable Download
EndIf
Else
DisableToolBarButton(#ToolBar,#Download,#True) ; Disable Download
EndIf
Select evt
Case #PB_Event_Gadget
If EventGadget()=#Web
If Url.s<>GetGadgetText(#Web)
StatusBarText(#StatusBar,1," "+GetGadgetText(#Web))
Url=GetGadgetText(#Web)
; Get the VideoId
VideoId.s=StringField(GetGadgetText(#Web),2,"=")
VideoId=StringField(VideoId,1,"&")
EndIf
EndIf
Case #PB_Event_SizeWindow
ResizeGadget(#Web,10,30,WindowWidth(0)-20,WindowHeight(0)-60)
ResizeGadget(#Progress,170,5,WindowWidth(0)-181,20)
Case #PB_Event_Menu
Select EventMenu()
Case #Home
SetGadgetText(#Web,"http://www.youtube.com")
Case #Back
SetGadgetState(#Web,#PB_Web_Back)
Case #Forward
SetGadgetState(#Web,#PB_Web_Forward)
Case #Path
temp.s=PathRequester("Backup Video Path",Path)
If temp<>""
Path=temp
SetWindowTitle(0,#Title+Path)
EndIf
Case #Download
If Not IsDownloading And Len(VideoId)=11
Source.s=GetGadgetItemText(#Web,#PB_Web_HtmlCode) ; Extract the HTML code from the WebGadget
; Extract the VideoName
g=FindString(Source,"<title>YouTube - ",1)+17
d=FindString(Source,"</title>",g)
VideoTitle.s=Mid(Source,g,d-g)
VideoTitle=FileNameConformation(VideoTitle)
Tid=CreateThread(@SaveYoutubeVideo(),VideoId)
EndIf
Case #Cancel
StopDownload = #True
IsDownloading = #False
SetGadgetState(#Progress,0)
DisableToolBarButton(#ToolBar,#Cancel,#True)
EndSelect
EndSelect
Until evt=#PB_Event_CloseWindow
WritePreferenceString("Path",Path)
;}