python discuz_python3实现discuz论坛数据库批量图文发帖搭建DZ插件模板网站附件源码下载...
最近想用discuz論壇搭建一個DZ插件模板下載網站,但內容較多一個個發布主題帖子會非常麻煩,因此想著有沒有什么好的方法可以批量發帖,批量回復,批量上傳圖片附件之類的。既然學了萬能的python,于是就想到用python3來實現。
python實現discuz論壇批量發帖的方式,愛在靈靈久博客認為主要分為兩類,一是通過登錄discuz論壇進行發帖(這種方式也可以登錄第三方的網站來批量發帖);二是作為站長直接通過寫入數據庫來發帖,可以實現無限量發帖,發帖速度快。第一種方式實現的已經有很多人介紹了,這里主要介紹第二種方式直接用python3寫入discuz論壇數據庫批量發帖可帶圖文。
一、discuz數據庫發帖原理
在介紹數據發帖前先來了解一下discuz論壇發帖涉及到的數據庫:
1、主題表 pre_forum_thread:這個表一個主要數據就是 tid 主題ID
2、post 分表協調表 pre_forum_post_tableid:這里需要獲取一個自增的 pid
3、帖子表 pre_forum_post :記錄主題pid、fid、tid、title、content等主要信息
4、版塊表 pre_forum_forum:這里主要更新版塊的主題、帖子數量
5、帖子主題審核數據表 pre_forum_thread_moderate:這個可以根據自己狀況決定,并不是必須的(可以省略)
6、用戶統計表 pre_common_member_count:主要是更新用戶的主題數量
pre_common_member_count表和pre_forum_forum表兩個表中主要修改帖子數據量其中主要涉及到以下幾個字段:
threads: 版塊內的主題數.
posts: 版塊內的帖子數.(主題數和帖子數是有區別的,發布的一個帖子會同時增加主題數和帖子數,而回復一個帖子只會增加一個帖子數不會增加主題數)
todayposts: 版塊內, 今日發帖的個數. 這個是post的個數, 不是thread的個數.
lastpost: 這個字段比較奇葩, ?看名字它是表示本版塊最新一個帖子. ? 但它的值比較有意思, 這是一個字符串, ?由四部分組成, 每部分之間用 \t 制表符分割. ?第一部分是這個帖子的pid, ?第二部分是帖子的標題, 第三部分是帖子的發帖時間, 第四部分是帖子的作者名. ? 這個字段可能是為了提高論壇首頁的性能, 有了他之后,首頁就負擔輕了很多。
二、python數據庫發帖環境
本次測試使用的是Windows10 64位的操作系統 python3.6的版本,pycharm的編輯器,本地搭建的discuz論壇網站(也可直接使用上線的discuz論壇網站,不過建議先在本地進行測試),另外需要安裝pymysql庫,通過pip install pymysql安裝上即可。
三、python寫入數據庫的步驟
discuz 發帖流程主要分為5個步驟:
第一步:給pre_forum_post_tableid表插入空數據進行自增,然后獲取自增pid。
cursor.execute('INSERT INTO pre_forum_post_tableid VALUES (NULL);')
cursor.execute('SELECT max(pid) FROM pre_forum_post_tableid;')
pid = cursor.fetchone()[0]
第二步:向 主題表 pre_forum_thread 中插入版塊ID、用戶ID、用戶名、帖子標題、發帖時間等信息,并獲取主題的ID作為tid。
sql_thread="INSERT INTO pre_forum_thread SET fid="+str(fid)+",author='"+author+"',authorid="+str(uid)+",subject='"+subject+"',dateline="+str(int(time.time()))+",lastposter='"+author+"',lastpost="+str(int(time.time()))+",views="+str(view)+";"
cursor.execute(sql_thread)
cursor.execute('SELECT max(tid) FROM pre_forum_thread')
tid = int(cursor.fetchone()[0])
第三步:向帖子表 pre_forum_post 中插入帖子相關信息,這里需要注意的是: pid為第一步的pid值,tid為第二步的tid值
sql_post = "INSERT INTO pre_forum_post SET pid="+str(pid)+",fid="+str(fid)+",tid="+str(tid)+",first=1,author='"+author+"', authorid="+str(uid)+", subject='"+subject+"' ,dateline="+str(int(time.time()))+", message='" + message + "' , useip='140.112.218.141' , port=11560 , invisible = 0, anonymous = 0 , usesig = 1 , htmlon = 1 , bbcodeoff =-1 , smileyoff =-1 , parseurloff =0 , attachment = 0 , tags='' , replycredit=0 , status=0;"
第四步:更新版塊 pre_forum_forum 相關主題、帖子數量信息
sql_forum = 'UPDATE pre_forum_forum SET threads=threads+1, posts=posts+1, todayposts=todayposts+1 , allowsmilies = 1,allowbbcode = 1, allowimgcode =1 ,allowspecialonly = 1,allowglobalstick = 1,alloweditpost = 1 ,recyclebin =1 WHERE fid='+str(fid)+';'
第五步:更新用戶 pre_common_member_count 帖子數量信息
sql_count = 'UPDATE pre_common_member_count SET threads = threads+1 WHERE uid ='+str(uid)+';'
discuz發帖過程主要就是以上5個步驟,通過這幾個步驟就可以實現discuz的發帖流程,其中涉及到一些積分等其他信息的可以自己加上。另外,通過數據庫發帖還可以發布帶圖片的帖子,只需先將圖片直接上傳到網站中圖片附件對應存放的位置,然后將其形成鏈接(直接上傳的圖片文件名稱最好是用拼音或數字,不要帶中文),在發帖時內容里面加入img標簽進行解析圖片地址即可形成帶圖文的帖子。或者,直接采集其他內容源碼后作為帖子內容,同時安裝一個圖片本地化插件即可實現帶圖片的帖子。
以上方法可以實現discuz論壇批量發布圖文帖子,可以解決大部分站長的需求,但是卻不能發布帶附件的帖子,因此需要想其他辦法。下一次將繼續分享如何使用resquests庫來批量發布帶附件的帖子,歡迎大家收藏關注本站 愛在靈靈久博客
四、源碼下載
def post_data(conn):
cursor = conn.cursor()
try:
cursor.execute('SELECT username FROM pre_common_member WHERE uid = '+str(uid)+";")
author = cursor.fetchone()[0] # 用戶name
print(author)
# 第一步給pre_forum_post_tableid表插入空數據進行自增,然后獲取自增pid
cursor.execute('INSERT INTO pre_forum_post_tableid VALUES (NULL);')
cursor.execute('SELECT max(pid) FROM pre_forum_post_tableid;')
pid = cursor.fetchone()[0]
# print(pid)
# 第二步給pre_forum_thread表插入帖子標題數據,然后獲取自增tid
sql_thread = "INSERT INTO pre_forum_thread SET fid="+str(fid)+",author='"+author+"',authorid="+str(uid)+",subject='"+subject+"',dateline="+str(int(time.time()))+",lastposter='"+author+"',lastpost="+str(int(time.time()))+",views="+str(view)+";"
cursor.execute(sql_thread)
cursor.execute('SELECT max(tid) FROM pre_forum_thread')
tid = int(cursor.fetchone()[0])
# print(tid)
# 第三步給pre_forum_post表插入帖子的標題、內容等,pid、tid用上兩步獲得的數據 如要增加附件需修改attachment
sql_post = "INSERT INTO pre_forum_post SET pid="+str(pid)+",fid="+str(fid)+",tid="+str(tid)+",first=1,author='"+author+"', authorid="+str(uid)+", subject='"+subject+"' ,dateline="+str(int(time.time()))+", message='" + message + "' , useip='140.112.218.141' , port=11560 , invisible = 0, anonymous = 0 , usesig = 1 , htmlon = 1 , bbcodeoff =-1 , smileyoff =-1 , parseurloff =0 , attachment = 0 , tags='' , replycredit=0 , status=0;"
# print(sql_post)
cursor.execute(sql_post)
# cursor.execute('SELECT max(aid) FROM pre_forum_attachment')
# aid = int(cursor.fetchone()[0]) + 1
# 第四步給pre_forum_forum版塊表進行更新帖子數量
sql_forum = 'UPDATE pre_forum_forum SET threads=threads+1, posts=posts+1, todayposts=todayposts+1 , allowsmilies = 1,allowbbcode = 1, allowimgcode =1 ,allowspecialonly = 1,allowglobalstick = 1,alloweditpost = 1 ,recyclebin =1 WHERE fid='+str(fid)+';'
# print(sql_forum)
cursor.execute(sql_forum)
# 第五步給pre_common_member_count表更新用戶帖子數量信息
sql_count = 'UPDATE pre_common_member_count SET threads = threads+1 WHERE uid ='+str(uid)+';'
cursor.execute(sql_count)
# cursor.execute('INSERT INTO pre_forum_attachment_" + str(aid % 10) + " SET `readperm`='0' , `price`='10' , `tid`='" + str(tid) + "' , pid=' + pid+ ',uid=1 , description=, aid=' + str(aid) + ' ,dateline='+ str(int(time.time())) + ',filename="' + att_name + '", filesize=4, attachment="upload/' + att_name + '",remote=0, isimage=0, width=0, thumb=0;')
# cursor.execute('INSERT INTO pre_forum_attachment SET tid=' + str(tid) + ', pid=' +str(pid)+ ', tableid='+ str(aid % 10) + ', aid=' + str(aid) + ';')
# 提交,不然無法保存新建或者修改的數據
conn.commit()
except:
print("寫入數據庫失敗,事物回滾!")
conn.rollback()
finally:
cursor.close()
總結
以上是生活随笔為你收集整理的python discuz_python3实现discuz论坛数据库批量图文发帖搭建DZ插件模板网站附件源码下载...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: unity让特效在UI上播放
- 下一篇: python之函数的定义