2. Automating MyBB with Selenium and Python

Inspection of the forums and threads generated by MyBB shows that each forum has an id, and each thread has an id. The URL to a forum thread only contains the thread id: e.g.https://peter-thomson.com/mybb/showthread.php?tid=593

So first of all I add a column for the thread id to my own database table that holds my web pages, and a column for the forum id to the database table that holds my categories of pages.( This is on my local system ).

Next add the Python library called Selenium to your local python installation.

You will also need the version of FireFox that can be driven by WebDriver

( You may need to do a little research to get this working on your system )

I have installed MyBB on my server and we are now ready. I have created a couple of forums on the server using the admin interface and noted the forum ids. Automating the creation of forums will come later once the automation of thread creation is fully working.

The script runs on your local system, accesses MyBB on your remote server, and updates your database on your local system. When you run the script successfuly you should see the browser window quickly updating each thread in turn

#!/usr/bin/env python3
# coding=utf-8
#  copyright Peter Thomson 2015,2018
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  GNU General Public License for more details.
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see http://www.gnu.org/licenses.
GPL3=u'''   This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see http://www.gnu.org/licenses
thisversion '1.01'
import os
import os.path
import cgi
import webbrowser
import sqlite3
import string
import requests
import http.cookiejar
import re
import datetime
import imaplib
import codecs
import urllib
import sys
from datetime import datetimedelta
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
global browser
browser webdriver.Firefox()
ti datetime.datetime.now()
timestamp = ("Current date & time = %s" ti)
myusername'*******' #this is your admin username
mypassword '''*********************'''#this is your admin password
def setforumthreads():
    mycategory 'dolomites' #for this tutorial these have been created first by the admin
    forumid_for_mycategory '11' # and the forum id noted when the forum is created in the forum management page
    ti datetime.datetime.now()
    timestamp = ("Current date & time = %s" ti)
    url='https://peter-thomson.com/mybb/member.php?action=login' #this is the URL to login to your installation of MyBB on your server
    payload={'email'myusername 'password' mypassword }
    browser.geturl )
    username browser.find_element_by_name("username")
    password browser.find_element_by_name("password")
    buttons browser.find_elements_by_css_selector("input.button");
    db sqlite3.connect('website2.mybookshelf' #I am using an SQLite database for my web page details, in the same folder as this script on my local system
    cursor db.cursor()
    cursor.execute('''SELECT page_name, book, page_heading, thread_id  FROM bookpages where book = ? ''',(mycategory,)) #These are the details about each web page that I need in order to create a new forum thread for each page. book is the category of web page.
    all_rows cursor.fetchall()
    for pmrow in all_rows:
        print("checking "page_name str(thread_id) )
        if thread_id is None:
            #if the thread has already been created and stored in the thread_id field, don't create a new thread
            browser.get'https://peter-thomson.com/mybb/newthread.php?fid='forumid_for_mycategory# needs to be a forum where postings are permitted, not a category
            print ('Got page  ' )
            mysubjectpage_heading # the thread subject is going to be the same as the page heading
            mymessage 'This thread is for the discussion of: 'page_heading #and the first thread message content will use this format
            subject browser.find_element_by_name('''subject''')
            print('inserted subject')
            editor_frame browser.find_element_by_xpath('''//*[@id="content"]/div/form/table[1]/tbody/tr[5]/td[2]/div/iframe''')#//*[@id="content"]/div/form/table[1]/tbody/tr[5]/td[2]/div/iframe
            editor_body browser.find_element_by_xpath("//body"#/html/body
            print('inserted message')
            buttons browser.find_element_by_xpath('''//*[@id="content"]/div/form/div/input[1]''');
            #the thread has now been created, and we want to collect the new thread id from the page, but we must wait for the page to update
            timeout 5
                element_present EC.presence_of_element_located((By.ID'posts_container'))
            except TimeoutException:
                print"Timed out waiting for page to load" )
            mypage browser.page_source
            m=re.search('href="newreply.php\?tid=(.+?)"' mypage re.DOTALL #extract the new thread id
            forumlink 'unchanged'
            if is not None:
                tid m.group(1)
                print('New Thread is ' +tid )
                cursor.execute('''UPDATE bookpages SET thread_id = ? WHERE book= ? AND page_name = ? ''', (tidbookpage_name ))#save the new thread id to the table in my local database
    return "<h1>all done</h1>"  
print("All Done")       

Now all the new threads  have been completed and the thread id stored in your web page database you need to update each page and insert the appropriate thread id into an iframe template for each page.

foruminsert = '''<p><figure class="threadcontainer">
<div contenteditable="false" class="threadcontainer"><iframe align="middle"  name="" scrolling="yes" src="https://peter-thomson.com/mybb/showthread.php?tid='''+thread_id+'''" ></iframe></div><figcaption>Discussion</figcaption></figure>

template =template.replace('</main>',foruminsert+'</main>')