Back-End

13 ago, 2007

Sistema de notícias alimentado por Feeds

Publicidade

Vamos desenvolver um sistema de notícias em ASP, alimentado por Feeds, e um gerenciamento completo com as configurações de exibição das notícias e temas selecionados.

Verifique o código abaixo, comentado:


<%
' Script ASP Para leitura de arquivos de RSS.
url = "http://www.euquerotrabalharnogoogle.com/feed/" 'Aqui vai o endereço do arquivo RSS, ou do arquivo "gerador"

' Criando Objeto XML
Set objXML = Server.CreateObject("msxml2.DOMDocument.3.0")
objXML.async = false
objXML.setProperty "ServerHTTPRequest", True

' Não validar o arquivo
objXML.validateOnParse = false 

' Retirar espaços
objXML.preserveWhiteSpace = false

blnLoaded = objXML.Load(url)


'Condição caso não tenha notícias no RSS
If Not blnLoaded Then

  Response.write "Nenhuma notícia na fonte!"
Else

 set objNodeList = objXML.getElementsByTagName("channel")


 For Each objNode In objNodeList
  For Each objNode2 In objNode.childNodes


  Select Case objNode2.nodeName
  
Case "title"
   'Define Propriedades do Titulo
 html = html &"<tr class='texto'><td><b>"
   html = html &objNode2.firstChild.nodevalue
   html = html &"</b></td></tr>"
  
Case "link"
'Define Propriedades do link  
   html = html &"<tr class='texto'><td><a target=_blank href="& objNode2.firstChild.nodevalue &">"
   html = html & objNode2.firstChild.nodevalue
   html = html &"</a></td></tr>"
  
Case "description" 
'Está desabilitado para exibir apenas os links
   html = html &"<tr class='texto'><td><i>"
   html = html & objNode2.firstChild.nodevalue
   html = html &"</i></td></tr>"
  End Select
  Next
 Next

 html = html &"<tr><td><hr></td></tr>"

 Set objNodeList = objXML.getElementsByTagName("item")
 
contador = 0
For Each objNode In objNodeList

'Definindo quantidade de notícias que será exibida
  if contador < 5 then
  contador = contador + 1

For Each objNode2 In objNode.childNodes

     Select Case objNode2.nodeName
      Case "title"
        strTitle = objNode2.firstChild.nodevalue
      Case "link"
       strURL = objNode2.firstChild.nodevalue
      Case "description"
        strDescription = objNode2.firstChild.nodevalue
      End Select

  Next
   
   html = html &"<tr class='texto'><td><li/><b><a target=_blank href="& strURL &">"& strTitle &"</a></b><br>"& strDescription &"</td></tr>"
  strTitle = ""
  strURL = ""
  strDescription = ""
End if 
 
 Next
 
'Variável onde gardamos o conteúdo do RSS
 html = "<table>"& html &"</table>"
 set objNodeList = Nothing
End if
%>
<html>
<head>
<style type="text/css">
<!--
.texto {
font-family: Verdana;
font-size: 11px;
color: #000000;
}
-->
</style>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
 <td>
<span class="texto">
  
<%
'Exibindo notícias do RSS escolhido
Response.write (html)
%>
</span>
 </td>
</tr>
</table>
</body>
</html>

Vejam o resultado final:

Exemplo das notícias

Vocês podem alterar o FEED para sua necessiade. No caso, eu utilizei o do nosso amigo que quer trabalhar no Google, mas fiquem a vontade. Simples né?

Qualquer dúvida entre em contato comigo deixando um comentário.

Abraços e bom proveito!