2 # -*- coding: utf-8 -*-
3 ####################################################
4 # Copyright © 2009 Luxia SAS. All rights reserved. #
7 # - Benoît Pin <pinbe@luxia.fr> #
8 ####################################################
10 Downloads RSS based order description and make a local human readable file tree
11 to facilitate printing tasks.
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
27 ELEMENT_NODE
= Node
.ELEMENT_NODE
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
)
36 def getXml(url
, opener
) :
37 url
= '%s?disable_cookie_login__=1' % url
38 xml
= opener
.open(url
).read()
41 def genFileTree(url
, login
, password
, dest
) :
42 opener
= getHttpOpener(url
, login
, password
)
43 xml
= getXml(url
, opener
)
45 doc
= d
.documentElement
47 channel
= doc
.getElementsByTagName('channel')[0]
48 orderName
= getContentOf(channel
, 'title')
53 for item
in iterElementChildsByTagName(d
.documentElement
, 'item') :
54 ppTitle
= getContentOf(item
, 'pp:title')
55 ppQuantity
= getContentOf(item
, 'pp:quantity')
57 printTypePath
= join(orderName
, ppTitle
)
58 printQuantityPath
= join(orderName
, ppTitle
, ppQuantity
)
60 if not exists(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'))
68 if not exists(printQuantityPath
) :
69 mkdir(printQuantityPath
)
71 hdUrl
= '%s?disable_cookie_login__=1' % getContentOf(item
, 'link')
72 localFileName
= getContentOf(item
, 'title')
74 localFile
= open(join(printQuantityPath
, localFileName
), 'w')
75 localFile
.write(opener
.open(hdUrl
).read())
78 def iterElementChildsByTagName(parent
, tagName
) :
79 child
= parent
.firstChild
81 if child
.nodeType
== ELEMENT_NODE
and child
.tagName
== tagName
:
83 child
= child
.nextSibling
85 def getContentOf(parent
, tagName
) :
86 child
= parent
.firstChild
88 if child
.nodeType
== ELEMENT_NODE
and child
.tagName
== tagName
:
89 return child
.firstChild
.nodeValue
.encode('utf-8')
90 child
= child
.nextSibling
92 raise ValueError("%r tag not found" % tagName
)
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]')
101 dest
= expanduser('~/Desktop')
103 genFileTree(url
, login
, password
, dest
)
105 if __name__
== '__main__' :