|
@@ -3,56 +3,78 @@ from settings import using_config
|
|
|
import tornado.ioloop
|
|
|
import tornado.web
|
|
|
import json
|
|
|
+from wechat_api.get_wechat_info import WechatApi
|
|
|
+from wechat_action.login_ad import LogIn
|
|
|
from wechat_action import sql_tools
|
|
|
+import threading
|
|
|
+from web_module import user_action
|
|
|
+from sqlalchemy import Table
|
|
|
+import pickle
|
|
|
+
|
|
|
# TODO:需要添加上supervisor,来维护进程
|
|
|
# TODO:有时间需要对tornado进行改进
|
|
|
|
|
|
|
|
|
db = DB(config=using_config)
|
|
|
+wechat_cookies_table = Table('wechat_cookies', db.metadata,
|
|
|
+ autoload=True, autoload_with=db.engine)
|
|
|
+layout_typesetting_table = Table('layout_typesetting', db.metadata,
|
|
|
+ autoload=True, autoload_with=db.engine)
|
|
|
|
|
|
|
|
|
# 1.实现本机服务
|
|
|
# 2.实现线上docker-selenium服务
|
|
|
|
|
|
-class login(tornado.web.RequestHandler):
|
|
|
- # 0.登录,
|
|
|
- # 1.成功---
|
|
|
- # 2.不超过,返回一个二维码
|
|
|
- def get(self):
|
|
|
- self.write("Hello, world")
|
|
|
-
|
|
|
- def post(self):
|
|
|
- # 利用request属性
|
|
|
- # 取出客户端提交的json字符串
|
|
|
- jsonbyte = self.request.body
|
|
|
- print('二进制格式json字符串:', jsonbyte)
|
|
|
- jsonstr = jsonbyte.decode('utf8') # 解码,二进制转为字符串
|
|
|
- print('json字符串:', jsonstr)
|
|
|
-
|
|
|
- # 从json字符串转换为json对象,然后利用json对象提供的api
|
|
|
- # 从json字符串中取出我想要的内容(解析json字符串)
|
|
|
- jsonobj = json.loads(jsonstr) # 将字符串转为json对象
|
|
|
- day = jsonobj.get('day') # 就可以用api取值
|
|
|
- title = jsonobj.get('title')
|
|
|
- print('day: ', day, ', title: ', title)
|
|
|
|
|
|
- self.write('hello post')
|
|
|
-
|
|
|
-
|
|
|
-class create_ad_plan(tornado.web.RequestHandler):
|
|
|
+class create_ad_plan_remote(tornado.web.RequestHandler):
|
|
|
# 1.批量创建计划
|
|
|
# 返回创建计划是否已经开始
|
|
|
+ def get(self):
|
|
|
+ pass
|
|
|
+
|
|
|
def post(self):
|
|
|
# 1.
|
|
|
pass
|
|
|
|
|
|
|
|
|
-class create_ad_show(tornado.web.RequestHandler):
|
|
|
+class create_ad_layout_remote(tornado.web.RequestHandler):
|
|
|
# 1.批量创建落地页
|
|
|
+ def post(self):
|
|
|
+ user_id = self.get_argument("user_id", None)
|
|
|
+ layout_name = self.get_argument("layout_name", None)
|
|
|
+ # wechat_json :[{'service_name':'one','wechat_name':''},{'service_name':'','wechat_name':''}]
|
|
|
+ wechat_json = self.get_argument('wechat_json', None)
|
|
|
+ log_ad, cookie_canuse = ad_human_info.refresh_wechat_cookies(self, user_id=user_id)
|
|
|
+ threading.Thread(target=user_action.create_layout,
|
|
|
+ args=(user_id, layout_name, wechat_json, log_ad, db, cookie_canuse)).start()
|
|
|
+
|
|
|
+
|
|
|
+class create_ad_layout_local(tornado.web.RequestHandler):
|
|
|
+ def post(self):
|
|
|
+ user_id = self.get_argument("user_id", None)
|
|
|
+ layout_typesetting = self.get_argument("layout_typesetting", None)
|
|
|
+ layout_name = self.get_argument("layout_name", None)
|
|
|
+ print(user_id, layout_typesetting, layout_name)
|
|
|
+ sql_session = db.DBSession()
|
|
|
+ if user_id is None or layout_name is None or layout_typesetting is None:
|
|
|
+ self.write(json.dumps({'status': {'msg': 'url parameter error', "RetCode": 400}}))
|
|
|
+ return
|
|
|
+ # 落地页名字精确到毫秒,默认是全局唯一
|
|
|
+ # TODO:检查一下layout--内容 有无问题-----和前端确定一下
|
|
|
+
|
|
|
+ layout_typesetting_info = {'user_id': user_id, 'name': layout_name, 'typesetting': layout_typesetting}
|
|
|
+ layout_typesetting_inserte = sql_tools.save_layout_typesetting_info(
|
|
|
+ layout_typesetting_info=layout_typesetting_info,
|
|
|
+ table_layout_typesetting=layout_typesetting_table)
|
|
|
+ sql_session.execute(layout_typesetting_inserte)
|
|
|
+ sql_session.commit()
|
|
|
+ self.write(json.dumps({'status': {'msg': 'success', "RetCode": 200}}, ensure_ascii=False))
|
|
|
+
|
|
|
+
|
|
|
+class create_ad_plan_local(tornado.web.RequestHandler):
|
|
|
pass
|
|
|
|
|
|
|
|
|
-# TODO:数据库创建一批虚拟数据,供明天测试使用起
|
|
|
# TODO:wechat_info,human_info 这两张表有空时需要进行对应改进
|
|
|
class ad_status(tornado.web.RequestHandler):
|
|
|
# 1.创建情况
|
|
@@ -60,16 +82,61 @@ class ad_status(tornado.web.RequestHandler):
|
|
|
|
|
|
|
|
|
class ad_human_info(tornado.web.RequestHandler):
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def refresh_wechat_cookies(tornado_web, user_id):
|
|
|
+ # 1.返回二维码链接
|
|
|
+ # ----1.查看cookie是否可用
|
|
|
+ sql_session = db.DBSession()
|
|
|
+ cookie_db = sql_tools.get_wechat_cookies(sql_session, user_id=user_id)
|
|
|
+
|
|
|
+ # 进行登录操作
|
|
|
+ log_ad = LogIn()
|
|
|
+
|
|
|
+ # 使driver可以使用
|
|
|
+ cookie_canuse = False
|
|
|
+ if cookie_db:
|
|
|
+ cookie_db = pickle.loads(cookie_db)
|
|
|
+
|
|
|
+ # TODO:log 日志需要进行对应配置
|
|
|
+ if not log_ad.wechat_cookies_check_alive(cookie_db):
|
|
|
+ # cookie 不能使用
|
|
|
+ wechat_code = log_ad.log_in()
|
|
|
+ tornado_web.write(json.dumps({'status': {'msg': 'success', "RetCode": 200},
|
|
|
+ 'wechat_code': wechat_code}))
|
|
|
+ print('cookie can not use')
|
|
|
+ else:
|
|
|
+ # cookie 可以继续使用
|
|
|
+ cookie_canuse = True
|
|
|
+ log_ad.driver.get('https://a.weixin.qq.com/index.html')
|
|
|
+ tornado_web.write(json.dumps({'status': {'msg': 'success', "RetCode": 200}}))
|
|
|
+ else:
|
|
|
+ # cookie 不能使用
|
|
|
+ wechat_code = log_ad.log_in()
|
|
|
+ tornado_web.write(json.dumps({'status': {'msg': 'success', "RetCode": 200},
|
|
|
+ 'wechat_code': wechat_code}))
|
|
|
+ return log_ad, cookie_canuse
|
|
|
+
|
|
|
# 1.人群包获取
|
|
|
def get(self):
|
|
|
+ # TODO:添加分页
|
|
|
+
|
|
|
# 0.是否刷新
|
|
|
# 1.获取userid,以及是否刷新
|
|
|
user_id = self.get_argument("user_id", None)
|
|
|
is_refresh = self.get_argument("is_refresh", None)
|
|
|
- if is_refresh:
|
|
|
- # 1.返回二维码链接
|
|
|
- # ----1.查看cookie是否可用
|
|
|
- pass
|
|
|
+ print(user_id, is_refresh)
|
|
|
+ if user_id is None or is_refresh is None:
|
|
|
+ self.write(json.dumps({'status': {'msg': 'url parameter error', "RetCode": 400}}))
|
|
|
+ return
|
|
|
+ sql_session = db.DBSession()
|
|
|
+ # TODO:一个涉及到selenium-driver的请求-生命周期.----看一下tornado是怎么处理请求的生命周期
|
|
|
+ if int(is_refresh) == 1:
|
|
|
+ log_ad, cookie_canuse = self.refresh_wechat_cookies(self, user_id=user_id)
|
|
|
+ threading.Thread(target=user_action.get_human_info,
|
|
|
+ args=(
|
|
|
+ user_id, log_ad, db, cookie_canuse)).start()
|
|
|
+
|
|
|
else:
|
|
|
# 1.查看是否在刷新,
|
|
|
# 在刷新中,
|
|
@@ -79,25 +146,62 @@ class ad_human_info(tornado.web.RequestHandler):
|
|
|
# 不在刷新
|
|
|
# 返回对应数据
|
|
|
# 2.获取userid对应数据
|
|
|
- sql_session = db.DBSession()
|
|
|
- sql_tools.get_human_info(sql_session=sql_session,user_id=user_id)
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- pass
|
|
|
+ result = sql_tools.get_human_info(sql_session=sql_session, user_id=user_id)
|
|
|
+ result = [json.loads(x) for x in result]
|
|
|
+ print(result)
|
|
|
+ self.write(json.dumps({'status': {'msg': 'success', "RetCode": 200},
|
|
|
+ 'human_info': result}, ensure_ascii=False))
|
|
|
|
|
|
|
|
|
-class ad_wechat_info():
|
|
|
+class ad_wechat_info(tornado.web.RequestHandler):
|
|
|
# 1.公众号相关信息获取
|
|
|
def get(self):
|
|
|
- pass
|
|
|
+ # TODO:添加分页
|
|
|
+
|
|
|
+ # 0.是否刷新
|
|
|
+ # 1.获取userid,以及是否刷新
|
|
|
+ user_id = self.get_argument("user_id", None)
|
|
|
+ is_refresh = self.get_argument("is_refresh", None)
|
|
|
+ print(user_id, is_refresh)
|
|
|
+ if user_id is None or is_refresh is None:
|
|
|
+ self.write(json.dumps({'status': {'msg': 'url parameter error', "RetCode": 400}}))
|
|
|
+ return
|
|
|
+ sql_session = db.DBSession()
|
|
|
+ # TODO:一个涉及到selenium-driver的请求-生命周期.----看一下tornado是怎么处理请求的生命周期
|
|
|
+ if int(is_refresh) == 1:
|
|
|
+ log_ad, cookie_canuse = ad_human_info.refresh_wechat_cookies(self, user_id=user_id)
|
|
|
+ threading.Thread(target=user_action.get_human_info,
|
|
|
+ args=(
|
|
|
+ user_id, log_ad, db, cookie_canuse)).start()
|
|
|
+ else:
|
|
|
+ # 1.查看是否在刷新,
|
|
|
+ # 在刷新中,
|
|
|
+ # 返回正在刷新
|
|
|
+
|
|
|
+ # -------不管上面逻辑让他们多刷新几次
|
|
|
+ # 不在刷新
|
|
|
+ # 返回对应数据
|
|
|
+ # 2.获取userid对应数据
|
|
|
+ result = sql_tools.get_wechat_info(sql_session=sql_session, user_id=user_id)
|
|
|
+ result_list = []
|
|
|
+ for _ in result:
|
|
|
+ service_name, wechat_name = _
|
|
|
+ result_list.append({'service_name': service_name, 'wechat_name': wechat_name})
|
|
|
+ print(result_list)
|
|
|
+ self.write(json.dumps({'status': {'msg': 'success', "RetCode": 200},
|
|
|
+ 'wechat_info': result_list}, ensure_ascii=False))
|
|
|
|
|
|
|
|
|
def make_app():
|
|
|
return tornado.web.Application([
|
|
|
- (r"/create_ad_plan", create_ad_plan),
|
|
|
- (r"/create_ad_show", create_ad_show),
|
|
|
- ], debug=True)
|
|
|
+ ("/create_ad_plan_local", create_ad_plan_local),
|
|
|
+ ("/create_ad_layout_local", create_ad_layout_local),
|
|
|
+ ("/create_ad_plan_remote", create_ad_plan_remote),
|
|
|
+ ("/create_ad_layout_remote", create_ad_layout_remote),
|
|
|
+ ("/ad_human_info", ad_human_info),
|
|
|
+ ("/ad_wechat_info", ad_wechat_info),
|
|
|
+ ("/ad_status", ad_status)
|
|
|
+ ], debug=True, autoreload=True)
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|