Commit 708da2d9 authored by gaoming's avatar gaoming

nice

parent 0a61d644
......@@ -10,6 +10,12 @@ CELERY_BROKER_URL = 'redis://127.0.0.1:6379/0' # Broker配置,使用Redis作
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/0' # BACKEND配置,这里使用redis
CELERY_RESULT_SERIALIZER = 'json' # 结果序列化方案
REDIS_HOST="127.0.0.1"
REDIS_PASSWORD=""
REDIS_PORT=6379
REDIS_DB=0
#***********************************************************************************
......
......@@ -33,6 +33,12 @@ CELERY_BROKER_URL = 'redis://:zi1jlhVHtH8MGNqB@172.17.40.145:6379/7' # Broker配
CELERY_RESULT_BACKEND = 'redis://:zi1jlhVHtH8MGNqB@172.17.40.145:6379/7' # BACKEND配置,这里使用redis
CELERY_RESULT_SERIALIZER = 'json' # 结果序列化方案
REDIS_HOST="172.17.40.145"
REDIS_PASSWORD="zi1jlhVHtH8MGNqB"
REDIS_PORT=6379
REDIS_DB=7
#************************************requests***********************************************
REQUESTS_CONNECT_TIMEOUT=5
REQUESTS_READ_TIMEOUT=5
......
......@@ -7,6 +7,7 @@ urlpatterns =[
re_path(r'^[a-z0-9]+/((?P<sub_name>[a-z0-9]+)/)?click-notify$', views.Channel.as_view()),
re_path(r'^statistic$', views.Statistic.as_view()),
re_path(r'^[a-z0-9]+/doublecheck$', views.DoubleCheck.as_view()),
re_path('callback',views.CallBack.as_view()),
re_path('ping',views.ping),
re_path('list',channelsinfo_views.list),
re_path('add',channelsinfo_views.add),
......
# coding:utf-8
#解决广点通回调问题
import json
import hashlib
import requests
import time
import uuid
import redis
from django.conf import settings
import logging
channel_logger = logging.getLogger('channel_logger')
channel_cache = redis.Redis(host=settings.REDIS_HOST, port=settings.REDIS_PORT,db=settings.REDIS_DB,password=settings.REDIS_PASSWORD,decode_responses=True)
client_id=1109746007
account_id=10082158
client_secret="zyaMgamVli3wgaVI"
base_url="https://api.e.qq.com/oauth/token"
request_timeout=6
def md5(string):
if string:
md5=hashlib.md5()
md5.update(string.encode(encoding='utf-8'))
return md5.hexdigest()
return string
def get_base_url(method):
'''获取请求的基础url'''
access_token=channel_cache.get("access_token")
channel_logger.info("从缓存中拿到的access_token---")
channel_logger.info(access_token)
#刷新access_token
if not access_token:
refresh_token=channel_cache.get("refresh_token")
access_token=refresh_access_token(refresh_token)
try:
timestamp= int(time.time())
#不超过32位
nonce=str(uuid.uuid4()).replace("-","")
channel_logger.info("here")
callback_url="https://api.e.qq.com/v1.1/{0}?access_token={1}&timestamp={2}&nonce={3}".format(method,access_token,timestamp,nonce)
channel_logger.info("base_url")
channel_logger.info(callback_url)
return callback_url
except Exception as e:
channel_logger.info(e)
def refresh_access_token(refresh_token):
'''
刷新access_token
'''
global client_id
global client_secret
global base_url
data={
"client_id":client_id,
"client_secret":client_secret,
"grant_type":"refresh_token",
"refresh_token":refresh_token
}
try:
headers={'Content-Type': 'application/json'}
response=requests.get(base_url,headers=headers, params=data,verify=False,timeout=request_timeout).json()
data=response.get("data")
access_token=data.get("access_token")
refresh_token=data.get("refresh_token")
access_token_expires_in=int(data.get("access_token_expires_in"))-3600
channel_cache.set("access_token",access_token)
channel_cache.expire("access_token",access_token_expires_in)
#23小时时就过去取重新刷数据
channel_cache.set("refresh_token",refresh_token)
return access_token
except Exception as e:
channel_logger.info(e)
def get_user_action_set_id(action_type="IOS"):
'''
获取user_action_set_id
action_type:WEB, ANDROID, IOS, OFFLINE
'''
#这个只需要拿一次就够了
if action_type=="IOS":
#有用户行为(创建账户时创建???)
return 1109850574
#应用id
mobile_app_id="639234809"
if action_type.lower()=="android":
mobile_app_id="1101126509"
callback_url=get_base_url("user_action_sets/add")
data={
"account_id":account_id,
"type":action_type,
"mobile_app_id":mobile_app_id,
"name": "action_set",
"description": ""
}
channel_logger.info("获取user_action_set_id")
channel_logger.info(data)
try:
response=requests.post(callback_url, data=data,verify=False,timeout=request_timeout)
data=response.json()
channel_logger.info(data)
return data.get("data").get("user_action_set_id")
except Exception as e:
channel_logger.info(e)
def guangdiantong_active(record):
'''
广点通数据上报
'''
if record.platform.lower()=="iphone":
user_action_set_id=get_user_action_set_id()
user_info={"hash_idfa":md5(record.idfa)}
else:
user_action_set_id=get_user_action_set_id(action_type="ANDROID")
user_info={"hash_imei":md5(record.imei)}
#操作时间
timestamp= int(time.time())
data={
"account_id": account_id,
"user_action_set_id":user_action_set_id,
"actions":[{"action_time":timestamp,"user_id": user_info,"action_type":"ACTIVATE_APP"}],
}
channel_logger.info("上报数据---")
channel_logger.info(data)
#{'account_id': 11270422, 'actions': [{'action_time': 1567048947, 'user_id': {'hash_idfa': u'9F81AB87-791D-4541-B8F8-8C6AF55C8E98'}, 'action_type': 'ACTIVATE_APP'}], 'user_action_set_id': 1109728494}
#{u'message': u'JSON string is malformed.', u'code': 51002}
callback_url=get_base_url("user_actions/add")
try:
headers={'Content-Type': 'application/json'}
response=requests.post(callback_url,headers=headers,verify=False,data=json.dumps(data),timeout=request_timeout)
data=response.json()
#code不为0,提交失败
if data.get("code"):
channel_logger.info(data)
else:
channel_logger.info("上报成功---")
except Exception as e:
channel_logger.info(e)
#获取access_token并更新到缓存
def get_token(authorization_code):
'''请求access_token和refresh_token'''
#portrait String https://heras.igengmei.com/doctor/2019/03/26/1703/c8f75ccf34b0-thumb
authorization_code="f565abb22414da02b1dbc8267a79c873"
global client_id
global client_secret
global base_url
channel_logger.info("拿到的redis实例---")
channel_logger.info(channel_cache)
data={
"client_id":client_id,
"client_secret":client_secret,
"authorization_code":authorization_code,
"grant_type":"authorization_code",
"redirect_uri":"http://channels.igengmei.com/like/callback",
}
channel_logger.info("收到的数据---")
channel_logger.info(data)
try:
response=requests.get("https://api.e.qq.com/oauth/token", params=data,verify=False,timeout=request_timeout)
channel_logger.info(response.text)
data=response.json().get("data")
#data=json.loads('{"authorizer_info":{"account_uin":2409388915,"account_id":11270422,"scope_list":["user_actions"],"wechat_account_id":""},"access_token":"360994df1652cea7b9c97308f4529cf5","refresh_token":"8ed65a1ea6890ca9dc19fef3bf76a4e2","access_token_expires_in":86400,"refresh_token_expires_in":2592000}')
access_token=data.get("access_token")
refresh_token=data.get("refresh_token")
channel_logger.info("获得的access_token--")
channel_logger.info(access_token)
access_token_expires_in=int(data.get("access_token_expires_in"))
#减少1小时
access_token_expires_in-=3600
result=channel_cache.set("access_token",access_token)
time_result=channel_cache.expire("access_token",access_token_expires_in)
channel_logger.info("缓存结果---")
channel_logger.info(result)
channel_logger.info(time_result)
#23小时时就过去取重新刷数据
channel_cache.set("refresh_token",refresh_token)
except Exception as e:
channel_logger.info(e)
\ No newline at end of file
......@@ -19,10 +19,10 @@ from celery import shared_task
from django.db.models import Q
import urllib
import logging
from .utils.guangdiantong import get_token,guangdiantong_active
channel_logger = logging.getLogger('channel_logger')
url_logger=logging.getLogger('url_logger')
def ping(request):
'''健康检查'''
channel_logger.info("ping------")
......@@ -142,6 +142,10 @@ class Statistic(View):
#激活
PromotionChannel.activate(record,kwargs)
#此处特殊处理(广点通数据上报)
if record.appid.startswith("guangdiantong"):
guangdiantong_active(record)
#无回调,不做处理
if record.callback_type==CHANNEL_CALLBACK_TYPE.NO_CALLBACK.value[0]:
return
......@@ -170,7 +174,7 @@ class Statistic(View):
except Exception as e:
channel_logger.info(e)
channel_logger.info("激活完成---")
return {'error':0, 'msg':''}
......@@ -205,8 +209,14 @@ class DoubleCheck(View):
return JsonResponse(result)
class CallBack(View):
'''
广点通处理回调---
'''
def get(self,request):
authorization_code=request.GET.get("authorization_code")
get_token(authorization_code)
return JsonResponse(data={"data":""})
class Channel(View):
'''
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment