Jump to content

FLV Download Concept


quake101
 Share

Recommended Posts

I would first like to say that I'm still new to AutoIt and this is my first script that I'm posting to the public. The rest of my scripts have been very basic and not very useful. :(

I created this script as just a concept and I think it could grow into a great program with time (and maybe some help on my sloppy code). This program concept isn't like most flash video downing programs, instead of trying to detect what time of URL and have a method for each flash video hosting website this program uses IE to do most of the work as IE can display and download flash video by default. This should also allow any type of javascript or any other special methods of loading the video as IE will handle it.

Anyways, here is the script and I hope you guys enjoy it. Please don't make fun of my sloppy code as I'm still learning.

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <String.au3>
#include <Array.au3>
#include <INet.au3>
#include <IE.au3>
#include <File.au3>

;### Global Vars ###
$IECache = @UserProfileDir & "\Local Settings\Temporary Internet Files"
Global $twist[8], $tn=0
$twist[0] = '|'
$twist[1] = '/'
$twist[2] = '-'
$twist[3] = '\'
$twist[4] = '|'
$twist[5] = '/'
$twist[6] = '-'
$twist[7] = '\'

$GUI = GUICreate("FLV Download Concept", 640, 580, _
        (@DesktopWidth - 640) / 2, (@DesktopHeight - 580) / 2, _
        $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)
GUISetState (@SW_SHOW)

;URL InputBox and Button
GUICtrlCreateLabel("URL:", 2,10,24,30)
$url_input = GUICtrlCreateInput("", 30, 8, 260, 20)
$download_btn = GUICtrlCreateButton("Download FLV", 294, 8, 80, 20)

;## Show Status Info ##
$status_text = GUICtrlCreateLabel("Ready", 2,560,400,30)

;### Functions ###
Func _download_youtube($url)
    $dest_file = @ScriptDir & "\temp.flv"
    If FileExists($dest_file) Then
        FileDelete($dest_file)
    EndIf
    $video_id = StringReplace($url, "http://www.youtube.com/watch?v=", "")
    $Get_Value = Get_Value("www.youtube.com", $video_id)
    MsgBox(0, "", "http://www.youtube.com/get_video?video_id=" & $video_id & "&t=" & $Get_Value)
    InetGet("http://www.youtube.com/get_video?video_id=" & $video_id & "&t=" & $Get_Value, $dest_file, 0, 1)
    $size = InetGetSize("http://youtube.com/get_video?" & $video_id)
    ProgressOn("Downloading","Getting youtube video")
    While @InetGetActive
        ProgressSet(Floor(@InetGetBytesRead * 100 / $size))
    WEnd
    ProgressOff()
EndFunc

Func _check_url($url)
    If StringInStr($url,"youtube") Then
        _download_youtube($url)
    Else
    ;## Before we do anything, we should clear the user's IE Cache ##
        _IECacheFix()
        _IECacheClear($IECache)
        
    ;## Open an embedded IE window and load the video ##
        _IEErrorHandlerRegister ()
        $oIE = _IECreateEmbedded ()
        $GUIActiveX = GUICtrlCreateObj($oIE, 10, 40, 600, 360)
        _IENavigate ($oIE, $url)
    ;Sleep(30000)

    ;## Find FLV files in IECache ##
        _IECacheFindFLV($IECache)
    EndIf
EndFunc

Func _IECacheFix()
    $desktopinifile = @UserProfileDir & "\Local Settings\Temporary Internet Files\Content.IE5\desktop.ini"
    If FileExists($desktopinifile) Then
    FileDelete($desktopinifile)
    EndIf
EndFunc

Func _IECacheClear($IECache)
    $FileList = RecursiveFileSearch($IECache)
    If @error Then Return
    For $i = 1 To UBound($FileList) - 1
        FileDelete($FileList[$i])
    Next
EndFunc

Func _IECacheFindFLV($IECache)
    $FileList = RecursiveFileSearch($IECache)
    For $i = 1 To UBound($FileList) - 1
        If StringRegExp($FileList[$i],"\.(flv)$",0) Then
            GUICtrlSetData($status_text, "Found FLV File(s)..." & hg())
            $found_flv = 1
            MonitorFileSize($FileList[$i])
            ExitLoop
        Else
            GUICtrlSetData($status_text, "Searching For FLV File(s)..." & hg())
            $found_flv = 0
        EndIf
    Next
    Sleep(500)
    If ($found_flv <> 1) Then _IECacheFindFLV($IECache)
EndFunc

Func RecursiveFileSearch ($startDir, $depth = 0)

    If $depth = 0 Then Global $RFSstring = ""

    $search = FileFindFirstFile($startDir & "\*.*")
    If @error Then Return

;Search through all files and folders in directory
    While 1
        $next = FileFindNextFile($search)
        If @error Then ExitLoop
      
    ;If folder, recurse
        If StringInStr(FileGetAttrib($startDir & "\" & $next), "D") Then
            RecursiveFileSearch ($startDir & "\" & $next, $depth + 1)
        Else
        ;Append filename to master string
            $RFSstring &= $startDir & "\" & $next & "*"
        EndIf
    WEnd
    FileClose($search)

    If $depth = 0 Then Return StringSplit(StringTrimRight($RFSstring, 1), "*")
EndFunc
    
Func MonitorFileSize($file, $fsize = 0, $count = 0)
    $OldSize = $fsize
    Sleep(2000)
    If (FileGetSize($file) <> $OldSize) Then
        GUICtrlSetData($status_text, "Downloading FLV File(s)..." & hg())
        MonitorFileSize($file, FileGetSize($file), 0)
    ElseIf ($count >= 5) Then
            CopyFLVFile($file)
    Else
        GUICtrlSetData($status_text, "Downloading FLV File(s)..." & hg())
        MonitorFileSize($file, FileGetSize($file), $count + 1)
    EndIf
EndFunc

Func CopyFLVFile($src_file)
    $dest_file = @ScriptDir & "\temp.flv"
    If FileExists($dest_file) Then
        FileDelete($dest_file)
    EndIf
    GUICtrlSetData($status_text, "Copying FLV File..." & hg())
    FileCopy($src_file, $dest_file)
    While (FileGetSize($dest_file) <> FileGetSize($src_file))
        Sleep(500)
        GUICtrlSetData($status_text, "Copying FLV File..." & hg())
    Wend
    GUICtrlSetData($status_text, "Download Complete!")
EndFunc

Func hg()
;hourglass
    $tn = $tn + 1
    If $tn = 8 Then $tn = 0
    Return $twist[$tn]
EndFunc

;thanks L|M|TER
Func Get_Value($Host, $Video_ID)
    $Page = "/v/" & $Video_ID
    $Resp = _HTTPGetRespones($Host, $Page)

    Local $Location_URL = StringStripWS(_GetMidleString($Resp, 'Location:', '\n'), 3)
    Local $URL_Value = _GetMidleString($Location_URL, '&t=', '$')
    If $URL_Value = '' Then $URL_Value = _GetMidleString($Location_URL, '&t=', '\?')
    If $URL_Value = '' Then $URL_Value = _GetMidleString($Location_URL, '&t=', '&')
    If $URL_Value = '' Then $URL_Value = _GetMidleString($Location_URL, '&t=', '=')
    Return $URL_Value
EndFunc

;thanks L|M|TER
Func _GetMidleString($sString, $sStart, $sEnd, $iCase = -1, $iRetType = 0)
    Local $iCaseSence = ''
    If $iCase = -1 Then $iCaseSence = '(?i)'

    Local $aArray = StringRegExp($sString, '(?s)' & $iCaseSence & $sStart & '(.*?)' & $sEnd, 3)
    Local $IsArrayCheck = IsArray($aArray)

    If $IsArrayCheck And $iRetType = 1 Then Return $aArray
    If $IsArrayCheck And $iRetType = 2 Then
        Local $iUbound = UBound($aArray)
        If Not StringInStr($aArray[$iUbound - 1], "Next") Then Return $aArray[$iUbound - 1]
        If ($iUbound - 2) >= 0 Then Return $aArray[$iUbound - 2]
        Return $aArray[$iUbound - 1]
    EndIf

    If $IsArrayCheck Then Return $aArray[0]
    Return SetError(1, 0, "")
EndFunc

;thanks L|M|TER
Func _HTTPGetRespones($Host, $Page)
    $Socket = _HTTPConnect($Host)
    If @error Then Return SetError(1, 0, "")

    _HTTPHead($Host, $Page, $Socket)

    Local $Recv = "", $CurrentRecv
    While 1
        $CurrentRecv = TCPRecv($Socket, 16)
        If @error <> 0 Then ExitLoop
        If $CurrentRecv <> "" Then $Recv &= $CurrentRecv
    WEnd

    If StringInStr($Recv, "HTTP/1.1 410 Gone") Then Return SetError(2, 0, "")
    Return $Recv
EndFunc

;thanks L|M|TER
Func _HTTPConnect($Host)
    Local $Name_To_IP = TCPNameToIP($Host)
    $Socket = TCPConnect($Name_To_IP, 80)

    If $Socket = -1 Then
        TCPCloseSocket($Socket)
        Return SetError(1, 0, "")
    EndIf

    Return $Socket
EndFunc

;thanks L|M|TER
Func _HTTPHead($Host, $Page, $Socket)
    Local $Command = "HEAD " & $Page & " HTTP/1.1" & @CRLF
    $Command &= "Host: " & $Host & @CRLF
    $Command &= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0" & @CRLF
    $Command &= "Connection: close" & @CRLF & @CRLF

    Local $BytesSent = TCPSend($Socket, $Command)
    If $BytesSent = 0 Then Return SetError(2, @error, 0)
    Return $BytesSent
EndFunc

;Main Program Loop
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $download_btn Then
        _check_url(GUICtrlRead($url_input, 1))
    EndIf
WEnd

Also, I tried to give credit where it was needed. If I missed anyone, I'm sorry and I'll be more then happy to add credit where it's needed. :mellow:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...