Last active
March 20, 2019 16:15
-
-
Save arseniy-panfilov/48b71b1607e390e047b5f11d326a851b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import json | |
import re | |
import human_curl as hurl | |
try: | |
from urllib.parse import urlencode, urljoin | |
except ImportError: | |
from urllib import urlencode | |
from urlparse import urljoin | |
class VKAudioScobbler(): | |
def __init__(self): | |
self._request_headers = { | |
'accept': 'text/html,application/xhtml+xml,application/xmlq=0.9,image/webp,*/*q=0.8', | |
'content-type': 'application/x-www-form-urlencoded', | |
'user-agent': 'Mozilla/5.0 (Windows NT 10.0 WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36' | |
} | |
self._auth_cookies = None | |
self._root_url = 'https://vk.com' | |
self._login_url = 'https://login.vk.com/?act=login' | |
def auth(self, login, password): | |
# получаем главную страницу | |
main_response = hurl.post( | |
url=self._root_url, | |
headers=self._request_headers | |
) | |
# парсим с главной страницы параметры ip_h и lg_h | |
ip_h = re.search('name=\"ip_h\" value=\"(.*?)\"', main_response.content, re.MULTILINE).group(1) | |
lg_h = re.search('name=\"lg_h\" value=\"(.*?)\"', main_response.content, re.MULTILINE).group(1) | |
login_params = { | |
'act': 'login', | |
'role': 'al_frame', | |
'_origin': self._root_url, | |
'ip_h': ip_h, | |
'lg_h': lg_h, | |
'email': login, | |
'pass': password | |
} | |
# логинимся | |
auth_response = hurl.post(self._login_url, data=login_params, headers=self._request_headers, | |
cookies=main_response.cookies) | |
location = auth_response.headers['Location'] | |
if not '__q_hash' in location: | |
raise Exception("Failed to auth") | |
# в случае успешной авторизации - переходим по редиректу | |
login_response = hurl.post(location, headers=self._request_headers, cookies=auth_response.cookies) | |
self._auth_cookies = login_response.cookies | |
def get_audio_list(self, owner_id): | |
if not self._auth_cookies: | |
raise Exception("Not authorized") | |
audio_response = hurl.post( | |
url=urljoin(self._root_url, 'al_audio.php'), | |
headers=self._request_headers, | |
cookies=self._auth_cookies, | |
data={ | |
"act": "load_silent", | |
"al": 1, | |
"album_id": -2, | |
"band": False, | |
"owner_id": owner_id | |
} | |
) | |
audios_json = re.search(r'list\"\:(\[\[(.*)\]\])', audio_response.content, re.MULTILINE | re.UNICODE).group(1) | |
audios = json.loads(audios_json, encoding='1251') | |
return [(audio[4], audio[3]) for audio in audios] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Возвращает информацию об аудиозаписях в виде списка кортежей:
(Исполнитель, Название)
Пример использования: