Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from urllib import urlencode
- from cookielib import CookieJar
- import urllib2
- from lxml import etree
- from zope.interface import classProvides
- from collective.blueprint.base import blueprint
- from collective.transmogrifier.interfaces import ISectionBlueprint
- import acoi.migration.ClientForm as ClientForm
- import acoi.migration.ClientCookie as ClientCookie
- from logging import getLogger
- logger = getLogger('acoi.migration')
- import acoi.migration.ClientForm as ClientForm
- import acoi.migration.ClientCookie as ClientCookie
- class AcoiSourceBlueprint(blueprint.Source):
- classProvides(ISectionBlueprint)
- def _yieldItem(self, opener, url):
- try:
- resp = opener.urlopen(url)
- except:
- logger.info("ATTENZIONE: Impossibile migrare %r", url)
- raise
- root = etree.fromstring(resp.read())
- children_calls = []
- ret = {}
- for elem in root.findall('attr'):
- name = elem.get('name')
- if name == 'children_calls':
- children_calls = eval(elem.text)
- else:
- ret[name] = elem.text
- msg = 'migrazione oggetto: %r'
- logger.info(msg, url)
- # logger.info(ret)
- if ret.keys():
- if ret['type'] not in self.options['excluded_types'].splitlines():
- yield ret
- if self.options['recursive'] == 'True':
- for child_url in children_calls:
- for elem in self._yieldItem(opener, child_url):
- yield elem
- else:
- logger.info('oggetto NON MIGRABILE %r', url)
- def getItems(self):
- portal_url = self.options['portal_url']
- source_urls = [el for el in self.options['source_urls'].splitlines() if el]
- source_username = self.options['username']
- source_password = self.options['password']
- # opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(CookieJar()))
- # opener.addheaders = [
- # ('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; sl; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8')
- # ]
- # resp = opener.open(portal_url+'/login_form',
- # data=urlencode({'__ac_name': source_username, '__ac_password': source_password}))
- login_form = ClientCookie.urlopen(portal_url+'/login_form')
- # NB: we should specify the login form among all the form in the page..
- form = ClientForm.ParseResponse(login_form,backwards_compat=False)[0]
- form['__ac_name'] = source_username
- form['__ac_password'] = source_password
- ClientCookie.urlopen(form.click()).read()
- import pdb; pdb.set_trace( )
- logger.info('INIZIO MIGRAZIONE')
- for source_url in source_urls:
- target_url = portal_url + source_url
- logger.info('inizio migrazione per: %r', target_url)
- for elem in self._yieldItem(ClientCookie, target_url):
- yield elem
- logger.info('MIGRAZIONE COMPLETATA per: %r', target_url)
- logger.info('MIGRAZIONE COMPLETATA')
Add Comment
Please, Sign In to add comment