User:Eleanorg/RSS Manipulation: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "<source lang=python> import lxml.etree, urllib2, codecs # Precede by a quick demo of open-ing a file and parsing it?! # in fact ... use wget + file for testing! # Read from l...")
 
No edit summary
Line 1: Line 1:
What turns the Pirate Bay on?
Makes a pretty (?) SVG from users' requests for porn films that they are looking for, or have been unable to find, on TPB.
[[File:pbPornScreenshot.png]]
<source lang=python>
<source lang=python>
import lxml.etree, urllib2, codecs
import lxml.etree, urllib2, codecs, random
# Precede by a quick demo of open-ing a file and parsing it?!
# in fact ... use wget + file for testing!
   
   
# Read from live URL
# Read from live URL
f = urllib2.urlopen("http://london.indymedia.org/articles.rss")
page = urllib2.urlopen("https://forum.suprbay.org/syndication.php?fid=50")
   
   
# Read from local file
# Make document into an etree object
#import sys
doc = lxml.etree.parse(page)
#f = codecs.open(sys.argv[1], encoding="utf-8")


# Read in the XML file
print """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
doc = lxml.etree.parse(f)
<!-- Created with Inkscape (http://www.inkscape.org/) -->
print doc
# Again brief review of file system paths (absolute vs. relative)
# XPATH = super paths for documents, not filesystems!
NS = {
    'media': 'http://search.yahoo.com/mrss/',
    'dc': 'http://purl.org/dc/elements/1.1/',
    'cc': 'http://creativecommons.org/ns#',
    'atom': 'http://www.w3.org/2005/Atom',
}


# grab top 3 items
<svg
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:cc="http://creativecommons.org/ns#"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:svg="http://www.w3.org/2000/svg"
  xmlns="http://www.w3.org/2000/svg"
  xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
  xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
  width="2000"
  height="650"
  id="svg2"
  version="1.1"
  inkscape:version="0.47 r22583"
  sodipodi:docname="text1.svg">
  <defs
    id="defs4">
    <inkscape:perspective
      sodipodi:type="inkscape:persp3d"
      inkscape:vp_x="0 : 526.18109 : 1"
      inkscape:vp_y="0 : 1000 : 0"
      inkscape:vp_z="744.09448 : 526.18109 : 1"
      inkscape:persp3d-origin="372.04724 : 350.78739 : 1"
      id="perspective10" />
  </defs>
  <sodipodi:namedview
    id="base"
    pagecolor="#f2f071"
    bordercolor="#666666"
    borderopacity="1.0"
    inkscape:pageopacity="1"
    inkscape:pageshadow="2"
    inkscape:zoom="0.35"
    inkscape:cx="375"
    inkscape:cy="508.57143"
    inkscape:document-units="px"
    inkscape:current-layer="layer1"
    showgrid="false"
    inkscape:window-width="745"
    inkscape:window-height="889"
    inkscape:window-x="5"
    inkscape:window-y="47"
    inkscape:window-maximized="0" />
  <metadata
    id="metadata7">
    <rdf:RDF>
      <cc:Work
        rdf:about="">


print stories
        <dc:format>image/svg+xml</dc:format>
        <dc:type
          rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title></dc:title>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
    inkscape:label="Layer 1"
    inkscape:groupmode="layer"
    id="layer1">"""
for item in doc.xpath("//item"):
    title = item.xpath(".//title/text()")[0]
    print """<text
      xml:space="preserve"
      style="font-size:""" + str(random.randint(1,150)) + """px;font-style:normal;font-weight:normal;fill:#fb""" + str(random.randint(7040,7099)) + """;fill-opacity:1;stroke:none;font-family:Sans"
      x="142.85715"
      y="189.50504"
      id="text2824"><tspan
        sodipodi:role="line"
        id="tspan2826"
        x="
""" + str(random.randint(-100,60)) + """"
        y="
""" + str(random.randint(250,550)) + """">
""" + title + """</tspan>
</text>"""


# generate new text with dadadodo from these 3 items
print """
  </g>
</svg>"""


for story in range(0,3):
    print stories = doc.xpath(".//item/description/text()")[story]
</source>
</source>

Revision as of 19:51, 9 November 2011

What turns the Pirate Bay on? Makes a pretty (?) SVG from users' requests for porn films that they are looking for, or have been unable to find, on TPB.

PbPornScreenshot.png

import lxml.etree, urllib2, codecs, random
 

# Read from live URL
page = urllib2.urlopen("https://forum.suprbay.org/syndication.php?fid=50")
 
# Make document into an etree object
doc = lxml.etree.parse(page)

print """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="2000"
   height="650"
   id="svg2"
   version="1.1"
   inkscape:version="0.47 r22583"
   sodipodi:docname="text1.svg">
  <defs
     id="defs4">
    <inkscape:perspective
       sodipodi:type="inkscape:persp3d"
       inkscape:vp_x="0 : 526.18109 : 1"
       inkscape:vp_y="0 : 1000 : 0"
       inkscape:vp_z="744.09448 : 526.18109 : 1"
       inkscape:persp3d-origin="372.04724 : 350.78739 : 1"
       id="perspective10" />
  </defs>
  <sodipodi:namedview
     id="base"
     pagecolor="#f2f071"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:pageopacity="1"
     inkscape:pageshadow="2"
     inkscape:zoom="0.35"
     inkscape:cx="375"
     inkscape:cy="508.57143"
     inkscape:document-units="px"
     inkscape:current-layer="layer1"
     showgrid="false"
     inkscape:window-width="745"
     inkscape:window-height="889"
     inkscape:window-x="5"
     inkscape:window-y="47"
     inkscape:window-maximized="0" />
  <metadata
     id="metadata7">
    <rdf:RDF>
      <cc:Work
         rdf:about="">

        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title></dc:title>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1">"""
for item in doc.xpath("//item"):
    title = item.xpath(".//title/text()")[0]
    print """<text
       xml:space="preserve"
       style="font-size:""" + str(random.randint(1,150)) + """px;font-style:normal;font-weight:normal;fill:#fb""" + str(random.randint(7040,7099)) + """;fill-opacity:1;stroke:none;font-family:Sans" 
       x="142.85715"
       y="189.50504"
       id="text2824"><tspan
         sodipodi:role="line"
         id="tspan2826"
         x="
""" + str(random.randint(-100,60)) + """"
         y="
""" + str(random.randint(250,550)) + """">
""" + title + """</tspan> 
</text>"""

print """
  </g>
</svg>"""