Guest User

Untitled

a guest
Aug 31st, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. from urllib import urlencode
  2. from cookielib import CookieJar
  3. import urllib2
  4. from lxml import etree
  5. from zope.interface import classProvides
  6. from collective.blueprint.base import blueprint
  7. from collective.transmogrifier.interfaces import ISectionBlueprint
  8.  
  9. import acoi.migration.ClientForm as ClientForm
  10. import acoi.migration.ClientCookie as ClientCookie
  11.  
  12. from logging import getLogger
  13. logger = getLogger('acoi.migration')
  14.  
  15. import acoi.migration.ClientForm as ClientForm
  16. import acoi.migration.ClientCookie as ClientCookie
  17.  
  18. class AcoiSourceBlueprint(blueprint.Source):
  19.  
  20. classProvides(ISectionBlueprint)
  21.  
  22. def _yieldItem(self, opener, url):
  23. try:
  24. resp = opener.urlopen(url)
  25. except:
  26. logger.info("ATTENZIONE: Impossibile migrare %r", url)
  27. raise
  28. root = etree.fromstring(resp.read())
  29. children_calls = []
  30. ret = {}
  31. for elem in root.findall('attr'):
  32. name = elem.get('name')
  33. if name == 'children_calls':
  34. children_calls = eval(elem.text)
  35. else:
  36. ret[name] = elem.text
  37.  
  38. msg = 'migrazione oggetto: %r'
  39. logger.info(msg, url)
  40. # logger.info(ret)
  41. if ret.keys():
  42. if ret['type'] not in self.options['excluded_types'].splitlines():
  43. yield ret
  44. if self.options['recursive'] == 'True':
  45. for child_url in children_calls:
  46. for elem in self._yieldItem(opener, child_url):
  47. yield elem
  48. else:
  49. logger.info('oggetto NON MIGRABILE %r', url)
  50.  
  51. def getItems(self):
  52. portal_url = self.options['portal_url']
  53. source_urls = [el for el in self.options['source_urls'].splitlines() if el]
  54. source_username = self.options['username']
  55. source_password = self.options['password']
  56. # opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(CookieJar()))
  57. # opener.addheaders = [
  58. # ('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; sl; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8')
  59. # ]
  60. # resp = opener.open(portal_url+'/login_form',
  61. # data=urlencode({'__ac_name': source_username, '__ac_password': source_password}))
  62.  
  63. login_form = ClientCookie.urlopen(portal_url+'/login_form')
  64. # NB: we should specify the login form among all the form in the page..
  65. form = ClientForm.ParseResponse(login_form,backwards_compat=False)[0]
  66. form['__ac_name'] = source_username
  67. form['__ac_password'] = source_password
  68. ClientCookie.urlopen(form.click()).read()
  69. import pdb; pdb.set_trace( )
  70. logger.info('INIZIO MIGRAZIONE')
  71. for source_url in source_urls:
  72. target_url = portal_url + source_url
  73. logger.info('inizio migrazione per: %r', target_url)
  74. for elem in self._yieldItem(ClientCookie, target_url):
  75. yield elem
  76. logger.info('MIGRAZIONE COMPLETATA per: %r', target_url)
  77. logger.info('MIGRAZIONE COMPLETATA')
Add Comment
Please, Sign In to add comment