I am writing a python script that will call mandrills export activity api for every blog on a worpress mu install except for the main site. The script is designed to be called from a cron job on a regular schedule. The code I have is here.
__author__ = 'Taylor'
import datetime
import mandrill
import phpserialize
import mysql.connector
class BlogReporter(object):
blog_id = None
table_name = 'wp_%s_options'
notify_email = None
sender_email = None
date_to = None
date_from = None
def __init__(self, blog_id_, database_config_, mandrill_client_):
self.blog_id = blog_id_
self.mandrill_client = mandrill_client_
self.table_name = 'wp_' + str(blog_id_) + '_options'
try:
cnx_ = mysql.connector.connect(**database_config_)
except mysql.connector.Error as err:
print(err)
else:
query_ = "SELECT option_value FROM " + self.table_name + " WHERE option_name=%s"
try:
cursor_ = cnx_.cursor()
cursor_.execute(query_, ('admin_email',))
result = cursor_.fetchone()
except Exception as err:
print(err)
else:
# set notify email from cursor
self.notify_email = result
cursor_.close()
try:
cursor_ = cnx_.cursor()
cursor_.execute(query_, ('wysija',))
encoded_result = cursor_.fetchone()[0]
except Exception as err:
print(err)
else:
decoded_result = phpserialize.unserialize(encoded_result.decode('base64', 'strict'))
self.sender_email = decoded_result['from_email']
cursor_.close()
cnx_.close()
self.date_to = datetime.datetime.now()
# if it monday lets get activity since saturday
if self.date_to.weekday() is 0:
self.date_from = self.date_to - datetime.timedelta(days=2)
# else just get the activity from the past day
else:
self.date_from = self.date_to - datetime.timedelta(days=1)
def export_activity(self):
self.mandrill_client.exports.activity(self.notify_email, self.date_from, self.date_to, None,
self.sender_email,'Sent')
# database name and credentials
# these should eventually be parsed from command line arguments
database_config = {
'user': 'root',
'password': 'password',
'database': 'wpmudb'
}
# Mandrill API Key
# this should also be parsed from command line
mandrill_api_key = 'xxxxx_xxxxxxxxxxxxxxxx'
mandrill_client = mandrill.Mandrill(mandrill_api_key)
try:
cnx = mysql.connector.connect(**database_config)
except mysql.connector.Error as err:
print(err)
else:
try:
cursor = cnx.cursor()
# Get all blogs but blog_id 1 since the main site needs no report
query = "SELECT blog_id FROM wp_blogs WHERE blog_id != '1'"
cursor.execute(query)
except Exception as err:
print(err)
else:
for blog_id in cursor:
blog = BlogReporter(blog_id[0], database_config, mandrill_client)
blog.export_activity()
del blog
cursor.close()
cnx.close()
I wrote it like this because it is the first program I have written in python and I was unsure of how to structure it and wanted to learn more about classes in python. However, recognizing that in the programs current state Class BlogReporter
's constructer and method export_activity
could be refactored into one function that accepts the arguments of class BlogReporter
's constructor and is simply the class's constructor plus the method export_activity
; I was curious if doing so would be beneficial to the program in any way.
I would also like to welcome comments as to whether the mandrill client should be reinstantiated for each instance of BlogReporter
or whether it is better to pass the same client to each instance.
keyring
module instead. \$\endgroup\$