python discuz_python3实现discuz论坛数据库批量图文发帖搭建DZ插件模板网站附件源码下载...
最近想用discuz論壇搭建一個(gè)DZ插件模板下載網(wǎng)站,但內(nèi)容較多一個(gè)個(gè)發(fā)布主題帖子會(huì)非常麻煩,因此想著有沒有什么好的方法可以批量發(fā)帖,批量回復(fù),批量上傳圖片附件之類的。既然學(xué)了萬能的python,于是就想到用python3來實(shí)現(xiàn)。
python實(shí)現(xiàn)discuz論壇批量發(fā)帖的方式,愛在靈靈久博客認(rèn)為主要分為兩類,一是通過登錄discuz論壇進(jìn)行發(fā)帖(這種方式也可以登錄第三方的網(wǎng)站來批量發(fā)帖);二是作為站長直接通過寫入數(shù)據(jù)庫來發(fā)帖,可以實(shí)現(xiàn)無限量發(fā)帖,發(fā)帖速度快。第一種方式實(shí)現(xiàn)的已經(jīng)有很多人介紹了,這里主要介紹第二種方式直接用python3寫入discuz論壇數(shù)據(jù)庫批量發(fā)帖可帶圖文。
一、discuz數(shù)據(jù)庫發(fā)帖原理
在介紹數(shù)據(jù)發(fā)帖前先來了解一下discuz論壇發(fā)帖涉及到的數(shù)據(jù)庫:
1、主題表 pre_forum_thread:這個(gè)表一個(gè)主要數(shù)據(jù)就是 tid 主題ID
2、post 分表協(xié)調(diào)表 pre_forum_post_tableid:這里需要獲取一個(gè)自增的 pid
3、帖子表 pre_forum_post :記錄主題pid、fid、tid、title、content等主要信息
4、版塊表 pre_forum_forum:這里主要更新版塊的主題、帖子數(shù)量
5、帖子主題審核數(shù)據(jù)表 pre_forum_thread_moderate:這個(gè)可以根據(jù)自己狀況決定,并不是必須的(可以省略)
6、用戶統(tǒng)計(jì)表 pre_common_member_count:主要是更新用戶的主題數(shù)量
pre_common_member_count表和pre_forum_forum表兩個(gè)表中主要修改帖子數(shù)據(jù)量其中主要涉及到以下幾個(gè)字段:
threads: 版塊內(nèi)的主題數(shù).
posts: 版塊內(nèi)的帖子數(shù).(主題數(shù)和帖子數(shù)是有區(qū)別的,發(fā)布的一個(gè)帖子會(huì)同時(shí)增加主題數(shù)和帖子數(shù),而回復(fù)一個(gè)帖子只會(huì)增加一個(gè)帖子數(shù)不會(huì)增加主題數(shù))
todayposts: 版塊內(nèi), 今日發(fā)帖的個(gè)數(shù). 這個(gè)是post的個(gè)數(shù), 不是thread的個(gè)數(shù).
lastpost: 這個(gè)字段比較奇葩, ?看名字它是表示本版塊最新一個(gè)帖子. ? 但它的值比較有意思, 這是一個(gè)字符串, ?由四部分組成, 每部分之間用 \t 制表符分割. ?第一部分是這個(gè)帖子的pid, ?第二部分是帖子的標(biāo)題, 第三部分是帖子的發(fā)帖時(shí)間, 第四部分是帖子的作者名. ? 這個(gè)字段可能是為了提高論壇首頁的性能, 有了他之后,首頁就負(fù)擔(dān)輕了很多。
二、python數(shù)據(jù)庫發(fā)帖環(huán)境
本次測試使用的是Windows10 64位的操作系統(tǒng) python3.6的版本,pycharm的編輯器,本地搭建的discuz論壇網(wǎng)站(也可直接使用上線的discuz論壇網(wǎng)站,不過建議先在本地進(jìn)行測試),另外需要安裝pymysql庫,通過pip install pymysql安裝上即可。
三、python寫入數(shù)據(jù)庫的步驟
discuz 發(fā)帖流程主要分為5個(gè)步驟:
第一步:給pre_forum_post_tableid表插入空數(shù)據(jù)進(jìn)行自增,然后獲取自增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、用戶名、帖子標(biāo)題、發(fā)帖時(shí)間等信息,并獲取主題的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 中插入帖子相關(guān)信息,這里需要注意的是: 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 相關(guān)主題、帖子數(shù)量信息
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 帖子數(shù)量信息
sql_count = 'UPDATE pre_common_member_count SET threads = threads+1 WHERE uid ='+str(uid)+';'
discuz發(fā)帖過程主要就是以上5個(gè)步驟,通過這幾個(gè)步驟就可以實(shí)現(xiàn)discuz的發(fā)帖流程,其中涉及到一些積分等其他信息的可以自己加上。另外,通過數(shù)據(jù)庫發(fā)帖還可以發(fā)布帶圖片的帖子,只需先將圖片直接上傳到網(wǎng)站中圖片附件對應(yīng)存放的位置,然后將其形成鏈接(直接上傳的圖片文件名稱最好是用拼音或數(shù)字,不要帶中文),在發(fā)帖時(shí)內(nèi)容里面加入img標(biāo)簽進(jìn)行解析圖片地址即可形成帶圖文的帖子。或者,直接采集其他內(nèi)容源碼后作為帖子內(nèi)容,同時(shí)安裝一個(gè)圖片本地化插件即可實(shí)現(xiàn)帶圖片的帖子。
以上方法可以實(shí)現(xiàn)discuz論壇批量發(fā)布圖文帖子,可以解決大部分站長的需求,但是卻不能發(fā)布帶附件的帖子,因此需要想其他辦法。下一次將繼續(xù)分享如何使用resquests庫來批量發(fā)布帶附件的帖子,歡迎大家收藏關(guān)注本站 愛在靈靈久博客
四、源碼下載
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表插入空數(shù)據(jù)進(jìn)行自增,然后獲取自增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表插入帖子標(biāo)題數(shù)據(jù),然后獲取自增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表插入帖子的標(biāo)題、內(nèi)容等,pid、tid用上兩步獲得的數(shù)據(jù) 如要增加附件需修改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版塊表進(jìn)行更新帖子數(shù)量
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表更新用戶帖子數(shù)量信息
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) + ';')
# 提交,不然無法保存新建或者修改的數(shù)據(jù)
conn.commit()
except:
print("寫入數(shù)據(jù)庫失敗,事物回滾!")
conn.rollback()
finally:
cursor.close()
總結(jié)
以上是生活随笔為你收集整理的python discuz_python3实现discuz论坛数据库批量图文发帖搭建DZ插件模板网站附件源码下载...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: unity让特效在UI上播放
- 下一篇: python之函数的定义