Sign in to follow this  
Followers 0
ripdad

Podcast Downloader

1 post in this topic

#1 ·  Posted (edited)

Recently, I downloaded over 2000 mp3's (podcast).

There was no way to do this with an aggregator because of the way the podcasts were posted on the website.

To make a long story short .. I wrote a script to deal with it.

Anyways, I am releasing 2 of the functions I used in that script as an example to download podcast ... or

possibly something else with a few modifications made to it.

; Podcast Downloader
; Released: April 08, 2011 by ripdad
; Example: Yes
;
#include <array.au3>
;
Local $rtn, $LinksArray, $sFolder = @ScriptDir & '\MyPodcast'
If Not FileExists($sFolder) Then DirCreate($sFolder)

$LinksArray = GetPodcastLinks('http://rss.cnn.com/services/podcasting/newscast/rss.xml', '<link>', '</link>', 0)
If @error Then
    MsgBox(16, '', $LinksArray); <-- NOTE: var $LinksArray is a string if an error was returned
    Exit
EndIf

_ArrayDisplay($LinksArray); Visually check if GetPodcastLinks() returned full links properly

If MsgBox(8228, 'Podcast Downloader', 'Start Download?') = 7 Then Exit; ability to escape

$rtn = DownloadPodcast($LinksArray, $sFolder)
If @error Then
    MsgBox(16, '', $rtn)
    Exit
EndIf
MsgBox(64, '', $rtn)
Exit

Func GetPodcastLinks($podURL, $tag1 = 'url="', $tag2 = '"', $addURL = 0)
    If $addURL Then
        If StringRight($podURL, 1) <> '/' Then $podURL &= $podURL & '/'
    EndIf
    Local $string = '', $DL_Page = InetRead($podURL, 1)
    If @error < 0 Then Return SetError(-1, 0, 'Connection Error')
    Local $PodLinks = StringRegExp(BinaryToString($DL_Page), '(?i)(?s)' & $tag1 & '(.*?)' & $tag2, 3)
    If Not IsArray($PodLinks) Then Return SetError(-2, 0, 'Nothing To View')
    For $i = 0 To UBound($PodLinks) - 1
        If StringRight($PodLinks[$i], 4) <> '.mp3' Then ContinueLoop
        If $addUrl Then
            $string &= $podURL & $PodLinks[$i] & '|'
        Else
            $string &= $PodLinks[$i] & '|'
        EndIf
    Next
    $PodLinks = StringSplit(StringTrimRight($string, 1), '|')
    _ArraySort($PodLinks, 0, 1)
    Return SetError(0, 0, $PodLinks)
EndFunc

Func DownloadPodcast($aLinks, $sPath)
    If Not IsArray($aLinks) Then Return SetError(-1, 0, 'Invalid Array')
    If StringRight($sPath, 1) <> '\' Then $sPath &= '\'
    If Not FileExists($sPath) Then
        If Not DirCreate($sPath) Then Return SetError(-2, 0, 'Error Creating Path')
        Sleep(2000)
    EndIf
    Local $dLoad, $sFile, $nSize, $BytesRead, $bdv
    Local $mps, $d1, $d2, $nCount, $cnt = $aLinks[0]
    For $i = 1 To $aLinks[0]
        $cnt -= 1
        If StringRight($aLinks[$i], 4) <> '.mp3' Then ContinueLoop
        $sFile = StringTrimLeft($aLinks[$i], StringInStr($aLinks[$i], '/', 0, -1))
        $nSize = InetGetSize($aLinks[$i])
        If FileExists($sPath & $sFile) Then
            If FileGetSize($sPath & $sFile) = $nSize Then ContinueLoop
            FileDelete($sPath & $sFile)
        EndIf
        $nCount = 0
        $dLoad = InetGet($aLinks[$i], $sPath & $sFile, 1, 1)
        Do
            Sleep(1000)
            $nCount += 1
            $BytesRead = InetGetInfo($dLoad, 0)
            $bdv = $BytesRead / 1024 / 1024
            $mps = StringLeft($bdv / ($nCount / 1000), 5)
            $d1 = 'Current File Progress: ' & Int($BytesRead / $nSize * 100) & '%' & @CRLF
            $d2 = "Speed (kb's/sec): " & $mps
            ToolTip($sPath & $sFile & @CRLF & $d1 & $d2, 0, 0, 'Remaining Files to Download: ' & $cnt, 1)
        Until InetGetInfo($dLoad, 2)
        If InetGetInfo($dLoad, 4) Then
            InetClose($dLoad)
            Return SetError(-3, 0, 'Connection Error')
        EndIf
        InetClose($dLoad)
    Next
    ToolTip('')
    Return SetError(0, 0, 'Finished')
EndFunc
Edited by ripdad
1 person likes this

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
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
Sign in to follow this  
Followers 0