|
@@ -7,44 +7,49 @@
|
|
import redis
|
|
import redis
|
|
import pymysql
|
|
import pymysql
|
|
from common.common import Common
|
|
from common.common import Common
|
|
|
|
+
|
|
# from common import Common
|
|
# from common import Common
|
|
|
|
|
|
|
|
+
|
|
class MysqlHelper:
|
|
class MysqlHelper:
|
|
@classmethod
|
|
@classmethod
|
|
def connect_mysql(cls, env, machine):
|
|
def connect_mysql(cls, env, machine):
|
|
- if machine == 'aliyun_hk':
|
|
|
|
|
|
+ if machine == "aliyun_hk":
|
|
# 创建一个 Connection 对象,代表了一个数据库连接
|
|
# 创建一个 Connection 对象,代表了一个数据库连接
|
|
connection = pymysql.connect(
|
|
connection = pymysql.connect(
|
|
- host="rm-j6cz4c6pt96000xi3.mysql.rds.aliyuncs.com",# 数据库IP地址,内网地址
|
|
|
|
|
|
+ host="rm-j6cz4c6pt96000xi3.mysql.rds.aliyuncs.com", # 数据库IP地址,内网地址
|
|
# host="rm-j6cz4c6pt96000xi3lo.mysql.rds.aliyuncs.com",# 数据库IP地址,外网地址
|
|
# host="rm-j6cz4c6pt96000xi3lo.mysql.rds.aliyuncs.com",# 数据库IP地址,外网地址
|
|
- port=3306, # 端口号
|
|
|
|
- user="crawler", # mysql用户名
|
|
|
|
- passwd="crawler123456@", # mysql用户登录密码
|
|
|
|
- db="piaoquan-crawler" , # 数据库名
|
|
|
|
|
|
+ port=3306, # 端口号
|
|
|
|
+ user="crawler", # mysql用户名
|
|
|
|
+ passwd="crawler123456@", # mysql用户登录密码
|
|
|
|
+ db="piaoquan-crawler", # 数据库名
|
|
# 如果数据库里面的文本是utf8编码的,charset指定是utf8
|
|
# 如果数据库里面的文本是utf8编码的,charset指定是utf8
|
|
- charset = "utf8")
|
|
|
|
- elif env == 'prod':
|
|
|
|
|
|
+ charset="utf8",
|
|
|
|
+ )
|
|
|
|
+ elif env == "prod":
|
|
# 创建一个 Connection 对象,代表了一个数据库连接
|
|
# 创建一个 Connection 对象,代表了一个数据库连接
|
|
connection = pymysql.connect(
|
|
connection = pymysql.connect(
|
|
- host="rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com",# 数据库IP地址,内网地址
|
|
|
|
|
|
+ host="rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com", # 数据库IP地址,内网地址
|
|
# host="rm-bp1159bu17li9hi94ro.mysql.rds.aliyuncs.com",# 数据库IP地址,外网地址
|
|
# host="rm-bp1159bu17li9hi94ro.mysql.rds.aliyuncs.com",# 数据库IP地址,外网地址
|
|
- port=3306, # 端口号
|
|
|
|
- user="crawler", # mysql用户名
|
|
|
|
- passwd="crawler123456@", # mysql用户登录密码
|
|
|
|
- db="piaoquan-crawler" , # 数据库名
|
|
|
|
|
|
+ port=3306, # 端口号
|
|
|
|
+ user="crawler", # mysql用户名
|
|
|
|
+ passwd="crawler123456@", # mysql用户登录密码
|
|
|
|
+ db="piaoquan-crawler", # 数据库名
|
|
# 如果数据库里面的文本是utf8编码的,charset指定是utf8
|
|
# 如果数据库里面的文本是utf8编码的,charset指定是utf8
|
|
- charset = "utf8")
|
|
|
|
|
|
+ charset="utf8",
|
|
|
|
+ )
|
|
else:
|
|
else:
|
|
# 创建一个 Connection 对象,代表了一个数据库连接
|
|
# 创建一个 Connection 对象,代表了一个数据库连接
|
|
connection = pymysql.connect(
|
|
connection = pymysql.connect(
|
|
- host="rm-bp1k5853td1r25g3n690.mysql.rds.aliyuncs.com",# 数据库IP地址,内网地址
|
|
|
|
|
|
+ host="rm-bp1k5853td1r25g3n690.mysql.rds.aliyuncs.com", # 数据库IP地址,内网地址
|
|
# host="rm-bp1k5853td1r25g3ndo.mysql.rds.aliyuncs.com", # 数据库IP地址,外网地址
|
|
# host="rm-bp1k5853td1r25g3ndo.mysql.rds.aliyuncs.com", # 数据库IP地址,外网地址
|
|
port=3306, # 端口号
|
|
port=3306, # 端口号
|
|
user="crawler", # mysql用户名
|
|
user="crawler", # mysql用户名
|
|
passwd="crawler123456@", # mysql用户登录密码
|
|
passwd="crawler123456@", # mysql用户登录密码
|
|
db="piaoquan-crawler", # 数据库名
|
|
db="piaoquan-crawler", # 数据库名
|
|
# 如果数据库里面的文本是utf8编码的,charset指定是utf8
|
|
# 如果数据库里面的文本是utf8编码的,charset指定是utf8
|
|
- charset="utf8")
|
|
|
|
|
|
+ charset="utf8",
|
|
|
|
+ )
|
|
|
|
|
|
return connection
|
|
return connection
|
|
|
|
|
|
@@ -91,34 +96,35 @@ class MysqlHelper:
|
|
# 关闭数据库连接
|
|
# 关闭数据库连接
|
|
connect.close()
|
|
connect.close()
|
|
|
|
|
|
|
|
+
|
|
class RedisHelper:
|
|
class RedisHelper:
|
|
@classmethod
|
|
@classmethod
|
|
def connect_redis(cls, env, machine):
|
|
def connect_redis(cls, env, machine):
|
|
- if machine == 'aliyun_hk':
|
|
|
|
|
|
+ if machine == "aliyun_hk":
|
|
redis_pool = redis.ConnectionPool(
|
|
redis_pool = redis.ConnectionPool(
|
|
# host='r-bp154bpw97gptefiqk.redis.rds.aliyuncs.com', # 内网地址
|
|
# host='r-bp154bpw97gptefiqk.redis.rds.aliyuncs.com', # 内网地址
|
|
- host='r-bp154bpw97gptefiqkpd.redis.rds.aliyuncs.com', # 外网地址
|
|
|
|
|
|
+ host="r-bp154bpw97gptefiqkpd.redis.rds.aliyuncs.com", # 外网地址
|
|
port=6379,
|
|
port=6379,
|
|
db=2,
|
|
db=2,
|
|
- password='Qingqu2019'
|
|
|
|
|
|
+ password="Qingqu2019",
|
|
)
|
|
)
|
|
redis_conn = redis.Redis(connection_pool=redis_pool)
|
|
redis_conn = redis.Redis(connection_pool=redis_pool)
|
|
- elif env == 'prod':
|
|
|
|
|
|
+ elif env == "prod":
|
|
redis_pool = redis.ConnectionPool(
|
|
redis_pool = redis.ConnectionPool(
|
|
- host='r-bp1mb0v08fqi4hjffu.redis.rds.aliyuncs.com', # 内网地址
|
|
|
|
|
|
+ host="r-bp1mb0v08fqi4hjffu.redis.rds.aliyuncs.com", # 内网地址
|
|
# host='r-bp1mb0v08fqi4hjffupd.redis.rds.aliyuncs.com', # 外网地址
|
|
# host='r-bp1mb0v08fqi4hjffupd.redis.rds.aliyuncs.com', # 外网地址
|
|
port=6379,
|
|
port=6379,
|
|
db=2,
|
|
db=2,
|
|
- password='Qingqu2019'
|
|
|
|
|
|
+ password="Qingqu2019",
|
|
)
|
|
)
|
|
redis_conn = redis.Redis(connection_pool=redis_pool)
|
|
redis_conn = redis.Redis(connection_pool=redis_pool)
|
|
else:
|
|
else:
|
|
redis_pool = redis.ConnectionPool(
|
|
redis_pool = redis.ConnectionPool(
|
|
# host='r-bp154bpw97gptefiqk.redis.rds.aliyuncs.com', # 内网地址
|
|
# host='r-bp154bpw97gptefiqk.redis.rds.aliyuncs.com', # 内网地址
|
|
- host='r-bp154bpw97gptefiqkpd.redis.rds.aliyuncs.com', # 外网地址
|
|
|
|
|
|
+ host="r-bp154bpw97gptefiqkpd.redis.rds.aliyuncs.com", # 外网地址
|
|
port=6379,
|
|
port=6379,
|
|
db=2,
|
|
db=2,
|
|
- password='Qingqu2019'
|
|
|
|
|
|
+ password="Qingqu2019",
|
|
)
|
|
)
|
|
redis_conn = redis.Redis(connection_pool=redis_pool)
|
|
redis_conn = redis.Redis(connection_pool=redis_pool)
|
|
return redis_conn
|
|
return redis_conn
|
|
@@ -139,20 +145,52 @@ class RedisHelper:
|
|
return redis_conn.rpop(machine)
|
|
return redis_conn.rpop(machine)
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
-if __name__ == "__main__":
|
|
|
|
- # sql_statement = f"INSERT INTO crawler_user ( user_id, out_user_id, out_user_name, out_avatar_url, platform, tag) " \
|
|
|
|
- # f"VALUES ('6282398', 'out_uid_003', 'out_user_name', '', 'xiaoniangao', 'xiaoniangao_play')"
|
|
|
|
- # edit_data = MysqlHelper.edit_data(sql=sql_statement)
|
|
|
|
- # print(edit_data)
|
|
|
|
-
|
|
|
|
- # get_data = MysqlHelper.get_values("demo", "youtube", "select * from crawler_user", "dev", "local")
|
|
|
|
- # print(get_data)
|
|
|
|
- print(RedisHelper.connect_redis("prod", "aliyun"))
|
|
|
|
- # RedisHelper.redis_push("dev", "local", "test1")
|
|
|
|
- # RedisHelper.redis_push("dev", "local", "test2")
|
|
|
|
-
|
|
|
|
- # print(RedisHelper.redis_pop("dev", "local"))
|
|
|
|
-
|
|
|
|
- pass
|
|
|
|
-
|
|
|
|
|
|
+class RedisClient(object):
|
|
|
|
+ """
|
|
|
|
+ Redis client by python
|
|
|
|
+ Todo 如果 Redis 服务挂了,怎么做能够不影响业务
|
|
|
|
+ 思路, 每次使用 redis 接口前先判断是否连接成功,如果连接失败则跳过 redis ,不影响全局
|
|
|
|
+ """
|
|
|
|
+
|
|
|
|
+ def __init__(self):
|
|
|
|
+ self.pool = None
|
|
|
|
+ self.host = "r-bp1mb0v08fqi4hjffu.redis.rds.aliyuncs.com"
|
|
|
|
+ self.port = 6379
|
|
|
|
+ self.db = 2
|
|
|
|
+ self.password = "Wqsd@2019"
|
|
|
|
+
|
|
|
|
+ def connect(self):
|
|
|
|
+ """
|
|
|
|
+ connect to redis server
|
|
|
|
+ :return: bool
|
|
|
|
+ """
|
|
|
|
+ try:
|
|
|
|
+ self.pool = redis.Redis(
|
|
|
|
+ host=self.host, port=self.port, db=self.db, password=self.password
|
|
|
|
+ )
|
|
|
|
+ return True
|
|
|
|
+ except Exception as e:
|
|
|
|
+ print("connect to redis fail, the reason is {}".format(e))
|
|
|
|
+ return False
|
|
|
|
+
|
|
|
|
+ def select(self, key):
|
|
|
|
+ """
|
|
|
|
+ read info from redis
|
|
|
|
+ :return:
|
|
|
|
+ """
|
|
|
|
+ return self.pool.get(key)
|
|
|
|
+
|
|
|
|
+ def insert(self, key, value, expire_time):
|
|
|
|
+ """
|
|
|
|
+ insert info from redis
|
|
|
|
+ :return:
|
|
|
|
+ """
|
|
|
|
+ self.pool.set(key, value, expire_time)
|
|
|
|
+
|
|
|
|
+ def delete(self, key):
|
|
|
|
+ """
|
|
|
|
+ delete key
|
|
|
|
+ :param key:
|
|
|
|
+ :return:
|
|
|
|
+ """
|
|
|
|
+ self.pool.delete(key)
|