feat: 支持从文件中读取官网数据库

This commit is contained in:
caoqianming 2024-05-11 14:21:06 +08:00
parent ebd49f5dc0
commit 3d3f2a971f
1 changed files with 40 additions and 37 deletions

View File

@ -111,43 +111,46 @@ def get_cbma_info_from_db_and_ana(year: str = '2023'):
df = pd.read_sql_query(query_gzhs, conn) df = pd.read_sql_query(query_gzhs, conn)
conn.close() conn.close()
# 尝试连接官网库进行查询 file_path = os.path.join(BASE_DIR, f'cbma_site/{year}.xlsx')
import psycopg2 if os.path.exists(file_path):
conn_web = None df_web = pd.read_excel(file_path, sheet_name='Result 1')
df_web = None else:
try: import psycopg2
conn_web = psycopg2.connect( conn_web = None
"dbname={} user={} password={} host={} port={}".format('edn_cms', 'auditor', 'Lde78B3_cbma', '10.65.253.10', '54321')) df_web = None
query_web = f""" try:
SELECT conn_web = psycopg2.connect(
a_outer.id, "dbname={} user={} password={} host={} port={}".format('edn_cms', 'auditor', 'Lde78B3_cbma', '10.65.253.10', '54321'))
TO_CHAR(a_outer.ctime, 'YYYY') AS pub_year, query_web = f"""
TO_CHAR(a_outer.ctime, 'MM') AS pub_month, SELECT
TO_CHAR(a_outer.ctime, 'DD') AS pub_day, a_outer.id,
a_outer.title, TO_CHAR(a_outer.ctime, 'YYYY') AS pub_year,
a_outer.source, TO_CHAR(a_outer.ctime, 'MM') AS pub_month,
a_outer.hits, TO_CHAR(a_outer.ctime, 'DD') AS pub_day,
t.title as bankuai, a_outer.title,
a_outer.src a_outer.source,
FROM a_outer.hits,
"a_article" a_outer t.title as bankuai,
left join ( a_outer.src
select id, title, father, path FROM
from a_article "a_article" a_outer
where father in (20110528, 19080024) left join (
) t on a_outer.father = t.id select id, title, father, path
WHERE from a_article
a_outer.TYPE = 3 where father in (20110528, 19080024)
and a_outer.deleted is NULL ) t on a_outer.father = t.id
and EXTRACT ( YEAR FROM a_outer.ctime ) = {year} WHERE
and bankuai is not NULL a_outer.TYPE = 3
ORDER BY and a_outer.deleted is NULL
a_outer.ctime; and EXTRACT ( YEAR FROM a_outer.ctime ) = {year}
""" and bankuai is not NULL
df_web = pd.read_sql_query(query_web, conn_web) ORDER BY
conn_web.close() a_outer.ctime;
except Exception as e: """
print(e) df_web = pd.read_sql_query(query_web, conn_web)
conn_web.close()
except Exception as e:
print(e)
df['source'] = '' df['source'] = ''
# 追加总院数据来源 # 追加总院数据来源