forked from 0xHJK/music-dl
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/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() |