User:Dave Young/Prototyping 5.1

From XPUB & Lens-Based wiki

Remixing an RSS Feed

CNN v FOX

For this exercise I have remixed the "Top Headlines" rss feeds of two news agencies with opposing agendas - FOX and CNN. In the lead up to the presidential elections in the US next year, I noticed increasingly biased reporting from both agencies (both were also accused of strategic editorialising in the lead up to the last election in 2008). With the recent controversies surrounding Rick Perry, Newt Gingrich, the Occupy Movement, Obama stating that the US army would pull out of Iraq etc, I am curious about how each agency will spin the headlines in order to encourage a particular public viewpoint.

The piece is intended to remain as objective as possible and let the website visitor to interpret the headlines as they wish. CNN appears in red and Fox in dark blue (colours taken from their logos), and only the headlines + their date of publishing are displayed on the rss feed. By clicking the headline, you are linked to the story.

link!

Code

Technically the piece is quite simple, but was a good exercise in learning about lxml in python :) It consists of two components: a python script that reads the rss feeds triggered by a cronjob every half hour, and dumps the results into a .txt file. The second component is a cgi-script that loads the data from the .txt file and prints it to the webpage.

#!/usr/bin/python
# Get RSS xml data from feeds and write to feed.txt
# For RSSMASHUP -> PZ.PROTOTYPING
# h6

import lxml.etree

file = open('feed.txt', 'w')

cnn = "http://rss.cnn.com/rss/edition.rss"
fox = "http://feeds.foxnews.com/foxnews/latest?format=xml"

def getFeed(feed):
        doc = lxml.etree.parse(feed)
        entries = []
        index = 0
        maxResults = 10
        # check how many items are in the rss
        # print len(doc.xpath("//item")), "items"

        #return headline + link
        for item in doc.xpath("//item"):
                index+=1
                if(index<=maxResults):
                        title = item.xpath(".//title/text()")[0]
                        link = item.xpath(".//link/text()")[0]
                        date = item.xpath(".//pubDate/text()")[0]
                        entries = title+'\t'+link+'\t'+date+'\n'
                        file.write(entries)

getFeed(cnn)
getFeed(fox)
file.close()
#!/usr/bin/python
# Reads feed data from feed.txt
# feed.txt = cnn[[title, link, date],...],fox[[...]...]
# h6

file = open('feed.txt','r')
file = file.read()
xline = []

entry = []

cnn = ""
fox = ""
index=0

# split feed.txt into cnn and fox
for line in file.splitlines():
        line = filter(None, line)
        element = line.split('\t')
        title = element[0]
        link = '"'+element[1]+'"'
        date = element[2]
        if(index<10):
                cnn += "<a class='cnn' href="+element[1]+">"+element[0]+"</a>"+"<div class='info'>"+element[2]+"</div>"
        elif(index<20):
                fox += "<a class='fox' href="+element[1]+">"+element[0]+"</a>"+"<div class='info'>"+element[2]+"</div>"
        index+=1

print """Content-type: text/html\n\n\n"""

print """<html>
<head>
<style type="text/css">
body {{ font-family: Sans; font-size: 20px; margin: 0 auto; width: 1024px;}}
h1.cnn{{ font-size: 48px; font-family: Arial,Helvetica,Utkal,sans-serif; color: #D32026; padding: 0px;}}
h1.fox{{ font-size: 48px; font-family: Arial, sans-serif; color: #183A52; padding: 0px;}}
#left{{ float: left; width: 490px; padding: 0px; margin: 10px 0px;}}
#right{{ float: right; width: 490px; padding: 10px;}}
a{{ padding-left: 5px; padding-right: 5px; text-decoration: none;}}
a.cnn:link{{ background: #D32026; color: #FFF;}}
a.cnn:visited{{ color: #FFF;}}
a.cnn:hover{{ background: #000; color: #FFF;}}
a.fox:link{{ background: #183A52; color: #FFF;}}
a.fox:visited{{ color: #FFF;}}
.info{{ font-size: 10px; font-style: italic; padding-bottom: 20px; padding-top: 5px;}}
</style>
</head>
<body>

<div id="left">
<h1 class="cnn">CNN</h1>
{0}
</div>

<div id="right">
<h1 class="fox">FOX</h1>
{1}
</div>

</body>
</html>
""".format(cnn, fox)