前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python粉丝数实时播报程序

Python粉丝数实时播报程序

作者头像
MoyiTech
发布2023-02-02 11:06:59
3340
发布2023-02-02 11:06:59
举报
文章被收录于专栏:科技日常科技日常

源码:

代码语言:javascript
复制
import requests
import time

import sys
from playsound import _playsoundWin

# 保证兼容python2以及python3
IS_PY3 = sys.version_info.major == 3
if IS_PY3:
    from urllib.request import urlopen
    from urllib.request import Request
    from urllib.error import URLError
    from urllib.parse import urlencode
    from urllib.parse import quote_plus
else:
    import urllib2
    from urllib import quote_plus
    from urllib2 import urlopen
    from urllib2 import Request
    from urllib2 import URLError
    from urllib import urlencode


def hechng(res):
    # 替换你的 API_KEY(百度开放平台)
    API_KEY = '替换你的 API_KEY'

    # 替换你的 SECRET_KEY(百度开放平台)
    SECRET_KEY = '替换你的 API_KEY'

    # 大姚的订单信息内容文本
    TEXT = res

    TTS_URL = 'http://tsn.baidu.com/text2audio'

    """  TOKEN start """

    TOKEN_URL = 'http://openapi.baidu.com/oauth/2.0/token'

    """
        获取token
    """

    def fetch_token():
        params = {'grant_type': 'client_credentials',
                  'client_id': API_KEY,
                  'client_secret': SECRET_KEY}
        post_data = urlencode(params)
        if (IS_PY3):
            post_data = post_data.encode('utf-8')
        req = Request(TOKEN_URL, post_data)
        try:
            f = urlopen(req, timeout=5)
            result_str = f.read()
        except URLError as err:
            print('token http response http code : ' + str(err.code))
            result_str = err.read()
        if (IS_PY3):
            result_str = result_str.decode()

        result = json.loads(result_str)

        if ('access_token' in result.keys() and 'scope' in result.keys()):
            if not 'audio_tts_post' in result['scope'].split(' '):
                print('please ensure has check the tts ability')
                exit()
            return result['access_token']
        else:
            print('please overwrite the correct API_KEY and SECRET_KEY')
            exit()

    """  TOKEN end """

    if __name__ == '__main__':

        token = fetch_token()

        tex = quote_plus(TEXT)  # 此处TEXT需要两次urlencode

        params = {'tok': token, 'tex': tex, 'cuid': "quickstart",
                  'lan': 'zh', 'ctp': 1}  # lan ctp 固定参数

        data = urlencode(params)

        req = Request(TTS_URL, data.encode('utf-8'))
        has_error = False
        try:
            f = urlopen(req)
            result_str = f.read()

            headers = dict((name.lower(), value) for name, value in f.headers.items())

            has_error = ('content-type' not in headers.keys() or headers['content-type'].find('audio/') < 0)
        except  URLError as err:
            print('http response http code : ' + str(err.code))
            result_str = err.read()
            has_error = True

        save_file = "error.txt" if has_error else u'tmp.mp3'

        with open(save_file, 'wb') as of:
            of.write(result_str)

        if has_error:
            if (IS_PY3):
                result_str = str(result_str, 'utf-8')
            print("tts api  error:" + result_str)

        print("file saved as : " + save_file)

        _playsoundWin('tmp.mp3')
        # playsound('tmp.mp3')


time.sleep(10)
res = requests.get('https://api.bilibili.com/x/relation/stat?vmid=128723428&jsonp=jsonp')
res = res.json()
res = res['data']['follower']
print(res)

while True:
    time.sleep(1)
    tmp = requests.get('https://api.bilibili.com/x/relation/stat?vmid=128723428&jsonp=jsonp')
    tmp = tmp.json()
    tmp = tmp['data']['follower']
    print(tmp)
    # if os.path.exists('tmp.mp3'):
    #     os.remove('tmp.mp3')
    if tmp > res:
        hechng('粉丝数增加了' + str(tmp - res) + ',目前粉丝数' + str(tmp))
        res = tmp
    if tmp < res:
        hechng('粉丝数减少了' + str(res - tmp) + ',目前粉丝数' + str(tmp))
        res = tmp
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档