Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Screen scape heise.de newsticker (german)

   1  
   2  #!/usr/bin/env python
   3  # -*- encoding: latin1 -*-
   4  
   5  import BeautifulSoup
   6  from PyRSS2Gen import RSSItem, Guid
   7  import ScrapeNFeed
   8  import urllib2
   9  import re
  10  
  11  debug = 0
  12  
  13  def fetch(url):
  14      response = urllib2.urlopen(urllib2.Request(url))
  15      return response.read(),response.info()
  16  
  17  class HeiFeed(ScrapeNFeed.ScrapedFeed):    
  18      def HTML2RSS(self, headers, body):
  19          items = []
  20          soup = BeautifulSoup.BeautifulSoup(body)
  21          for item in soup('a', {'href' : re.compile('^meldung.*')}):
  22              link = 'http://www.heise.de/newsticker/' + item['href']
  23              if not self.hasSeen(link):
  24                  title = item.contents[0].strip()
  25                  if debug:
  26                      print "title: " + title
  27                      print "link : " + link
  28                  response, headers = fetch(link)
  29                  s = BeautifulSoup.BeautifulSoup(response)
  30                  desc = s.fetch('div',{'class':'meldung_wrapper'})[0].prettify()
  31                  items.append(RSSItem(title=title, description=desc, link=link))
  32              self.addRSSItems(items)
  33  
  34  HeiFeed.load("heise.de newsticker", 'http://www.heise.de/newsticker/',
  35               "heise.de newsticker", 'heise_rss.xml', 'heise_rss.pickle',
  36               managingEditor = 'tsch')

Create an RSS feed from an SQL query

   1  
   2  #!/usr/bin/env python
   3  # -*- encoding: latin1 -*-
   4  
   5  import datetime,PyRSS2Gen,sqlobject
   6  from sqlobject.postgres import builder
   7  
   8  con = builder()(user = 'user', passwd = '', host = 'localhost', db='name')
   9  
  10  # set db encoding (maybe optional)
  11  con.queryOne("SET client_encoding TO 'latin1'; SELECT 1;")
  12  
  13  items = []
  14  for res in con.queryAll("""SELECT title,url,datum,description FROM table ORDER BY datum DESC LIMIT 30"""):
  15      items.append(
  16          PyRSS2Gen.RSSItem(
  17          title = res[0], link = res[1],
  18          description = """<h2>%s</h2>on %s<br/><p>%s</p>"""%(res[0],res[2],res[]3),
  19          guid = PyRSS2Gen.Guid(res[1]), pubDate = res[2]))
  20  
  21      # generate rss feed
  22  PyRSS2Gen.RSS2(
  23      title         = "sql2rss feed",
  24      link          = "http://localhost/die URL",
  25      description   = "The latest sql2rss news",
  26      lastBuildDate = datetime.datetime.now(),
  27      items         = items).write_xml(open("sql2rss.xml", "w"))
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS