import datetime
import json
import logging
import requests
logger = logging.getLogger(__name__)
[docs]
class Profile():
"""Entails all relevant data concerning a profile
:ivar int follower_count: Follower count
:ivar int following_count: Following count
:ivar int friend_count: Friend count
:ivar int heart: Heart
:ivar int heart_count: Heart count
:ivar int video_count: Video count
:ivar bool commerce_user: Is commerce user True/False
:ivar str nickname: Nickname
:ivar bool private_account: Is private account True/False
:ivar str signature: Signature
:ivar bool verified: Is verified True/False
"""
def __init__(self, **kwargs):
kwargs = kwargs["userInfo"]
self.follower_count = int(kwargs["stats"]["followerCount"])
self.following_count = int(kwargs["stats"]["followingCount"])
self.friend_count = int(kwargs["stats"]["friendCount"])
self.heart = int(kwargs["stats"]["heart"])
self.heart_count = int(kwargs["stats"]["heartCount"])
self.video_count = int(kwargs["stats"]["videoCount"])
self.commerce_user = bool(kwargs["user"]["commerceUserInfo"]["commerceUser"]) if "commerceUserInfo" in kwargs["user"] else None
self.nickname = kwargs["user"]["nickname"]
self.unique_id = kwargs["user"]["uniqueId"]
self.secUid = kwargs["user"]["secUid"]
self.private_account = bool(kwargs["user"]["privateAccount"])
self.signature = kwargs["user"]["signature"]
self.verified = bool(kwargs["user"]["verified"])
[docs]
class Video():
"""Entails all relevant data concerning a video
:ivar str video_id: Video ID
:ivar str description: Description
:ivar int create_time: Creat time
:ivar bool is_pinned: Is pinned
:ivar str music_author: Music author name
:ivar str music_id: Music id
:ivar str music_title: Music title
:ivar str music_url: Music URL
:ivar int collect_count: Collect count
:ivar int comment_count: Comment count
:ivar int digg_count: Digg Count
:ivar int play_count: Play count
:ivar int share_count: Share count
:ivar str download_url: Download URL
"""
def __init__(self, **kwargs):
self.id = kwargs["id"]
self.description = kwargs["desc"]
self.create_time = int(kwargs["createTime"])
self.is_pinned = bool(kwargs["isPinnedItem"]) if "isPinnedItem" in kwargs else False
self.music_author = kwargs["music"]["authorName"]
self.music_id = kwargs["music"]["id"]
self.music_title = kwargs["music"]["title"]
self.music_url = kwargs["music"]["playUrl"] if "playUrl" in kwargs["music"] else None
self.collect_count = kwargs["stats"]["collectCount"]
self.comment_count = kwargs["stats"]["commentCount"]
self.digg_count = kwargs["stats"]["diggCount"]
self.play_count = kwargs["stats"]["playCount"]
self.share_count = kwargs["stats"]["shareCount"]
# not all videos provide a download adress
self.download_url = kwargs["video"]["downloadAddr"] if "video" in kwargs and "downloadAddr" in kwargs["video"] else None
def download(self, location):
if self.download_url is None:
logger.info("No download url available.")
return None
res = requests.get(self.download_url, stream=True)
with open(location, 'wb') as fd:
for chunk in res.iter_content(chunk_size=128):
fd.write(chunk)