You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.4 KiB
Python
107 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
# @Time : 2024/11/12 16:10
|
|
# @Author : zhaoxiangpeng
|
|
# @File : get_cookie.py
|
|
# 用来获取session_id
|
|
|
|
import asyncio
|
|
import json
|
|
from random import random
|
|
|
|
import redis
|
|
import aiohttp
|
|
|
|
|
|
class GetSessionID:
|
|
__redis_cli = None
|
|
|
|
def __init__(self, redis_uri: str, pool_key: str, ttl: int = None, **kwargs):
|
|
self.redis_uri = redis_uri
|
|
self.pool_key = pool_key
|
|
self.ttl = ttl
|
|
|
|
@staticmethod
|
|
async def new_session_id() -> dict:
|
|
session = aiohttp.ClientSession()
|
|
resp = await session.get(
|
|
'http://cssci.nju.edu.cn/index.html',
|
|
headers={
|
|
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
|
|
'host': 'cssci.nju.edu.cn'
|
|
}
|
|
)
|
|
assert resp.status == 200
|
|
resp = await session.post(
|
|
'http://cssci.nju.edu.cn/control/controllers.php',
|
|
headers={
|
|
'content-type': 'application/x-www-form-urlencoded',
|
|
'host': 'cssci.nju.edu.cn',
|
|
'origin': 'http://cssci.nju.edu.cn',
|
|
'referer': 'http://cssci.nju.edu.cn/index.html',
|
|
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'
|
|
},
|
|
data=dict(control='user_control', action='check_user_online', rand=random()),
|
|
)
|
|
assert resp.status == 200
|
|
# 把cookiejar转为dict并返回
|
|
cookie_obj = dict()
|
|
cookie_jar = session.cookie_jar.filter_cookies(resp.url)
|
|
for key, jar in cookie_jar.items():
|
|
cookie_obj.setdefault(jar.key, jar.value)
|
|
# 关闭session
|
|
await session.close()
|
|
return cookie_obj
|
|
|
|
@property
|
|
def redis_cli(self):
|
|
if self.__class__.__redis_cli is None:
|
|
self.__class__.__redis_cli = redis.asyncio.Redis.from_url(
|
|
self.redis_uri,
|
|
decode_responses=True
|
|
)
|
|
return self.__class__.__redis_cli
|
|
|
|
async def set_cookie_to_redis(self, val):
|
|
result = await self.redis_cli.setex(self.pool_key, time=self.ttl, value=val)
|
|
return result
|
|
|
|
async def get_cookie_from_redis(self, to_dict=True):
|
|
"""
|
|
:param to_dict: 是否从字符串转为dict
|
|
:return:
|
|
"""
|
|
cookie_str = await self.redis_cli.get(self.pool_key)
|
|
if not cookie_str:
|
|
return cookie_str
|
|
if to_dict:
|
|
return json.loads(cookie_str)
|
|
return cookie_str
|
|
|
|
async def get_cookie_to_redis(self) -> dict:
|
|
"""
|
|
直接获取cookie并塞到redis
|
|
:return:
|
|
"""
|
|
cookie_obj = await self.new_session_id()
|
|
await self.set_cookie_to_redis(val=json.dumps(cookie_obj, ensure_ascii=False))
|
|
return cookie_obj
|
|
|
|
async def test(self):
|
|
from loguru import logger
|
|
cookie_obj = await self.new_session_id()
|
|
logger.info("""
|
|
cookie: %s""" % cookie_obj)
|
|
res = await self.set_cookie_to_redis(val=json.dumps(cookie_obj, ensure_ascii=False))
|
|
logger.info("""
|
|
插入: %s""" % res)
|
|
res = await self.get_cookie_from_redis()
|
|
logger.info("""
|
|
获取: %s""" % res)
|
|
|
|
def main(self):
|
|
asyncio.run(self.test())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
GetSessionID().main()
|