save all the course json date to database

This commit is contained in:
lostecho
2024-05-26 19:35:57 +08:00
parent 6e4c28fb76
commit b7d1f71575
6 changed files with 175 additions and 6 deletions

View File

@@ -1,7 +1,10 @@
import json
import logging
import os
import sqlite3
from pathlib import Path
import random
from time import sleep
import requests
from pydub import AudioSegment
@@ -90,7 +93,8 @@ def get_audio(audio_data):
for file in temp_mp3_files:
file.unlink()
logging.info(f"All MP3 files have been downloaded, merged into {output_filename}, and temporary files have been removed.")
logging.info(
f"All MP3 files have been downloaded, merged into {output_filename}, and temporary files have been removed.")
# 获取全部附件
@@ -99,16 +103,60 @@ def get_all_attachments(attachment_json_data):
attachments = [item for item in data["data"]["course_contents"]]
attachmentlist = []
for attachment in attachments:
attachment = Attachment(attachments[0]["id"], attachments[0]["course_id"], attachments[0]["content"], attachments[0]["content"], attachments[0]["attachment"].get("url"))
attachment = Attachment(attachments[0]["id"], attachments[0]["course_id"], attachments[0]["content"],
attachments[0]["content"], attachments[0]["attachment"].get("url"))
print(attachment)
attachmentlist.append(attachment)
print(attachments)
return attachmentlist
def query_all_course():
conn = sqlite3.connect('course_database.db')
print("数据库打开成功")
c = conn.cursor()
all_course_json = c.execute('SELECT JSON from JSON_DATA jd WHERE "TYPE" = "ALL"').fetchall()
return all_course_json[0][0]
def query_course_by_id(course_id):
conn = sqlite3.connect('course_database.db')
print("数据库打开成功")
c = conn.cursor()
all_course_json = c.execute('SELECT JSON from JSON_DATA jd WHERE ID = ' + str(course_id)).fetchall()
# return re.sub(r'[\r\n]', '', str(all_course_json[0]))
return all_course_json[0][0]
def save_course_json(ids):
conn = sqlite3.connect('course_database.db')
print("数据库打开成功")
c = conn.cursor()
for id in ids:
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiIxMDAwMDgzNDciLCJleHAiOjE3MTkxODk0ODQsImp0aSI6IjU3ZTJhMzdmLTMyZGEtNGQ2My1hZjQxLTY5NTRlNmU1OTg2MiIsImlhdCI6MTcxNjUxMTA4NCwiaXNzIjoiYXBwdXNlciIsInVpZCI6ImJlMmViOGIyLTFhOTItNGVmMC05ZDAwLTA1YTlkN2E2OWRiMiIsInNjaGVtZSI6Imp3dGhzIiwic2lkIjoiMWI4ZjE1ZTItYjQ5ZC00MmRmLWEwNDUtZmQxYTUwNzI5ZjkxIn0.IO7C2gtsi8lMdrOgWGNuxK-t2zzmDPvmI4BqISHeZEI"
json_data = request_date(ids[0], token)
# 将JSON数据转换为字符串
json_string = json.dumps(str(json_data))
# 插入JSON字符串到SQLite表中
c.execute("INSERT OR IGNORE INTO JSON_DATA (ID,JSON,TYPE) VALUES (?,?,?)", (id,json_string,"COURSE"))
#c.execute(
# 'INSERT OR IGNORE INTO JSON_DATA (ID,JSON,TYPE) values (' + str(id) + ',' + str(json_data) + ',' + "COURSE" + ')')
conn.commit()
print("insert one "+str(id) + json_string)
secs = random.normalvariate(5, 0.4)
if secs <= 0:
secs = 1 # 太小则重置为平均值
sleep(secs)
conn.close()
if __name__ == '__main__':
ids = get_course_id('all/course.json')
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiIxMDAwMDgzNDciLCJleHAiOjE3MTkxODk0ODQsImp0aSI6IjU3ZTJhMzdmLTMyZGEtNGQ2My1hZjQxLTY5NTRlNmU1OTg2MiIsImlhdCI6MTcxNjUxMTA4NCwiaXNzIjoiYXBwdXNlciIsInVpZCI6ImJlMmViOGIyLTFhOTItNGVmMC05ZDAwLTA1YTlkN2E2OWRiMiIsInNjaGVtZSI6Imp3dGhzIiwic2lkIjoiMWI4ZjE1ZTItYjQ5ZC00MmRmLWEwNDUtZmQxYTUwNzI5ZjkxIn0.IO7C2gtsi8lMdrOgWGNuxK-t2zzmDPvmI4BqISHeZEI"
json_data = request_date(ids[0], token)
#get_audio(json_data)
# json_data = request_date(ids[0], token)
json_data = query_course_by_id(488)
# get_audio(json_data)
print(json_data)
get_all_attachments(json_data)
save_course_json(ids)