Désactivation d'options obsolètes.
[photoprint.git] / _utils / import_printing_list.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #######################################################################################
4 # Copyright © 2009 Benoît Pin <pin@cri.ensmp.fr> #
5 # Plinn - http://plinn.org #
6 # #
7 # #
8 # This program is free software; you can redistribute it and/or #
9 # modify it under the terms of the GNU General Public License #
10 # as published by the Free Software Foundation; either version 2 #
11 # of the License, or (at your option) any later version. #
12 # #
13 # This program is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16 # GNU General Public License for more details. #
17 # #
18 # You should have received a copy of the GNU General Public License #
19 # along with this program; if not, write to the Free Software #
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #
21 #######################################################################################
22 """
23 Downloads RSS based order description and make a local human readable file tree
24 to facilitate printing tasks.
25
26 """
27
28
29 from urllib2 import HTTPBasicAuthHandler
30 from urllib2 import build_opener
31 from urllib2 import urlopen
32 from xml.dom.minidom import parseString
33 from xml.dom import Node
34 from os import mkdir, chdir
35 from os.path import abspath, join, expanduser, exists
36 from getpass import getpass
37
38 ELEMENT_NODE = Node.ELEMENT_NODE
39
40 def getHttpOpener(url, login, password) :
41 auth_handler = HTTPBasicAuthHandler()
42 host = '/'.join(url.split('/', 3)[:3])
43 auth_handler.add_password('Zope', host, login, password)
44 opener = build_opener(auth_handler)
45 return opener
46
47 def getXml(url, opener) :
48 url = '%s?disable_cookie_login__=1' % url
49 xml = opener.open(url).read()
50 return xml
51
52 def genFileTree(url, login, password, dest) :
53 opener = getHttpOpener(url, login, password)
54 xml = getXml(url, opener)
55 d = parseString(xml)
56 doc = d.documentElement
57
58 channel = doc.getElementsByTagName('channel')[0]
59 orderName = getContentOf(channel, 'title')
60
61 chdir(dest)
62 mkdir(orderName)
63
64 for item in iterElementChildsByTagName(d.documentElement, 'item') :
65 ppTitle = getContentOf(item, 'pp:title')
66 ppQuantity = getContentOf(item, 'pp:quantity')
67
68 printTypePath = join(orderName, ppTitle)
69 printQuantityPath = join(orderName, ppTitle, ppQuantity)
70
71 if not exists(printTypePath) :
72 mkdir(printTypePath)
73 infoFile = open(join(printTypePath, 'info.txt'), 'w')
74 infoFile.write(getContentOf(item, 'pp:title'))
75 infoFile.write('\n\n')
76 infoFile.write(getContentOf(item, 'pp:description'))
77 infoFile.close()
78
79 if not exists(printQuantityPath) :
80 mkdir(printQuantityPath)
81
82 hdUrl = '%s?disable_cookie_login__=1' % getContentOf(item, 'link')
83 localFileName = getContentOf(item, 'title')
84 print localFileName
85 localFile = open(join(printQuantityPath, localFileName), 'w')
86 localFile.write(opener.open(hdUrl).read())
87 localFile.close()
88
89 def iterElementChildsByTagName(parent, tagName) :
90 child = parent.firstChild
91 while child :
92 if child.nodeType == ELEMENT_NODE and child.tagName == tagName :
93 yield child
94 child = child.nextSibling
95
96 def getContentOf(parent, tagName) :
97 child = parent.firstChild
98 while child :
99 if child.nodeType == ELEMENT_NODE and child.tagName == tagName :
100 return child.firstChild.nodeValue.encode('utf-8')
101 child = child.nextSibling
102
103 raise ValueError("%r tag not found" % tagName)
104
105
106 def main() :
107 url = raw_input('url flux xml de la commande : ')
108 login = raw_input('login : ')
109 password = getpass('mot de passe : ')
110 dest = raw_input('cible [~/Desktop]')
111 if not dest :
112 dest = expanduser('~/Desktop')
113
114 genFileTree(url, login, password, dest)
115
116 if __name__ == '__main__' :
117 main()
118
119