Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added proxies support to most get* funcs #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions weibo_base/weibo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
_COMMENTS_HOTFLOW = "https://m.weibo.cn/comments/hotflow"


def search_by_name(name: str) -> Response:
def search_by_name(name: str, proxies: dict = None) -> Response:
"""get summary info which searched by name,
this api is like 'https://m.weibo.cn/api/container/getIndex?queryVal=<name sample as Helixcs>&containerid=100103type%3D3%26q%3D<name sample as Helixcs>'

Expand All @@ -27,13 +27,13 @@ def search_by_name(name: str) -> Response:
:return json string including summary info
"""
_params = {'queryVal': name, 'containerid': '100103type%3D3%26q%3D' + name}
_response = requests.get(url=_GET_INDEX, params=_params)
_response = requests.get(url=_GET_INDEX, params=_params, proxies=proxies, verify=False)
if _response.status_code == 200:
return _response.json()
return None


def weibo_getIndex(uid_value: str) -> Response:
def weibo_getIndex(uid_value: str, proxies: dict = None) -> Response:
"""
get personal summary info which request by uid, and uid is got by 'search_by_name'
this api is like 'https://m.weibo.cn/api/container/getIndex?type=uid&value=<uid_value sample as 1843242321>'
Expand All @@ -44,13 +44,13 @@ def weibo_getIndex(uid_value: str) -> Response:
:return:
"""
_params = {"type": "uid", "value": uid_value}
_response = requests.get(url=_GET_INDEX, params=_params)
_response = requests.get(url=_GET_INDEX, params=_params, proxies=proxies, verify=False)
if _response.status_code == 200:
return _response.json()
return None


def weibo_tweets(containerid: str, page: int) -> Response:
def weibo_tweets(containerid: str, page: int, proxies: dict = None) -> Response:
"""
get person weibo tweets which from contaninerid in page,
this api is like 'https://m.weibo.cn/container/getIndex?containerid=<containerid>&page=<page>'
Expand All @@ -61,63 +61,63 @@ def weibo_tweets(containerid: str, page: int) -> Response:
:return:
"""
_params = {"containerid": containerid, "page": page}
_response = requests.get(url=_GET_INDEX, params=_params)
_response = requests.get(url=_GET_INDEX, params=_params, proxies=proxies, verify=False)
if _response.status_code == 200 and _response.json().get("ok") == 1:
return _response.json()
raise WeiboApiException(
"weibo_tweets request failed, url={0},params={1},response={2}".format(_GET_INDEX, _params,
_response if _response is None else _response.text))


def weibo_containerid(containerid: str, page: int) -> Response:
def weibo_containerid(containerid: str, page: int, proxies: dict = None) -> Response:
"""

:param containerid:
:param page:
:return:
"""
_params = {"containerid": containerid, "page": page}
_response = requests.get(url=_GET_INDEX, params=_params)
_response = requests.get(url=_GET_INDEX, params=_params, proxies=proxies, verify=False)
if _response.status_code == 200 and _response.json().get("ok") == 1:
return _response.json()
raise WeiboApiException(
"weibo_containerid request failed, url={0},params={1},response={2}".format(_GET_INDEX, _params, _response))


def weibo_second(containerid: str, page: int) -> Response:
def weibo_second(containerid: str, page: int, proxies: dict = None) -> Response:
"""
https://m.weibo.cn/api/container/getSecond
:param containerid:
:param page:
:return:
"""
_params = {"containerid": containerid, "page": page}
_response = requests.get(url=_GET_SECOND, params=_params)
_response = requests.get(url=_GET_SECOND, params=_params, proxies=proxies, verify=False)
if _response.status_code == 200 and _response.json().get("ok") == 1:
return _response.json()
raise WeiboApiException(
"weibo_second request failed, url={0},params={1},response={2}".format(_GET_SECOND, _params, _response))


def weibo_comments(id: str, mid: str) -> Response:
def weibo_comments(_id: str, mid: str, proxies: dict = None) -> Response:
"""
https://m.weibo.cn/comments/hotflow?id=4257059677028285&mid=4257059677028285
get comments from userId and mid
:param id: userId
:param mid: mid
:return:
"""
_params = {"id": id, "mid": mid}
_response = requests.get(url=_COMMENTS_HOTFLOW, params=_params)
_params = {"id": _id, "mid": mid}
_response = requests.get(url=_COMMENTS_HOTFLOW, params=_params, proxies=proxies, verify=False)
if _response.status_code == 200 and _response.json().get("ok") == 1:
return _response.json()
raise WeiboApiException(
"weibo_comments request failed, url={0},params={1},response={2}".format(_COMMENTS_HOTFLOW, _params, _response))


def realtime_hotword():
def realtime_hotword(proxies: dict = None):
_params = {"containerid": "106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Drealtimehot"}
_response = requests.get(url=_GET_INDEX, params=_params)
_response = requests.get(url=_GET_INDEX, params=_params, proxies=proxies, verify=False)

if _response.status_code == 200 and _response.json().get("ok") == 1:
return _response.json()
Expand Down
8 changes: 4 additions & 4 deletions weibo_base/weibo_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
from weibo_base.weibo_parser import weibo_getIndex, WeiboGetIndexParser


def exist_get_uid(search_by_name_response: str = None, name: str = "") -> Dict:
def exist_get_uid(search_by_name_response: str = None, name: str = "", proxies: dict = None) -> Dict:
"""
whether name is exist in response which from search api, if exist ,return uid
:param search_by_name_response:
:param name:
:return:
"""
if not search_by_name_response or str(search_by_name_response) == '':
search_by_name_response = search_by_name(name)
search_by_name_response = search_by_name(name, proxies=proxies)
# bad request
if search_by_name_response.get('ok') != 1:
return {"exist": False, "name": name, "uid": None}
Expand All @@ -36,7 +36,7 @@ def exist_get_uid(search_by_name_response: str = None, name: str = "") -> Dict:
return {"exist": False, "name": name, "uid": None}


def get_tweet_containerid(weibo_get_index_response: str = None, uid: str = ""):
def get_tweet_containerid(weibo_get_index_response: str = None, uid: str = "", proxies: dict = None):
"""
get weibo_containerid
:param weibo_get_index_response:
Expand All @@ -45,7 +45,7 @@ def get_tweet_containerid(weibo_get_index_response: str = None, uid: str = ""):
"""

if weibo_get_index_response is None or str(weibo_get_index_response) == '':
weibo_get_index_response = weibo_getIndex(uid)
weibo_get_index_response = weibo_getIndex(uid, proxies=proxies)
if weibo_get_index_response.get('ok') != 1:
return None

Expand Down
Loading