|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 如默 于 2022-11-4 16:55 编辑
先说一下,这个代码是网上看到用来给女友发送微信模板消息的,我调用了一个星座运势的接口,但是,不知道怎么把数据统一放在一个地方,然后调用这个。
目前代码比较笨,是请求多次,然后返回自己需要的数据,很浪费资源,能否修改一下,非常感谢!
多说一句:1,最重要是将多个请求合并成一个,且保留全部功能;2,能否将type参数后的这个星座名称设置成参数,这样方便修改,要不然需要修改很多个,比较麻烦
- import requests
- import json
- import random
- def httpGet(url, params):
- r = requests.get(url, params)
- return json.loads(r.content)
- def httpPost(url, params):
- r = requests.post(url, params)
- return json.loads(r.content)
- # 获取微信token
- def getAccessToken(appId, appSecret):
- params = {
- 'grant_type': 'client_credential',
- 'appid': appId,
- 'secret': appSecret
- }
- url = 'https://api.weixin.qq.com/cgi-bin/token'
- return httpGet(url, params)
- # 获取运势标题
- def getFortuneTitle():
- url = 'https://api.vvhan.com/api/horoscope'
- talk = httpGet(url, {'type': 'aries','time':'today'})
- return talk['data']['title']
- # 获取幸运颜色
- def getLuckyColor():
- url = 'https://api.vvhan.com/api/horoscope'
- talk = httpGet(url, {'type': 'aries','time':'today'})
- return talk['data']['luckycolor']
- # 获取幸运数字
- def getLuckyNumber():
- url = 'https://api.vvhan.com/api/horoscope'
- talk = httpGet(url, {'type': 'aries','time':'today'})
- return talk['data']['luckynumber']
- # 获取速配星座
- def getLuckyConstellation():
- url = 'https://api.vvhan.com/api/horoscope'
- talk = httpGet(url, {'type': 'aries','time':'today'})
- return talk['data']['luckyconstellation']
- # 获取短评
- def getShortComment():
- url = 'https://api.vvhan.com/api/horoscope'
- talk = httpGet(url, {'type': 'aries','time':'today'})
- return talk['data']['shortcomment']
- # 获取综合运势
- def getFortuneText():
- url = 'https://api.vvhan.com/api/horoscope'
- talk = httpGet(url, {'type': 'aries','time':'today'})
- return talk['data']['fortunetext']['all']
- # 获取爱情运势
- def getFortuneLove():
- url = 'https://api.vvhan.com/api/horoscope'
- talk = httpGet(url, {'type': 'aries','time':'today'})
- return talk['data']['fortunetext']['love']
- # 获取学业运势
- def getFortuneWork():
- url = 'https://api.vvhan.com/api/horoscope'
- talk = httpGet(url, {'type': 'aries','time':'today'})
- return talk['data']['fortunetext']['work']
- # 获取财富运势
- def getFortuneMoney():
- url = 'https://api.vvhan.com/api/horoscope'
- talk = httpGet(url, {'type': 'aries','time':'today'})
- return talk['data']['fortunetext']['money']
- # 获取健康运势
- def getFortuneHealth():
- url = 'https://api.vvhan.com/api/horoscope'
- talk = httpGet(url, {'type': 'aries','time':'today'})
- return talk['data']['fortunetext']['health']
- # 发送模版消息
- def sendTemplateMessage(content, accessToken):
- url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' + accessToken
- return httpPost(url, content)
- # 获取爱称
- def getNickName():
- nameHub = ['安琪','安琪宝贝','Angel','安安','琪琪','琪琪子','亲爱的安琪','心爱的安琪']
- currentName = random.choice(nameHub)
- return currentName
- if __name__ == '__main__':
- # 微信公众号的appId和appSecret
- appId = 'xxx'
- appSecret = 'xxx'
- # 要发送人的openId列表
- openIdList = ['xxx','xxx']
- # 模版Id
- templateId = 'xxx'
- accessTokenInfo = getAccessToken(appId, appSecret)
- accessToken = accessTokenInfo['access_token']
- name = getNickName()
- fortuneTitle = getFortuneTitle()
- luckyColor = getLuckyColor()
- luckyNumber = getLuckyNumber()
- luckyConstellation = getLuckyConstellation()
- shortComment = getShortComment()
- fortuneText = getFortuneText()
- fortuneLove = getFortuneLove()
- fortuneWork = getFortuneWork()
- fortuneMoney = getFortuneMoney()
- fortuneHealth = getFortuneHealth()
- for i in range(len(openIdList)):
- data = {
- 'touser': openIdList[i],
- 'template_id': templateId,
- 'topcolor' : '#FF0000',
- 'data': {
- 'name':{
- 'value': name,
- 'color': '#ee3f4d'
- },
- 'fortuneTitle':{
- 'value': fortuneTitle,
- 'color': '#282c34'
- },
- 'luckyColor':{
- 'value': luckyColor,
- 'color': '#065279'
- },
- 'luckyNumber':{
- 'value': luckyNumber,
- 'color': '#4b5cc4'
- },
- 'luckyConstellation':{
- 'value': luckyConstellation,
- 'color': '#e06c75'
- },
- 'shortComment':{
- 'value': shortComment,
- 'color':'#4c8dae'
- },
- 'fortuneText':{
- 'value': fortuneText,
- 'color': '#db5a6b'
- },
- 'fortuneLove':{
- 'value': fortuneLove,
- 'color': '#801dae'
- },
- 'fortuneWork':{
- 'value': fortuneWork,
- 'color': '#21a675'
- },
- 'fortuneMoney':{
- 'value': fortuneMoney,
- 'color': '#f2be45'
- },
- 'fortuneHealth':{
- 'value': fortuneHealth,
- 'color': '#b36d61'
- }
- }
- }
- params = json.dumps(data)
- print(sendTemplateMessage(params, accessToken))
复制代码
粗略看了一下你写的,说说我的第一感觉。
不用写那么多的函数啊,返回的本来就是一个字典形式的。本来数据就在一起的,现在你反而把它分开来写了,感觉这不是弄复杂了吗?
|
|