Creare una sitemaps in ASP

Oggi vediamo come generare una sitemap  usando ASP.

Ecco il codice ASP per generare una sitemap per un webserver. La pagina si chiama sitemap_gen.asp :

<%' sitemap_gen.asp' A simple script to automatically produce sitemaps for a webserver, in the Google Sitemap Protocol (GSP)' by Francesco Passantino' www.iteam5.net/francesco/sitemap_gen' v0.1  04.06.05' v0.2  05.06.05 Listing a directory tree recursively improvement' v0.3b 09.06.05 File exclusion by Calvin Dunkley' v0.4  17.06.05 iso8601dates http://www.tumanov.com/projects/scriptlets/iso8601dates.asp' v0.4b 18.06.05 vdrir bug fixed' v0.4c 13.07.05 phisical dir patch for some windows systems'' BSD 2.0 license,' http://www.opensource.org/licenses/bsd-license.php'modificare in base al proprio dominiosession("server")="http://www.yourdomain.net"vDir = "/"phisicalDir="C:Inetpubwwwroot"'Info qui http://www.time.gov/utcOffset=1set objfso = CreateObject("Scripting.FileSystemObject")root = Server.MapPath(vDir)response.ContentType = "text/xml"response.write ""response.write ""response.write ""Set objFolder = objFSO.GetFolder(root)'response.write getfilelink(objFolder.Path,objFolder.dateLastModified)Set colFiles = objFolder.FilesFor Each objFile In colFilesresponse.write getfilelink(objFile.Path,objfile.dateLastModified)NextShowSubFolders(objFolder)response.write ""set fso = nothingSub ShowSubFolders(objFolder)Set colFolders = objFolder.SubFoldersFor Each objSubFolder In colFoldersif folderpermission(objSubFolder.Path) thenresponse.write getfilelink(objSubFolder.Path,objSubFolder.dateLastModified)Set colFiles = objSubFolder.FilesFor Each objFile In colFilesresponse.write getfilelink(objFile.Path,objFile.dateLastModified)NextShowSubFolders(objSubFolder)end ifNextEnd SubFunction getfilelink(file,data)file=replace(file,phisicalDir,"")file=replace(file,"","/")If FileExtensionIsBad(file) then Exit FunctionIf FileNameIsBad(file) then Exit Functionfilelmdate=iso8601date(data,utcOffset)getfilelink = ""&server.htmlencode(session("server")&file)&""&filelmdate&"1.0"session("URLS")=session("URLS")+1Response.FlushEnd FunctionFunction Folderpermission(pathName)'modificare qui per escludere un percorsoPathExclusion=Array("emp","\_vti_cnf","\_vti_pvt","\_vti_log","cgi-bin")Folderpermission =Truefor each PathExcluded in PathExclusionif instr(ucase(pathName),ucase(PathExcluded))>0 thenFolderpermission = Falseexit forend ifnextEnd FunctionFunction FileExtensionIsBad(sFileName)Dim sFileExtension, bFileExtensionIsValid, sFileExt'http://www.googleguide.com/file_type.htmlExtensions = Array("png","gif","jpg","zip","pdf","ps","html","htm","asp","wk1","wk2","wk3","wk4","wk5","wki","wks","wku","lwp","mw","xls","ppt","doc","wks","wps","wdb","wri","rtf","ans","txt")if len(trim(sFileName)) = 0 thenFileExtensionIsBad = trueExit Functionend ifsFileExtension = right(sFileName, len(sFileName) - instrrev(sFileName, "."))bFileExtensionIsValid = false'assume extension is badfor each sFileExt in extensionsif ucase(sFileExt) = ucase(sFileExtension) thenbFileExtensionIsValid = Trueexit forend ifnextFileExtensionIsBad = not bFileExtensionIsValidEnd FunctionFunction FileNameIsBad(sFileName)  if len(trim(sFileName)) = 0 then    FileNameIsBad = true    Exit Function  end if  Filename = Array("/alive.asp","/404.asp","/500.asp","/sitemap_gen.asp")  FileNameNumber = ubound(Filename)  for i=0 to FileNameNumber    if Filename(i) = sFileName thenFileNameIsBad = True    end if  nextEnd FunctionFunction iso8601date(dLocal,utcOffset)Dim d' Converte l' ora locale in UTCd = DateAdd("H",-1 * utcOffset,dLocal)' crea la dataiso8601date = Year(d) & "-" & Right("0" & Month(d),2) & "-" & Right("0" & Day(d),2) & "T" & _Right("0" & Hour(d),2) & ":" & Right("0" & Minute(d),2) & ":" & Right("0" & Second(d),2) & "Z"End Function%>

Di seguito, invece, il codice di sitemap_gen_db.asp , per generare una sitemap dinamica, auto-aggiornabile da database:

' by Francesco Passantino' www.iteam5.net/francesco/sitemap_gen' v0.1b released 5 june 2005' v0.2  released 17 june 2005 iso8601dates http://www.tumanov.com/projects/scriptlets/iso8601dates.asp' v0.2b released 28 july 2005 id_page=Server.URLEncode(rs("id")) to put words in id, thanks to Mike Kellogg'' BSD 2.0 license,' http://www.opensource.org/licenses/bsd-license.phpMAXURLS_PER_SITEMAP = 50000'modificare in base al proprio URLbaseurl="http://www.yoursite.com/default.asp?page="xDb_Conn_Str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("dbyourdb.mdb") & ";"strsql = "SELECT * FROM yourtable"'info su http://www.time.gov/ utcOffset=1response.ContentType = "text/xml"response.write ""response.write ""response.write ""Set conn = Server.CreateObject("ADODB.Connection")conn.Open xDb_Conn_StrSet rs = Server.CreateObject("ADODB.Recordset")rs.Open strsql, connDo while not rs.eofif URLS1.0 then priority="1.0"response.write ""&server.htmlencode(baseurl&id_page)&""&filedate&""&priority&""URLS=URLS+1Response.Flushrs.movenextend ifLoopresponse.write ""rs.CloseSet rs = Nothingconn.CloseSet conn = NothingFunction iso8601date(dLocal,utcOffset)Dim d' converte l' ora locale in UTCd = DateAdd("H",-1 * utcOffset,dLocal)' ccrea la dataiso8601date = Year(d) & "-" & Right("0" & Month(d),2) & "-" & Right("0" & Day(d),2) & "T" & _Right("0" & Hour(d),2) & ":" & Right("0" & Minute(d),2) & ":" & Right("0" & Second(d),2) & "Z"End Function%>

Infine, lo script sitemap_gen_spider, per creare sitemap usando l' MSXML spider:

<%' sitemap_gen_spider.asp' A simple script to automatically produce sitemaps for a webserver, in the Google Sitemap Protocol (GSP)' by Francesco Passantino' www.iteam5.net/francesco/sitemap_gen' v0.1 released 9 june 2005' v0.2 released 17 june 2005 iso8601dates http://www.tumanov.com/projects/scriptlets/iso8601dates.asp'' BSD 2.0 license,' http://www.opensource.org/licenses/bsd-license.php'configurazione dello scriptUrl="http://www.yoursite.com/"FinalDepth=3LimitUrl=100'lasciare sitemapDate vuoto se si vuole che appaia sitemapDate=nowsitemapDate=""'da 0.1 a 1.0sitemapPriority="0.7"'valori possibili: always, hourly, daily, weekly, monthly, yearly, neversitemapChangefreq="monthly"'info su http://www.time.gov/utcOffset=1Dim objRegExp,objUrlArchive,strHTML,objMatchServer.ScriptTimeout=300set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")Set objUrlArchive=Server.CreateObject("Scripting.Dictionary")Set objRegExp = New RegExpobjRegExp.IgnoreCase = TrueobjRegExp.Global = True'modificabili a piacimentoobjRegExp.Pattern = "href=(.*?)[s|>]"'per rimuovere elementi dagli url htmlRemoveText=array("<",">","a href=",chr(34),"'","href=")'per escludere elementi dagli URLExcludeUrl=array("mailto:","javascript:",".css",".ico")'si si vuole che risulti sitemapDate=nowif sitemapDate="" then filelmdate=now()sitemapDate=iso8601date(filelmdate,utcOffset)crawl url,0For Depth=0 to FinalDeptharrUrl=objUrlArchive.KeysarrDepth=objUrlArchive.ItemsFor LoopUrl= 0 to ubound(arrurl)-1'debugging'response.write ""crawl url&"/"&arrUrl(LoopUrl),Depth'se si vuole limitare il numero amssimo di URL'if objUrlArchive.Count-1>LimitUrl then exit forNexterase arrUrlerase arrDepthNext' crea l' XMLarrUrl=objUrlArchive.KeysarrDepth=objUrlArchive.Itemsresponse.ContentType = "text/xml"response.write ""response.write ""response.write ""response.write ""For LoopUrl=0 to ubound(arrurl)-1response.write ""response.write ""&server.htmlencode(url&arrUrl(LoopUrl))&""response.write ""&sitemapDate&""response.write ""&sitemapPriority&""response.write ""&sitemapChangefreq&""response.write ""Nextresponse.write ""erase arrUrlerase arrDepthobjUrlArchive.RemoveAll()set xmlhttp = nothingSub crawl(url,depth)xmlhttp.open "GET", url, falsexmlhttp.send ""strHTML = xmlhttp.responseTextFor Each objMatch in objRegExp.Execute(strHTML)for i=0 to ubound(excludeUrl)if instr(objmatch,excludeUrl(i))>0 then objmatch=""nextif objmatch<>"" thenfor i=0 to ubound(RemoveText)objMatch=replace(lcase(objMatch),lcase(RemoveText(i)),"")next'in alcune occasioni è meglio sostituire con if left(objMatch,len(url))=Url thenif instr(objMatch,"http://")=0 and objmatch<>"" thenif objUrlArchive.Exists(objMatch)=false thenobjUrlArchive.Add objMatch,depth'debugging'response.write objmatch&"
"'response.flushend ifend ifend ifNextEnd SubFunction iso8601date(dLocal,utcOffset)Dim d' converte l' ora locale in UTCd = DateAdd("H",-1 * utcOffset,dLocal)' compone da dataiso8601date = Year(d) & "-" & Right("0" & Month(d),2) & "-" & Right("0" & Day(d),2) & "T" & _Right("0" & Hour(d),2) & ":" & Right("0" & Minute(d),2) & ":" & Right("0" & Second(d),2) & "Z"End Function



Ti potrebbe interessare anche

commenta la notizia