Mise à jour fonction utilitaire « translate ».
[photoprint.git] / _utils / import_printing_list.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 ####################################################
4 # Copyright © 2009 Luxia SAS. All rights reserved. #
5 # #
6 # Contributors: #
7 # - Benoît Pin <pinbe@luxia.fr> #
8 ####################################################
9 """
10 Downloads RSS based order description and make a local human readable file tree
11 to facilitate printing tasks.
12
13
14
15 """
16
17
18 from urllib2 import HTTPBasicAuthHandler
19 from urllib2 import build_opener
20 from urllib2 import urlopen
21 from xml.dom.minidom import parseString
22 from xml.dom import Node
23 from os import mkdir, chdir
24 from os.path import abspath, join, expanduser, exists
25 from getpass import getpass
26
27 ELEMENT_NODE = Node.ELEMENT_NODE
28
29 def getHttpOpener(url, login, password) :
30 auth_handler = HTTPBasicAuthHandler()
31 host = '/'.join(url.split('/', 3)[:3])
32 auth_handler.add_password('Zope', host, login, password)
33 opener = build_opener(auth_handler)
34 return opener
35
36 def getXml(url, opener) :
37 url = '%s?disable_cookie_login__=1' % url
38 xml = opener.open(url).read()
39 return xml
40
41 def genFileTree(url, login, password, dest) :
42 opener = getHttpOpener(url, login, password)
43 xml = getXml(url, opener)
44 d = parseString(xml)
45 doc = d.documentElement
46
47 channel = doc.getElementsByTagName('channel')[0]
48 orderName = getContentOf(channel, 'title')
49
50 chdir(dest)
51 mkdir(orderName)
52
53 for item in iterElementChildsByTagName(d.documentElement, 'item') :
54 ppTitle = getContentOf(item, 'pp:title')
55 ppQuantity = getContentOf(item, 'pp:quantity')
56
57 printTypePath = join(orderName, ppTitle)
58 printQuantityPath = join(orderName, ppTitle, ppQuantity)
59
60 if not exists(printTypePath) :
61 mkdir(printTypePath)
62 infoFile = open(join(printTypePath, 'info.txt'), 'w')
63 infoFile.write(getContentOf(item, 'pp:title'))
64 infoFile.write('\n\n')
65 infoFile.write(getContentOf(item, 'pp:description'))
66 infoFile.close()
67
68 if not exists(printQuantityPath) :
69 mkdir(printQuantityPath)
70
71 hdUrl = '%s?disable_cookie_login__=1' % getContentOf(item, 'link')
72 localFileName = getContentOf(item, 'title')
73 print localFileName
74 localFile = open(join(printQuantityPath, localFileName), 'w')
75 localFile.write(opener.open(hdUrl).read())
76 localFile.close()
77
78 def iterElementChildsByTagName(parent, tagName) :
79 child = parent.firstChild
80 while child :
81 if child.nodeType == ELEMENT_NODE and child.tagName == tagName :
82 yield child
83 child = child.nextSibling
84
85 def getContentOf(parent, tagName) :
86 child = parent.firstChild
87 while child :
88 if child.nodeType == ELEMENT_NODE and child.tagName == tagName :
89 return child.firstChild.nodeValue.encode('utf-8')
90 child = child.nextSibling
91
92 raise ValueError("%r tag not found" % tagName)
93
94
95 def main() :
96 url = raw_input('url flux xml de la commande : ')
97 login = raw_input('login : ')
98 password = getpass('mot de passe : ')
99 dest = raw_input('cible [~/Desktop]')
100 if not dest :
101 dest = expanduser('~/Desktop')
102
103 genFileTree(url, login, password, dest)
104
105 if __name__ == '__main__' :
106 main()
107
108