newmediamonitoring/polls/utils.py

179 lines
6.6 KiB
Python
Raw Normal View History

2020-09-03 02:56:28 +00:00
import requests
2021-01-10 07:21:51 +00:00
from requests.auth import HTTPBasicAuth
2020-08-20 10:34:14 +00:00
import random
2020-09-03 02:56:28 +00:00
from parsel import Selector
from channels.db import database_sync_to_async
from django.conf import settings
2020-09-20 11:14:54 +00:00
from itertools import chain
2020-11-17 00:45:44 +00:00
from yunpian_python_sdk.model import constant as YC
from yunpian_python_sdk.ypclient import YunpianClient
2020-11-28 10:23:56 +00:00
from aliyunsdkdyvmsapi.request.v20170525 import SingleCallByTtsRequest
from aliyunsdkcore.client import AcsClient
import uuid
2021-01-26 15:33:40 +00:00
import json
2020-11-28 10:23:56 +00:00
from aliyunsdkcore.profile import region_provider
2020-09-03 02:56:28 +00:00
2021-01-10 07:21:51 +00:00
def send_tnps(phones, content):
2021-01-26 15:33:40 +00:00
url = 'https://api.tpns.tencent.com/v3/push/app'
2021-01-10 07:21:51 +00:00
r = requests.post(url,
auth=HTTPBasicAuth(
'1500015491', 'd5c793535529b6dc55d00b22302bad64'),
json={
"audience_type": "account_list",
"account_list": phones,
"message": {
2021-01-30 13:13:28 +00:00
"title": content,
"content": ''
2021-01-10 07:21:51 +00:00
},
2021-01-26 15:33:40 +00:00
"message_type": "notify",
"account_push_type": 1
2021-01-10 07:21:51 +00:00
})
2021-01-10 13:22:39 +00:00
return r
2021-01-10 07:21:51 +00:00
2020-09-03 09:46:04 +00:00
def sent_sms_code(phone, code):
2020-11-17 00:45:44 +00:00
clnt = YunpianClient('304eb08353f7ebf00596737acfc31f53')
2020-11-28 10:23:56 +00:00
param = {YC.MOBILE: phone,
YC.TEXT: '【甘肃大未来科技】您的验证码是%s。如非本人操作,请忽略本短信' % (code,)}
2020-11-17 00:45:44 +00:00
r = clnt.sms().single_send(param)
return r
def sent_sms_notify(phone, type):
clnt = YunpianClient('304eb08353f7ebf00596737acfc31f53')
2020-11-18 09:31:01 +00:00
if type == 1:
2020-11-28 10:23:56 +00:00
param = {YC.MOBILE: phone,
YC.TEXT: '【甘肃大未来科技】政务新媒体管理APP有新消息请及时登录处理。若其他管理员已处理请忽略*测试*'}
2020-11-17 00:45:44 +00:00
else:
2020-11-28 10:23:56 +00:00
param = {YC.MOBILE: phone, YC.TEXT: '【甘肃大未来科技】政务新媒体管理APP有新任务请及时登录处理。'}
2020-11-17 00:45:44 +00:00
r = clnt.sms().single_send(param)
return r
2020-08-20 10:34:14 +00:00
2020-09-03 02:56:28 +00:00
2020-11-28 10:23:56 +00:00
acs_client = AcsClient("LTAI4GDaSp41vp1X4mCb7uCa",
"am8fsIou5TZULV7mEAIEUwYfxN3cHK", "cn-hangzhou")
region_provider.add_endpoint(
"Dyvmsapi", "cn-hangzhou", "dyvmsapi.aliyuncs.com")
2021-01-10 07:21:51 +00:00
2020-12-05 04:46:36 +00:00
def send_voice_notify(phone, type):
2020-11-28 10:23:56 +00:00
business_id = uuid.uuid4()
ttsRequest = SingleCallByTtsRequest.SingleCallByTtsRequest()
# 申请的语音通知tts模板编码,必填
2020-12-05 04:46:36 +00:00
template_code = "TTS_205885979" if type == 1 else "TTS_205890042"
ttsRequest.set_TtsCode(template_code)
2020-11-28 10:23:56 +00:00
# 设置业务请求流水号,必填。后端服务基于此标识区分是否重复请求的判断
ttsRequest.set_OutId(business_id)
# 语音通知的被叫号码,必填。
ttsRequest.set_CalledNumber(phone)
# 语音通知显示号码,必填。
ttsRequest.set_CalledShowNumber("")
# 调用tts文本呼叫接口返回json
ttsResponse = acs_client.do_action_with_exception(ttsRequest)
return ttsResponse
2021-01-10 07:21:51 +00:00
2020-08-20 10:34:14 +00:00
def generate_code():
2020-08-24 13:43:19 +00:00
return random.randint(1000, 9999)
2020-09-03 02:56:28 +00:00
def detect_type(url):
if 'mp.weixin.qq.com' in url:
return 'weixin'
elif 'toutiao.com' in url:
return 'toutiao'
elif 'ixigua.com' in url:
return 'xigua'
2020-10-21 16:29:06 +00:00
elif 'gansudaily.com.cn' in url:
return 'xgs'
2020-09-03 02:56:28 +00:00
else:
return 'other'
def parse(url):
t = detect_type(url)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 QBCore/3.53.1159.400 QQBrowser/9.0.2524.400 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
}
r = requests.get(url, headers=headers)
2021-01-26 04:23:57 +00:00
print(r.encoding)
if t == 'xgs' and r.encoding=='ISO-8859-1':
2021-01-26 04:02:02 +00:00
r.encoding='GBK'
else:
r.encoding = 'utf-8'
2021-01-26 04:23:57 +00:00
# way2@8797799
2020-09-03 02:56:28 +00:00
selector = Selector(text=r.text)
if t == 'weixin':
og_title = selector.xpath(
'//head/meta[@property="og:title"]/@content').get()
og_description = selector.xpath(
'//head/meta[@property="og:description"]/@content').get()
og_url = selector.xpath(
'//head/meta[@property="og:url"]/@content').get()
og_image = selector.xpath(
'//head/meta[@property="og:image"]/@content').get()
elif t == 'toutiao':
2020-10-21 16:29:06 +00:00
og_title = selector.xpath('//head/title/text()').get()
og_description = selector.xpath(
'//head/meta[@name="description"]/@content').get()
og_url = url
og_image = None
elif t == 'xgs':
og_title = selector.xpath('//head/title/text()').get()
2020-09-03 02:56:28 +00:00
og_description = selector.xpath(
'//head/meta[@name="description"]/@content').get()
og_url = url
og_image = None
else:
2020-10-21 16:29:06 +00:00
og_title = selector.xpath('//head/title/text()').get()
2020-09-03 02:56:28 +00:00
og_description = selector.xpath(
'//head/meta[@name="description"]/@content').get()
og_url = url
og_image = None
return (og_title, og_description, og_url, og_image)
2020-09-20 11:14:54 +00:00
def model_to_dict(instance, fields):
opts = instance._meta
data = {}
for f in chain(opts.concrete_fields, opts.private_fields, opts.many_to_many):
if not getattr(f, 'editable', False):
continue
if fields and f.name not in fields:
continue
if f.name == 'id':
data[f.name] = str(f.value_from_object(instance))
else:
data[f.name] = f.value_from_object(instance)
return data
def queryset_to_list(q, fields):
l = []
for row in q:
r = model_to_dict(row, fields)
l.append(r)
return l
2021-01-10 07:21:51 +00:00
2020-08-24 13:43:19 +00:00
if __name__ == '__main__':
2020-11-28 10:23:56 +00:00
# r = sent_sms_code('13993199566', 4321)
# print(r.code(), type(r.code()))
2020-10-21 16:29:06 +00:00
# og_title, og_description, og_url, og_image = parse(
# 'https://mp.weixin.qq.com/s/EhX0Pm1e0FAfse0zz9ow8Q')
# og_title, og_description, og_url, og_image = parse(
# 'https://m.toutiao.com/i6883651337003729420/?tt_from=weixin&utm_campaign=client_share&app=news_article&utm_source=weixin&iid=1494959660475024&utm_medium=toutiao_android&wxshare_count=1')
# og_title, og_description, og_url, og_image = parse(
# 'http://www.gov.cn/xinwen/2020-10/13/content_5550906.htm')
2021-01-26 15:33:40 +00:00
# og_title, og_description, og_url, og_image = parse(
# 'http://gansu.gansudaily.com.cn/system/2021/01/25/030261998.shtml')
2020-10-21 16:29:06 +00:00
2021-01-26 15:33:40 +00:00
# print(og_title, og_description, og_url, og_image)
2021-01-10 13:22:39 +00:00
# print(send_voice_notify('13993199566'))
2021-02-03 03:07:42 +00:00
r = send_tnps(['13609346975'], 'contenteeeent!!!!')
2021-01-26 15:33:40 +00:00
print(r.status_code, r.text)