forked from 0xHJK/music-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
45 lines (40 loc) · 1.36 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author: HJK
@file: api.py
@time: 2019-06-11
"""
import requests
from . import config
from .exceptions import RequestError, ResponseError, DataError
class MusicApi:
# class property
# 子类修改时使用deepcopy
session = requests.Session()
session.headers.update(config.get("fake_headers"))
if config.get("proxies"):
session.proxies.update(config.get("proxies"))
session.headers.update({"referer": "http://www.google.com/"})
@classmethod
def request(cls, url, method="POST", data=None):
if method == "GET":
resp = cls.session.get(url, params=data, timeout=7)
else:
resp = cls.session.post(url, data=data, timeout=7)
if resp.status_code != requests.codes.ok:
raise RequestError(resp.text)
if not resp.text:
raise ResponseError("No response data.")
return resp.json()
@classmethod
def requestInstance(cls, url, method="POST", data=None):
if method == "GET":
resp = cls.session.get(url, params=data, timeout=7)
else:
resp = cls.session.post(url, data=data, timeout=7)
if resp.status_code != requests.codes.ok:
raise RequestError(resp.text)
if not resp.text:
raise ResponseError("No response data.")
return resp