kAldown

works

Sep 26th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. import pycurl
  2. from StringIO import StringIO
  3. import re
  4. from urllib import urlencode
  5. import requests
  6. from lxml import html
  7. import cookielib
  8.  
  9. params = {}
  10.  
  11. HEADERS = [
  12. 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  13. 'Content-Type: application/x-www-form-urlencoded',
  14. 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36'
  15. ]
  16.  
  17. login = 'login'
  18. password = 'pass'
  19.  
  20. def curl_post(url=None, params=None):
  21.  
  22.     storage = StringIO()
  23.  
  24.     ch = pycurl.Curl()
  25.  
  26.     ch.setopt(ch.URL,  url)
  27.  
  28.     ch.setopt(ch.HEADER, 1)
  29.     ch.setopt(ch.CONNECTTIMEOUT, 30)
  30.  
  31.     ch.setopt(ch.WRITEFUNCTION, storage.write)
  32.  
  33.     if params is not None:
  34.         if params.get('params'):
  35.             ch.setopt(ch.POST, 1)
  36.             ch.setopt(ch.POSTFIELDS, params['params'])
  37.  
  38.         if params.get('headers'):
  39.             ch.setopt(ch.HTTPHEADER, params['headers'])
  40.  
  41.         if params.get('cookies'):
  42.             ch.setopt(ch.COOKIE, params['cookies'])
  43.  
  44.     ch.perform()
  45.     ch.close()
  46.  
  47.     result = storage.getvalue()
  48.  
  49.     headers, content = result.split('\r\n\r\n')
  50.  
  51.     cookies_list = re.findall(r'Set-Cookie: (.*);', headers, re.MULTILINE)
  52.     cookies = ';'.join([c.split(';')[0] for c in cookies_list])
  53.  
  54.     with open('test.cookies', 'w') as cookies_file:
  55.         cookies_file.write(cookies)
  56.  
  57.     return {'headers': headers, 'cookies': cookies, 'content': content}
  58.  
  59. init_get = curl_post('https://vk.com', {'headers': HEADERS})
  60.  
  61. tree = html.fromstring(init_get['content'])
  62. ip_h = tree.xpath("//form[@id='quick_login_form']/input[@name='ip_h']")[0].value
  63. lg_h = tree.xpath("//form[@id='quick_login_form']/input[@name='lg_h']")[0].value
  64.  
  65. post_params = 'act=login&role=al_frame&%s&%s&%s&%s&%s' % (urlencode({'_origin': 'http://vk.com'}),
  66.                                                           urlencode({'ip_h': ip_h}),
  67.                                                           urlencode({'lg_h': lg_h}),
  68.                                                           urlencode({'email': login}),
  69.                                                           urlencode({'pass': password}))
  70.  
  71. auth_post = curl_post('https://login.vk.com/?act=login', {'headers': HEADERS,
  72.                                                           'params': post_params,
  73.                                                           'cookies': init_get['cookies']})
  74.  
  75. location = re.search(r'Location: (.*)', auth_post['headers'])
  76. if not location:
  77.     print 'Can not auth'
  78.     exit(1)
  79.  
  80. authed = curl_post(location.group(1), {'headers': HEADERS,
  81.                                        'cookies': init_get['cookies']})
  82.  
  83. remixsid = re.search(r'remixsid=\w*;', authed['cookies'])
  84. remixsid = remixsid.group()[:-1]
  85.  
  86. im = curl_post('https://vk.com/im', {'headers': HEADERS,
  87.                                 'cookies': remixsid})
  88.  
  89. with open('vk.html', 'w') as vk_file:
  90.     vk_file.write(im['content'])
Advertisement
Add Comment
Please, Sign In to add comment