andrewb icon

Pyrity.py

andrewb | PRO | 08/29/15 02:20:49 AM UTC | 0 ⭐ | 896 👁️ | Never ⏰ | []
Python |

1.47 KB

|

None

|

0 👍

/

0 👎

import urllib
import xml.etree.ElementTree as ET
 
class Pyrity:
    def __init__(self, address):
        self.pics = []
        self.address = address
        if address.endswith('index.xml'):
            self.base_address = address[:-len("index.xml")]
 
    def downloadXml(self):
        f = urllib.urlopen(self.address)
        self.xml = f.read()
 
    def getAllDir(self):
        if len(self.xml) == 0:
            return False
        self.dirs = {}
        xml = ET.fromstring(self.xml)
        for dir in xml.iter('dir'):
            d = dir.attrib
            self.dirs.update({d['type']:d['path']})
 
    def getAllPic(self):
        if len(self.xml) == 0 or len(self.dirs) == 0:
            return False
        xml = ET.fromstring(self.xml)
        for pic in xml.iter('image'):
            image = {}
            for key in self.dirs:
                image.update({key:pic.find(key).text})
            self.pics.append(image)
 
    def makeUrl(self, file, size):
        return self.base_address + self.dirs[size] + '/' + file
 
    def getOnePic(self, index, size="small"):
        if index < 0 or index > (len(self.pics) - 1):
            return False
        return self.makeUrl(self.pics[index][size], size)
    
    def getOneGroup(self, index):
        if index < 0 or index > (len(self.pics) - 1):
            return False
        pics = {}
        for k in self.dirs:
            pics.update({k:self.getOnePic(index, k)})
        return pics

Comments