Advertisement
andrewb

Pyrity.py

Aug 28th, 2015
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. import urllib
  2. import xml.etree.ElementTree as ET
  3.  
  4. class Pyrity:
  5.     def __init__(self, address):
  6.         self.pics = []
  7.         self.address = address
  8.         if address.endswith('index.xml'):
  9.             self.base_address = address[:-len("index.xml")]
  10.  
  11.     def downloadXml(self):
  12.         f = urllib.urlopen(self.address)
  13.         self.xml = f.read()
  14.  
  15.     def getAllDir(self):
  16.         if len(self.xml) == 0:
  17.             return False
  18.         self.dirs = {}
  19.         xml = ET.fromstring(self.xml)
  20.         for dir in xml.iter('dir'):
  21.             d = dir.attrib
  22.             self.dirs.update({d['type']:d['path']})
  23.  
  24.     def getAllPic(self):
  25.         if len(self.xml) == 0 or len(self.dirs) == 0:
  26.             return False
  27.         xml = ET.fromstring(self.xml)
  28.         for pic in xml.iter('image'):
  29.             image = {}
  30.             for key in self.dirs:
  31.                 image.update({key:pic.find(key).text})
  32.             self.pics.append(image)
  33.  
  34.     def makeUrl(self, file, size):
  35.         return self.base_address + self.dirs[size] + '/' + file
  36.  
  37.     def getOnePic(self, index, size="small"):
  38.         if index < 0 or index > (len(self.pics) - 1):
  39.             return False
  40.         return self.makeUrl(self.pics[index][size], size)
  41.    
  42.     def getOneGroup(self, index):
  43.         if index < 0 or index > (len(self.pics) - 1):
  44.             return False
  45.         pics = {}
  46.         for k in self.dirs:
  47.             pics.update({k:self.getOnePic(index, k)})
  48.         return pics
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement