設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 243|回復: 0
打印 上一主題 下一主題

python3实现discuz论坛数据库批量图文发帖搭建DZ插件模板网站

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2023-4-9 08:49:22 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
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。

  1. cursor.execute('INSERT INTO pre_forum_post_tableid VALUES (NULL);')
  2. cursor.execute('SELECT max(pid) FROM pre_forum_post_tableid;')
  3. pid = cursor.fetchone()[0]
複製代碼

第二步:向 主题表 pre_forum_thread 中插入版块ID、用户ID、用户名、帖子标题、发帖时间等信息,并获取主题的ID作为tid。

  1. 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)+";"
  2. cursor.execute(sql_thread)
  3. cursor.execute('SELECT max(tid) FROM pre_forum_thread')
  4. tid = int(cursor.fetchone()[0])
複製代碼

第三步:向帖子表 pre_forum_post 中插入帖子相关信息,这里需要注意的是: pid为第一步的pid值,tid为第二步的tid值

  1. 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 相关主题、帖子数量信息

  1. 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 帖子数量信息

  1. sql_count = 'UPDATE pre_common_member_count SET threads = threads+1 WHERE uid ='+str(uid)+';'
複製代碼

discuz发帖过程主要就是以上5个步骤,通过这几个步骤就可以实现discuz的发帖流程,其中涉及到一些积分等其他信息的可以自己加上。另外,通过数据库发帖还可以发布带图片的帖子,只需先将图片直接上传到网站中图片附件对应存放的位置,然后将其形成链接(直接上传的图片文件名称最好是用拼音或数字,不要带中文),在发帖时内容里面加入img标签进行解析图片地址即可形成带图文的帖子。或者,直接采集其他内容源码后作为帖子内容,同时安装一个图片本地化插件即可实现带图片的帖子。

以上方法可以实现discuz论坛批量发布图文帖子,可以解决大部分站长的需求,但是却不能发布带附件的帖子,因此需要想其他办法。

  1. def post_data(conn):
  2.     cursor = conn.cursor()
  3.     try:
  4.         cursor.execute('SELECT username FROM pre_common_member WHERE uid = '+str(uid)+";")
  5.         author = cursor.fetchone()[0]  # 用户name
  6.         print(author)
  7.         # 第一步给pre_forum_post_tableid表插入空数据进行自增,然后获取自增pid
  8.         cursor.execute('INSERT INTO pre_forum_post_tableid VALUES (NULL);')
  9.         cursor.execute('SELECT max(pid) FROM pre_forum_post_tableid;')
  10.         pid = cursor.fetchone()[0]
  11.         # print(pid)
  12.         # 第二步给pre_forum_thread表插入帖子标题数据,然后获取自增tid
  13.         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)+";"
  14.         cursor.execute(sql_thread)
  15.         cursor.execute('SELECT max(tid) FROM pre_forum_thread')
  16.         tid = int(cursor.fetchone()[0])
  17.         # print(tid)
  18.         # 第三步给pre_forum_post表插入帖子的标题、内容等,pid、tid用上两步获得的数据  如要增加附件需修改attachment
  19.         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;"
  20.         # print(sql_post)
  21.         cursor.execute(sql_post)
  22.         # cursor.execute('SELECT max(aid) FROM pre_forum_attachment')
  23.         # aid = int(cursor.fetchone()[0]) + 1
  24.         # 第四步给pre_forum_forum版块表进行更新帖子数量
  25.         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)+';'
  26.         # print(sql_forum)
  27.         cursor.execute(sql_forum)
  28.         # 第五步给pre_common_member_count表更新用户帖子数量信息
  29.         sql_count = 'UPDATE pre_common_member_count SET threads = threads+1 WHERE uid ='+str(uid)+';'
  30.         cursor.execute(sql_count)
  31.         # 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;')
  32.         # cursor.execute('INSERT INTO pre_forum_attachment SET tid=' + str(tid) + ', pid=' +str(pid)+ ', tableid='+ str(aid % 10) + ', aid=' + str(aid) + ';')
  33.         # 提交,不然无法保存新建或者修改的数据
  34.         conn.commit()
  35.     except:
  36.         print("写入数据库失败,事物回滚!")
  37.         conn.rollback()
  38.     finally:
  39.         cursor.close()
複製代碼



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-4-29 22:55 , Processed in 0.255885 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表