初始化项目

This commit is contained in:
chenwj113 2023-05-21 13:07:44 +08:00
parent 16e6606d9c
commit efc52dbcdf
16 changed files with 539 additions and 0 deletions

26
Dockerfile Normal file
View File

@ -0,0 +1,26 @@
FROM python:3.8.6-slim as build
RUN mkdir /install
WORKDIR /install
COPY requirements.txt .
RUN sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list
# RUN apt-get update \
# && apt-get install gcc -y \
# && apt-get clean
RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ --prefix=/install
# 应用启动
FROM python:3.8.6-slim
COPY --from=build /install /usr/local
COPY . /app
WORKDIR /app
CMD ["python", "main.py"]

9
docker-compose.yml Normal file
View File

@ -0,0 +1,9 @@
version: "3.5"
services:
schedule_tasks:
build: .
image: "schedule_tasks"
container_name: "schedule_tasks"
restart: always
# ports:
# - "18000:8000"

119
lottery/__init__.py Normal file
View File

@ -0,0 +1,119 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import re
from datetime import date, datetime
from model.pls import PLS
from model.plw import PLW
from model.sd import SD
from model.klb import KLB
from model.football_sfc import FootballSfc
class Lottery(object):
"""
Lottery Base Object
"""
def __init__(self, lottery_type='pls'):
self._pat1 = re.compile('开奖日期:(\d+)年(\d+)月(\d+)日.*')
self._pat2 = re.compile('开奖日期:(\d+)-(\d+)-(\d+)\s.*')
self._lottery_type = lottery_type
self.db = self._get_db_session()
if lottery_type.lower() == 'pls':
self._Model = PLS
elif lottery_type.lower() == 'sd':
self._Model = SD
elif lottery_type.lower() == 'plw':
self._Model = PLW
elif lottery_type.lower() == 'sfc':
self._Model = FootballSfc
elif lottery_type.lower() == 'klb':
self._Model = KLB
else:
raise Exception("未知的lottery_type")
def _get_db_session(self):
_engine = create_engine("mysql+pymysql://test:123456@172.17.0.1/lottery?charset=utf8", pool_pre_ping=True, pool_recycle=3600)
_DbSession = sessionmaker(bind=_engine)
return _DbSession()
def __enter__(self):
return self
def __exit__(self, type, value, trace):
print("close the db session...")
self.db.close()
def get_last_id_by_sum_num(self, sum_num):
"""
根据总和获取id
"""
last_id = self.db.query(self._Model.id).filter_by(sum_num=sum_num).order_by(self._Model.id.desc()).first()
if last_id is not None:
return last_id[0]
return None
def insert(self, draw_issue, draw_date, draw_code, **kwargs):
"""
插入3d排列三排列五
"""
result = self.db.query(self._Model).filter_by(draw_issue=draw_issue).first()
if result is None:
record = self._Model()
record.draw_issue = draw_issue
record.draw_code = draw_code
record.created_at = datetime.now()
m1 = re.match(self._pat1, draw_date)
m2 = re.match(self._pat2, draw_date)
if m1 or m2 :
if m1:
record.draw_date = date(int(m1.group(1)), int(m1.group(2)), int(m1.group(3)))
else:
record.draw_date = date(int(m2.group(1)), int(m2.group(2)), int(m2.group(3)))
else:
raise Exception(f"issue:{draw_issue}数据写入失败。。。")
# 如果是排列五、排列五和3D
if isinstance(record, (PLS, SD, PLW)):
record.sum_num = sum(map(int, draw_code))
record.code_small = len(list(filter(lambda x: True if int(x) < 5 else False, draw_code)))
record.code_big = len(list(filter(lambda x: True if int(x) >= 5 else False, draw_code)))
record.code_single = len(list(filter(lambda x: True if int(x) % 2 == 1 else False, draw_code)))
record.code_double = len(list(filter(lambda x: True if int(x) % 2 == 0 else False, draw_code)))
if isinstance(record, (PLS, SD)):
record.hundred = draw_code[0]
record.ten = draw_code[1]
record.one = draw_code[2]
record.sum_hundred_one = int(draw_code[2]) + int(draw_code[0])
record.sum_hundred_ten = int(draw_code[2]) + int(record.draw_code[1])
record.sum_ten_one = int(record.draw_code[1]) + int(record.draw_code[0])
if len(set(draw_code)) == 2:
record.group_type = 3
elif len(set(draw_code)) == 3:
record.group_type = 6
else:
record.group_type = 1
elif isinstance(record, PLW):
record.ten_thousand = draw_code[0]
record.thousand = draw_code[1]
record.hundred = draw_code[2]
record.ten = draw_code[3]
record.one = draw_code[4]
record.sum_hundred_one = int(draw_code[2]) + int(draw_code[4])
record.sum_hundred_ten = int(draw_code[2]) + int(record.draw_code[3])
record.sum_ten_one = int(record.draw_code[3]) + int(record.draw_code[4])
elif isinstance(record, FootballSfc):
record.open_result = draw_code
record.sale_money = kwargs["sfc_sale"]
record.r9_sale_money = kwargs["rj_sale"]
record.award_1_money = kwargs["award_1_money"]
record.award_1_num = kwargs["award_1_num"]
record.award_2_money = kwargs["award_2_money"]
record.award_2_num = kwargs["award_2_num"]
record.r9_award_money = kwargs["r9_award_money"]
record.r9_award_num = kwargs["r9_award_num"]
self.db.add(record)
self.db.commit()
return record.id
else:
print(f"{draw_issue}开奖结果已经写入数据库中...")
return None

View File

@ -0,0 +1,18 @@
# coding: utf-8
from sqlalchemy import Column, DateTime, Integer, text
from sqlalchemy.dialects.mysql import VARCHAR
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class FootballLeague(Base):
__tablename__ = 'football_league'
__table_args__ = {'comment': '足球联赛信息表'}
id = Column(Integer, primary_key=True)
league_id = Column(Integer, comment='联赛id')
league_name = Column(VARCHAR(255), comment='联赛名称')
league_all_name = Column(VARCHAR(255), comment='联赛全称')
created_at = Column(DateTime, server_default=text("CURRENT_TIMESTAMP"))

View File

@ -0,0 +1,31 @@
# coding: utf-8
from sqlalchemy import Column, DateTime, Integer, String, text
from sqlalchemy.dialects.mysql import VARCHAR
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class FootballMatch(Base):
__tablename__ = 'football_match'
__table_args__ = {'comment': '足球比赛表'}
id = Column(Integer, primary_key=True)
issue = Column(VARCHAR(255))
match_date = Column(DateTime, comment='比赛时间')
buy_end_time = Column(DateTime, comment='购买截至时间')
league_name = Column(String(255, 'utf8mb4_general_ci'), comment='联赛名')
home_name = Column(VARCHAR(255), comment='主队')
away_name = Column(VARCHAR(255), comment='客队')
half_score = Column(String(10, 'utf8mb4_general_ci'), comment='半场比分')
score = Column(String(10, 'utf8mb4_general_ci'), comment='全场比分')
total_goal_count = Column(Integer, comment='总进球数')
result = Column(Integer, comment='结果0 负 1 平 3 胜')
rq_count = Column(Integer, comment='让球数')
rq_result = Column(Integer, comment='让球结果0 负 1 平 3 胜')
spf_odds_ranking = Column(Integer, comment='胜平负赔率排名')
bf_odds_ranking = Column(Integer, comment='比分赔率排名')
bqc_odds_ranking = Column(Integer, comment='半全场赔率排名')
zjq_odds_ranking = Column(Integer, comment='总进球数赔率排名')
created_at = Column(DateTime, server_default=text("CURRENT_TIMESTAMP"))

View File

@ -0,0 +1,49 @@
# coding: utf-8
from sqlalchemy import Column, DateTime, Float, Integer, text
from sqlalchemy.dialects.mysql import VARCHAR
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class FootballMatchBfOdd(Base):
__tablename__ = 'football_match_bf_odds'
__table_args__ = {'comment': '竞彩足球比分赔率表'}
id = Column(Integer, primary_key=True)
match_id = Column(Integer)
issue = Column(VARCHAR(255))
result = Column(Float)
_1_0 = Column('1-0', Float)
_2_0 = Column('2-0', Float)
_2_1 = Column('2-1', Float)
_3_0 = Column('3-0', Float)
_3_1 = Column('3-1', Float)
_3_2 = Column('3-2', Float)
_4_0 = Column('4-0', Float)
_4_1 = Column('4-1', Float)
_4_2 = Column('4-2', Float)
_5_0 = Column('5-0', Float)
_5_1 = Column('5-1', Float)
_5_2 = Column('5-2', Float)
_0_0 = Column('0-0', Float)
_1_1 = Column('1-1', Float)
_2_2 = Column('2-2', Float)
_3_3 = Column('3-3', Float)
_0_1 = Column('0-1', Float)
_0_2 = Column('0-2', Float)
_1_2 = Column('1-2', Float)
_0_3 = Column('0-3', Float)
_1_3 = Column('1-3', Float)
_2_3 = Column('2-3', Float)
_0_4 = Column('0-4', Float)
_1_4 = Column('1-4', Float)
_2_4 = Column('2-4', Float)
_0_5 = Column('0-5', Float)
_1_5 = Column('1-5', Float)
_2_5 = Column('2-5', Float)
win_others = Column(Float)
draw_others = Column(Float)
lost_others = Column(Float)
created_at = Column(DateTime, server_default=text("CURRENT_TIMESTAMP"))

View File

@ -0,0 +1,27 @@
# coding: utf-8
from sqlalchemy import Column, DateTime, Float, Integer, String, text
from sqlalchemy.dialects.mysql import VARCHAR
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class FootballMatchBqcOdd(Base):
__tablename__ = 'football_match_bqc_odds'
__table_args__ = {'comment': '竞彩足球半全场赔率表'}
id = Column(Integer, primary_key=True)
match_id = Column(Integer)
issue = Column(VARCHAR(255))
result = Column(String(50, 'utf8mb4_general_ci'))
_3_3 = Column('3_3', Float)
_3_1 = Column('3_1', Float)
_3_0 = Column('3_0', Float)
_1_3 = Column('1_3', Float)
_1_1 = Column('1_1', Float)
_1_0 = Column('1_0', Float)
_0_3 = Column('0_3', Float)
_0_1 = Column('0_1', Float)
_0_0 = Column('0_0', Float)
created_at = Column(DateTime, server_default=text("CURRENT_TIMESTAMP"))

View File

@ -0,0 +1,26 @@
# coding: utf-8
from sqlalchemy import Column, DateTime, Float, Integer, text
from sqlalchemy.dialects.mysql import VARCHAR
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class FootballMatchSpfOdd(Base):
__tablename__ = 'football_match_spf_odds'
__table_args__ = {'comment': '竞彩足球胜平负赔率表'}
id = Column(Integer, primary_key=True)
match_id = Column(Integer)
issue = Column(VARCHAR(255))
result = Column(Integer, comment='结果')
rq_count = Column(Integer, comment='让球数')
rq_result = Column(Integer, comment='让球结果')
win = Column(Float, comment='')
draw = Column(Float, comment='')
lost = Column(Float, comment='')
rq_win = Column(Float, comment='让球-胜')
rq_draw = Column(Float, comment='让球-平')
rq_lost = Column(Float, comment='让球-负')
created_at = Column(DateTime, server_default=text("CURRENT_TIMESTAMP"))

View File

@ -0,0 +1,25 @@
# coding: utf-8
from sqlalchemy import Column, Float, Integer, String
from sqlalchemy.dialects.mysql import VARCHAR
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class FootballMatchZjqOdd(Base):
__tablename__ = 'football_match_zjq_odds'
__table_args__ = {'comment': '竞彩足球总进球数赔率表'}
id = Column(Integer, primary_key=True)
issue = Column(VARCHAR(255))
match_id = Column(Integer)
result = Column(String(50, 'utf8mb4_general_ci'))
ball_0 = Column(Float)
ball_1 = Column(Float)
ball_2 = Column(Float)
ball_3 = Column(Float)
ball_4 = Column(Float)
ball_5 = Column(Float)
ball_6 = Column(Float)
ball_7_plus = Column(Float)

View File

@ -0,0 +1,26 @@
# coding: utf-8
from sqlalchemy import Column, DateTime, Float, Integer, text
from sqlalchemy.dialects.mysql import VARCHAR
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class FootballSfc(Base):
__tablename__ = 'football_sfc'
__table_args__ = {'comment': '足彩胜负彩/任选九表'}
id = Column(Integer, primary_key=True)
draw_issue = Column(VARCHAR(10), comment='期数')
draw_date = Column(DateTime, comment='开奖时间')
draw_code = Column(VARCHAR(50), comment='开奖结果')
sale_money = Column(Integer, comment='胜负彩销售额')
award_1_money = Column(Float(10), comment='一等奖单注奖金')
award_1_num = Column(Integer, comment='一等奖中奖数')
award_2_money = Column(Float(10), comment='二等奖单注奖金')
award_2_num = Column(Integer, comment='二等奖中奖数')
r9_sale_money = Column(Integer, comment='任九销售额')
r9_award_money = Column(Float(10), comment='任九奖金')
r9_award_num = Column(Integer, comment='任九中奖数')
created_at = Column(DateTime, server_default=text("CURRENT_TIMESTAMP"))

25
lottery/model/klb.py Normal file
View File

@ -0,0 +1,25 @@
# coding: utf-8
from sqlalchemy import Column, Integer, String, Date
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class KLB(Base):
__tablename__ = 'klb'
id = Column(Integer, primary_key=True)
draw_issue = Column(String(25, 'utf8mb4_general_ci'))
draw_date = Column(Date)
draw_code = Column(String(10, 'utf8mb4_general_ci'))
code_big = Column(Integer)
code_small = Column(Integer)
code_single = Column(Integer)
code_double = Column(Integer)
sum_num = Column(Integer, comment='总和')
sum_ten_one = Column(Integer, comment='十位个位和')
sum_hundred_ten = Column(Integer, comment='百位十位和')
sum_hundred_one = Column(Integer, comment='百位各位和')
sum_single_double = Column(Integer, comment='总和奇偶')
sum_big_small = Column(Integer, comment='总和大小')

32
lottery/model/pls.py Normal file
View File

@ -0,0 +1,32 @@
# coding: utf-8
from sqlalchemy import Column, Date, DateTime, Integer, text
from sqlalchemy.dialects.mysql import VARCHAR
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class PLS(Base):
__tablename__ = 'pls'
__table_args__ = {'comment': '排列三统计表'}
id = Column(Integer, primary_key=True)
draw_issue = Column(VARCHAR(25))
draw_date = Column(Date)
draw_code = Column(VARCHAR(10))
hundred = Column(Integer)
ten = Column(Integer)
one = Column(Integer)
group_type = Column(Integer)
code_big = Column(Integer)
code_small = Column(Integer)
code_single = Column(Integer)
code_double = Column(Integer)
sum_num = Column(Integer, comment='总和')
sum_ten_one = Column(Integer, comment='十位个位和')
sum_hundred_ten = Column(Integer, comment='百位十位和')
sum_hundred_one = Column(Integer, comment='百位各位和')
sum_single_double = Column(Integer, comment='总和奇偶')
sum_big_small = Column(Integer, comment='总和大小')
created_at = Column(DateTime, server_default=text("CURRENT_TIMESTAMP"))

33
lottery/model/plw.py Normal file
View File

@ -0,0 +1,33 @@
# coding: utf-8
from sqlalchemy import Column, Date, DateTime, Integer, text
from sqlalchemy.dialects.mysql import VARCHAR
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class PLW(Base):
__tablename__ = 'plw'
__table_args__ = {'comment': '排列五统计表'}
id = Column(Integer, primary_key=True)
draw_issue = Column(VARCHAR(25))
draw_date = Column(Date)
draw_code = Column(VARCHAR(10))
ten_thousand = Column(Integer, comment='')
thousand = Column(Integer, comment='')
hundred = Column(Integer, comment='')
ten = Column(Integer, comment='')
one = Column(Integer, comment='')
code_big = Column(Integer)
code_small = Column(Integer)
code_single = Column(Integer)
code_double = Column(Integer)
sum_num = Column(Integer, comment='总和')
sum_ten_one = Column(Integer, comment='十位个位和')
sum_hundred_ten = Column(Integer, comment='百位十位和')
sum_hundred_one = Column(Integer, comment='百位各位和')
sum_single_double = Column(Integer, comment='总和奇偶')
sum_big_small = Column(Integer, comment='总和大小')
created_at = Column(DateTime, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))

34
lottery/model/sd.py Normal file
View File

@ -0,0 +1,34 @@
# coding: utf-8
from sqlalchemy import Column, Date, DateTime, Integer
from sqlalchemy.dialects.mysql import VARCHAR
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class SD(Base):
__tablename__ = 'sd'
__table_args__ = {'comment': '福彩3d统计表'}
id = Column(Integer, primary_key=True)
draw_issue = Column(VARCHAR(25))
draw_date = Column(Date)
draw_code = Column(VARCHAR(10))
hundred = Column(Integer)
ten = Column(Integer)
one = Column(Integer)
group_type = Column(Integer)
group_3_span = Column(Integer, comment='组三跨度')
code_big = Column(Integer)
code_small = Column(Integer)
code_single = Column(Integer)
code_double = Column(Integer)
sum_num = Column(Integer, comment='总和')
sum_num_span = Column(Integer, comment='总和跨度')
sum_ten_one = Column(Integer, comment='十位个位和')
sum_hundred_ten = Column(Integer, comment='百位十位和')
sum_hundred_one = Column(Integer, comment='百位各位和')
sum_single_double = Column(Integer, comment='总和奇偶')
sum_big_small = Column(Integer, comment='总和大小')
created_at = Column(DateTime)

51
main.py Normal file
View File

@ -0,0 +1,51 @@
import time
from schedule import every, repeat, run_pending
from requests_html import HTML, HTMLSession
from lottery import Lottery
session = HTMLSession()
def get_data_job(lottery_type='pls'):
"""
:param lottery_type
"""
url = f"https://kaijiang.500.com/{lottery_type}.shtml"
r = session.get(url)
if lottery_type == 'pls':
issue_elem = r.html.search('排列3 第 {}')
elif lottery_type == 'plw':
issue_elem = r.html.search('排列5 第 {}')
else:
issue_elem = r.html.search('福彩3D 第 {}')
draw_date = r.html.xpath('//td[@class="td_title01"]/span[@class="span_right"]/text()', first=True)
result = r.html.xpath('//li[@class="ball_orange"]/text()')
if issue_elem is not None and draw_date is not None and result is not None and len(result) != 0 :
draw_issue = HTML(html=issue_elem[0]).text
draw_code = "".join(result)
print(f"result:{draw_code}")
with Lottery(lottery_type=lottery_type) as lottery:
last_id = lottery.insert(draw_issue, draw_date, draw_code)
if last_id:
print(f"issue:{draw_issue}数据写入完成。。。")
else:
print(f'issue:{draw_issue}已经存在')
@repeat(every().day.at("00:01"))
def pls_job():
get_data_job(lottery_type='pls')
@repeat(every().day.at("00:02"))
def plw_job():
get_data_job(lottery_type='plw')
@repeat(every().day.at("00:03"))
def sd_job():
get_data_job(lottery_type='sd')
while True:
run_pending()
time.sleep(1)

8
requirements.txt Normal file
View File

@ -0,0 +1,8 @@
numpy==1.24.3
pandas==2.0.1
PyMySQL==1.0.3
requests-html==0.10.0
retry==0.9.2
retrying==1.3.4
schedule==1.2.0
SQLAlchemy==2.0.11