F#: Breaking out of a loop -
i new programming , f# first language.
i have list of urls that, when first accessed, either returned http error 404 or experienced gateway timeout. these urls, try accessing them 3 times. @ end of these 3 attempts, if webexception error still thrown, assume url doesn't exist, , add text file containing of invalid urls.
here code:
let tryaccessingagain (url: string) (numattempts: int) = async { attempt = 1 numattempts try let! html = fetchhtmlasync url let name = getnamefrompage html let id = getidfromurl url let newtextfile = file.create(htmldirectory + "\\" + id.tostring("00000") + " " + name.trimend([|' '|]) + ".html") use file = new streamwriter(newtextfile) file.write(html) file.close() :? system.net.webexception -> file.appendalltext("g:\user\invalid urls.txt", url + "\n") }
i have tested fetchhtmlasync, getnamefrompage , getidfromurl in f# interactive. of them work fine.
if succeed in downloading html contents of url without using 3 attempts, want break out of for-loop immediately. question is: how may so?
use recursion instead of loop:
let rec tryaccessingagain (url: string) (numattempts: int) = async { if numattempts > 0 try let! html = fetchhtmlasync url let name = getnamefrompage html let id = getidfromurl url let newtextfile = file.create(htmldirectory + "\\" + id.tostring("00000") + " " + name.trimend([|' '|]) + ".html") use file = new streamwriter(newtextfile) file.write(html) file.close() | :? system.net.webexception -> file.appendalltext("g:\user\invalid urls.txt", url + "\n") return! tryaccessingagain url (numattempts-1) }
please note not test , there might syntax errors - sorry if
as @ - might want rewrite logging of invalid url this:
let rec tryaccessingagain (url: string) (numattempts: int) = async { if numattempts <= 0 file.appendalltext("g:\user\invalid urls.txt", url + "\n") else try let! html = fetchhtmlasync url let name = getnamefrompage html let id = getidfromurl url let newtextfile = file.create(htmldirectory + "\\" + id.tostring("00000") + " " + name.trimend([|' '|]) + ".html") use file = new streamwriter(newtextfile) file.write(html) file.close() | :? system.net.webexception -> return! tryaccessingagain url (numattempts-1) }
this way logged once attempts made
Comments
Post a Comment