Jump to content
Sign in to follow this  
cuShane

Need help making script efficient

Recommended Posts

cuShane

I have put together a script that worked when I tested it on a small input xml file, but when I tried it on a large file it ran all night and is actually still running :)

I would love any help that you guys could give me to make this script more efficient.

Here is a small sample of what the xml file contents could look like (I have also attached my full file for your reference):

CODE
<?xml version="1.0" encoding="Windows-1252"?>

<!--Created by mee2XML on 2/1/2009 1:54:54 AM-->

<Music>

<Item>

<MeedioID>1</MeedioID>

<MeedioName>Here Without You</MeedioName>

<MeedioLocation>C:\Documents and Settings\HTPC User\My Documents\Music\iTunes\Music\3 Doors Down\Away From the Sun\3 Doors Down - Here Without You.mp3</MeedioLocation>

<MeedioImage>C:\Documents and Settings\HTPC User\My Documents\Music\iTunes\Music\3 Doors Down\Away From the Sun\folder.jpg</MeedioImage>

<album>Away From the Sun</album>

<artist>3 Doors Down</artist>

<LastUpdate>633690499272007053</LastUpdate>

<SortAlbum>Away From the Sun</SortAlbum>

<SortArtist>3 Doors Down</SortArtist>

<track>6</track>

</Item>

<Item>

<MeedioID>1</MeedioID>

<MeedioName>Some song</MeedioName>

<MeedioLocation>C:\Documents and Settings\HTPC User\My Documents\Music\iTunes\Music\Some Band\Some Album\Some Band - Some Song.mp3</MeedioLocation>

<MeedioImage>C:\Documents and Settings\HTPC User\My Documents\Music\iTunes\Music\Some Band\Some Album\folder.jpg</MeedioImage>

<album>Some Album</album>

<artist>Some Band</artist>

<Fanart>C:\meedio\meedio data\fanart\music\filename.jpg</Fanart>

</Item>

</Music>

Now here is my script:

CODE
#cs

This code will:

sect1) Parse the file c:\test.xml

sect1.1) Process the file one entry at a time where an entry is defined as

starting with <item> and ending with </item>

sect1.2) If an entry does not contain a <Fanart> tag, add the entry's artist

(contained between the artist tags) into $artistarray

sect2) reduce $artistarray to unique entries

sect3) Open Internet Explorer

sect4) Get the URL of fanart (a.k.a. backdrops) from HTBackdrops.com for all of the identified artists

sect4.1) Navigate to htbackdrops.com and use the search form to search for current artist

sect4.2) Parse the resulting html page's source code for results as identified by the word "views"

sect4.3) If any results were found identify the one with the most views

sect4.4) parse the line of the desired result to locate the corresponding image's URL

sect4.5) update the xml file to add fanart url for all instances of this artist using the <OnlineURL> tag

sect5) Close Internt Explorer

#ce

#include <file.au3>

#include <IE.au3>

#Include <Array.au3>

Dim $artistsarray[1] ;array used to hold all the artist names parsed from the xml that do not currently have fanart

Dim $file = "C:\test.xml" ;file that is to be read and updated

;______________________________sect1________________________________

;Parse the file c:\test.xml

$openedfile = FileOpen($file, 0) ;Open the file (using the file path specified above)

$linesttl = _FileCountLines($file) ;count the number of lines in the file

For $i = 1 To $linesttl ;from line 1 of the file to the last line...

$line = FileReadLine($file, $i)

;______________________________sect1.1________________________________

;Process the file one entry at a time where an entry is defined as starting with <item> and ending with </item

;if the current line contains the <Item> tag then locate the line that contains the matching </Item> tag

if StringInStr($line,"<Item>") Then

For $stopline = $i + 1 To $linesttl

$line = FileReadLine($file, $stopline)

if StringInStr($line,"</Item>") Then

ExitLoop

EndIf

Next

;concatenate all lines for this item and save them to the string $section

$section = ""

For $j = $i to $stopline

$section = $section & FileReadLine($file, $j)

Next

;______________________________sect1.2________________________________

;If an entry does not contain a <Fanart> tag, add the entry's artist contained between the artist tags) into $artistarray

;check $section to see if Fanart is not present

if StringInStr($section,"<Fanart>") = 0 Then

$startA = StringInStr($section,"<artist>") ;position of <artist> tag

$stopA = StringInStr($section,"</artist>") ;position of </artist> tag

$artist = StringMid($section, $startA + 8, $stopA - $startA - 8)

_ArrayAdd($artistsarray, $artist) ;add artist to $artistsarray

EndIf

EndIf

Next

;______________________________sect2________________________________

;reduce $artistarray to unique entries

$artistsarray = _ArrayUnique($artistsarray)

;______________________________sect3________________________________

;Open Internet Explorer

$o_IE = _IECreate ()

;______________________________sect4________________________________

;Get the URL of fanart (a.k.a. backdrops) from HTBackdrops.com for all of the identified artists

;for each artist in array

For $k = 1 to UBound($artistsarray)-1

;note, the two arrays below are used in tandem as if they were one two deminisional array.

;I implemented them as separate arrays because it was easier for me to code.

Dim $viewsarray[1] ;array used to hold all results when searching for number of views

Dim $linearray[1] ;array used to hold the line number in the html source where the view results were located

;get this artist

$artist = $artistsarray[$k]

;______________________________sect4.1________________________________

;Navigate to htbackdrops.com and use the search form to search for current artist

;go to htbackdrops.com

_IENavigate ($o_IE, "http://htbackdrops.com/")

;get pointers to the search form and search field and submit button

$o_form = _IEFormGetObjByName($o_IE, "custom_search")

$o_search = _IEFormElementGetObjByName($o_form, "search")

$oSubmit = _IEGetObjByName($o_form, "Search")

;Set search field value and submit form

_IEFormElementSetValue($o_search, $artist)

_IEAction ($oSubmit, "focus")

ControlSend($o_IE, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "{Enter}")

While $o_IE.busy

Sleep(100)

WEnd

Sleep(2000)

;______________________________sect4.2________________________________

;Parse the resulting html page's source code for results as identified by the word "views"

$HTMLSource = _IEBodyReadHTML($o_IE)

$output = StringSplit($HTMLSource, @CRLF)

$hit = 0 ;used to denote if any results located (0 = false, 1 = true)

;gather results by searching for the word "views"

For $i = 1 To $output[0]

If StringInStr($output[$i], "views") Then

$hit = 1

$resultPos = StringInStr($output[$i], "views") ;get position of the word "views"

;get the number of views by isolating the number before the word "views"

$count = StringMid ( $output[$i], $resultPos - 10, 10 )

$count = StringRegExpReplace ( $count, "[^0-9]", "")

;add resulting number of views and the corresponding line in the html source to the appropriate arrays

_ArrayAdd($viewsarray, $count)

_ArrayAdd($linearray, $i)

EndIf

Next

;______________________________sect4.3________________________________

;If any results were found identify the one with the most views

if $hit Then

$mostviews = _ArrayMaxIndex($viewsarray) ;find the result with the most views

$i = $linearray[$mostviews] ;set $i to be the line number of this result

;______________________________sect4.4________________________________

;parse the line of the desired result to locate the corresponding image's URL

$result1 = StringInStr($output[$i], "Filename") ;position of the word "Filename"

$result2 = StringInStr($output[$i], ".jpg",0,1,$result1) ;position of the end of the filename as identified by the .jpg extension

$start = $result1 + 9

$count = $result2 - $result1 -5

$imgname = StringMid ( $output[$i],$start ,$count) ;parse the filename

$imgURL = "http://htbackdrops.com/albums/userpics/" & $imgname ;set $imgURL to the image's URL

;______________________________sect4.5________________________________

;update the xml file to add fanart url for all instances of this artist using the <OnlineURL> tag

_ReplaceStringInFile("C:\test.xml", "<artist>" & $artist & "</artist>", "<artist>" & $artist & "</artist>" & @CRLF & "<OnlineURL>" & $imgURL & "</OnlineURL>")

EndIf

Next

;______________________________sect5________________________________

;Close Internt Explorer

_IEQuit($o_IE )

Any tips will be greatly appreciated!!!

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.