Skip to main content

Ralsina.Me — Roberto Alsina's website

Posts about python (old posts, page 32)

Urssus: July 19th

I con­fess I cheat­ed and kept work­ing on it yes­ter­day af­ter the blog post. OTO­H, I will not touch it to­day ;-). Big func­tion­al­i­ty added, too.

  • Feed items (The things on the tree) now are up­­­dat­ed when the back­­­ground process checks them.

  • They are al­­so up­­­dat­ed when you read ar­ti­­cles from a feed.

  • The fil­ter thingie work­s, you type some tex­t, and on­­ly the ar­ti­­cles with that text are shown (see screen­shot).

  • Added a wid­get (not di­alog) for search­ing with­­in the page, fire­­fox-­­like (see screen­shot)

urssus4

The bad news is that the "next un­read ar­ti­cle" code and a few oth­ers is garbage. It's quite in­ef­fi­cient be­cause I tried to be cheap and not cre­ate a co­her­ent mod­el for feed­s.

How­ev­er it works and you will nev­er tell the dif­fer­ence un­less you have 2000 ar­ti­cles be­tween where you are and the next un­read (in which case the win­dow goes kin­da nuts for a cou­ple of sec­ond­s).

Still fun!

Urssus again

An­oth­er day, an­oth­er two hours of work on it.

What's new?

  • Some UI el­e­­ments with­­out code be­hind them (the "fil­ter ar­ti­­cle" thingie), and a feed prop­er­ties di­a­log.

  • A (IMVHO) bet­ter UI dis­­tri­bu­­tion than akre­­ga­­tor. Con­sid­er this screen­shot:

urssus3

The ar­ti­cle list and feed tree are the same size, but re­mov­ing the tabs and mov­ing the fil­ter in­to a tool­bar re­al­ly makes the ac­tu­al read­ing area quite larg­er.

  • Del­e­­gate all links from the read­­ing area to the desk­­top's web brows­er (this is not a web brows­er yet ;-)

  • Progress re­­port for web page load­­ing (more use­­ful once you can de­­clare a feed as "load link di­rec­t­­ly")

  • Fin­ished feed/ar­ti­­cle nav­i­­ga­­tion (nex­t/pre­vi­ous feed/ar­ti­­cle un­read­­/any), and fixed bugs in the part that was al­ready done.

  • Zoom in­­/out for the web view

  • Show/hide sta­­tus­bar

  • Sev­er­al bug fix­es

And yes, still fun!

Urssus improves

To­day's 2 hours:

  • An about di­a­log (not wired to the UI yet)

  • Icons on the feed tree (not fav­i­­con­s, though)

  • Han­­dle a few more feed quirks

  • A sta­­tus mes­sage queue, so sub­­pro­cess­es can post their progress in the sta­­tus bar.

  • UI for im­­por­­tOPML

  • Im­­ple­­men­t­ed "Next Ar­ti­­cle" and "Next Feed". This Mod­­el/View Qt thing is kin­­da painful (but pow­er­­ful!)

  • Use of Mako tem­­plates to dis­­­play the ar­ti­­cles pret­­ty and neat

  • Menu & Short­­­cuts fol­low­ing Akreg­­ga­­tor

  • Added sev­er­al fields to the Post and Feed mod­­els to make them more use­­ful (which means the DB changed, but that's to be ex­pec­t­ed at this stage). These in­­­clude things like "un­read" and "au­thor" and "link" ;-)

Still fun!

A programming challenge for myself

I worked on uRSSus for a cou­ple of hours again, and it's work­ing pret­ty nice­ly.

  • Re­al­­ly us­es the ORM

  • Mul­ti­pro­cess­ing for a non-block­­ing UI (python-pro­cess­ing is awe­­some)

  • Adapts to the quirks of some feeds (why on earth would some­one do a feed with­­out dates? It's AOL Fan­­House­­!)

I in­tend to keep work­ing like this for a cou­ple of week­s, and see how far I can get in fea­ture par­i­ty to akre­ga­tor.

No, I don't ex­pect to reach fea­ture par­i­ty, I on­ly want to strive for it. SInce I lack the fo­cu­sand/or en­er­gy for a mul­ti year com­mit­ment it re­quires to write the av­er­age free soft­ware, I want to see how far a sprint gets me.

urssus2

So far, it's fun.

The world lamest GUI newsreader... in 130 LOC

I start­ed this as an ex­per­i­ment to see how hard it was to build apps us­ing QT and Elixir. This is how it looks af­ter two hours of cod­ing:

urssus1

And here's the code: http://urssus.­google­code.­com

You will need:

  • PyQt 4.4 or lat­er

  • Mark Pil­­grim's feed­­pars­er

  • Python 2.5 (or when­ev­er el­e­­men­t­tree got in­­­clud­ed)

  • A OPML file

  • The Python SQLite bind­ings

  • Elixir (the declar­a­­tive lay­er over SQL Alche­my)

You can find the re­al code at

And then you can use these 131 LOC ;-)

# -*- coding: utf-8 -*-

# Mark Pilgrim's feed parser
import feedparser as fp

# DB Classes
from elixir import *

metadata.bind = "sqlite:///urssus.sqlite"
metadata.bind.echo = True

class Feed(Entity):
  htmlUrl     = Field(Text)
  xmlUrl      = Field(Text)
  title       = Field(Text)
  text        = Field(Text)
  description = Field(Text)
  children    = OneToMany('Feed')
  parent      = ManyToOne('Feed')
  posts       = OneToMany('Post')
  def __repr__(self):
    return self.text

  def update(self):
    d=fp.parse(self.xmlUrl)

class Post(Entity):
  feed        = ManyToOne('Feed')
  title       = Field(Text)
  post_id     = Field(Text)
  content     = Field(Text)

# This is just temporary
setup_all()
create_all()

# UI Classes
from PyQt4 import QtGui, QtCore
from Ui_main import Ui_MainWindow

class MainWindow(QtGui.QMainWindow):
  def __init__(self):
    QtGui.QMainWindow.__init__(self)

    # Set up the UI from designer
    self.ui=Ui_MainWindow()
    self.ui.setupUi(self)

    # Initialize the tree from the Feeds
    self.model=QtGui.QStandardItemModel()

    # Internal function
    def addSubTree(parent, node):
      nn=QtGui.QStandardItem(unicode(node))
      parent.appendRow(nn)
      nn.feed=node
      if not node.children:
        return
      else:
        for child in node.children:
          addSubTree(nn, child)

    roots=Feed.query.filter(Feed.parent==None)
    iroot=self.model.invisibleRootItem()
    iroot.feed=None
    for root in roots:
      addSubTree(iroot, root)

    self.ui.feeds.setModel(self.model)

    QtCore.QObject.connect(self.ui.feeds, QtCore.SIGNAL("clicked(QModelIndex)"), self.openFeed)
    QtCore.QObject.connect(self.ui.posts, QtCore.SIGNAL("clicked(QModelIndex)"), self.openPost)

  def openFeed(self, index):
    item=self.model.itemFromIndex(index)
    feed=item.feed

    if not feed.xmlUrl:
      return

    d=fp.parse(feed.xmlUrl)
    posts=[]
    for post in d['entries']:
      print post
      print '----------------------------------\n\n'
      if 'content' in post:
        posts.append(Post(feed=feed, title=post['title'], post_id=post['id'], content='<hr>'.join([ c.value for c in post['content']])))
      elif 'summary' in post:
        posts.append(Post(feed=feed, title=post['title'], post_id=post['id'], content=post['summary']))
      elif 'value' in post:
        posts.append(Post(feed=feed, title=post['title'], post_id=post['id'], content=post['value']))
    session.flush()

    self.ui.posts.__model=QtGui.QStandardItemModel()
    for post in posts:
      item=QtGui.QStandardItem(post.title)
      item.post=post
      self.ui.posts.__model.appendRow(item)
    self.ui.posts.setModel(self.ui.posts.__model)

  def openPost(self, index):
    item=self.ui.posts.__model.itemFromIndex(index)
    post=item.post
    self.ui.view.setHtml(post.content)

if __name__ == "__main__":
  import sys
  # For starters, lets import a OPML file into the DB so we have some data to work with
  from xml.etree import ElementTree
  tree = ElementTree.parse(sys.argv[1])
  current=None
  for outline in tree.findall("//outline"):
    xu=outline.get('xmlUrl')
    if xu:
      f=Feed(xmlUrl=outline.get('xmlUrl'),
             htmlUrl=outline.get('htmlUrl'),
             title=outline.get('title'),
             text=outline.get('text'),
             description=outline.get('description')
             )
      if current:
        current.children.append(f)
        f.parent=current
    else:
      current=Feed(text=outline.get('text'))
    session.flush()
  app=QtGui.QApplication(sys.argv)
  window=MainWindow()
  window.show()
  sys.exit(app.exec_())

Contents © 2000-2020 Roberto Alsina