Commit 77ed3db3 authored by Anastasia Galkin's avatar Anastasia Galkin
Browse files

wordpress stats, log file

parent bb667e87
settings.py
settings*
*.log
#!/usr/bin/python
import sys
import time
import MySQLdblog
from settings import *
log_name = '' + time.strftime("%Y%m%d_%H%M%S") + '_wordpress_stats.log'
file = open(log_name, "w")
import MySQLdb
import settings
# Open database connection
db = MySQLdb.connect(WORDPRESS_DATABASES["localhost","testuser","test123","TESTDB" )
for i, wp in WORDPRESS_DATABASES.items():
file.write("Wordpress: '{0}' ".format(i) + '\n')
db = MySQLdb.connect(wp['HOST'],wp['USER'],wp['PASSWORD'],wp['DATABASE'])
cursor = db.cursor()
# users, posts, pages
for j, stats in WORDPRESS_STATS.items():
cursor.execute(stats['count'])
count = cursor.fetchone()
if count > 0:
# prepare a cursor object using cursor() method
cursor = db.cursor()
file.write("{0} new {1}\n".format(''.join(map(str,count)), j) )
file.write(stats['header'] + "\n")
# execute SQL query using execute() method.
cursor.execute(stats['query'])
for row in cursor:
file.write(' | '.join(map(str, row)) + '\n')
# check for new posts,
query_count_posts = "select count(*) from wp_posts where post_type like 'post'"
file.write(' \n')
cursor.execute(query_count_posts)
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print "Database version : %s " % data
db.close()
# disconnect from server
db.close()
\ No newline at end of file
file.close()
\ No newline at end of file
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.mysql',
# 'NAME': '',
# 'USER': '',
# 'PASSWORD': '',
# 'HOST': '',
# 'PORT': '',
# 'DATABASE: '',
# }
# }
\ No newline at end of file
# }
WORDPRESS_STATS = {
'users': {
'count': "SELECT COUNT(*) FROM wp_users WHERE user_registered >= NOW() - INTERVAL 1 DAY",
'header': 'ID | user_login | display_name | user_registered',
'query': "SELECT ID, user_login, display_name, user_registered FROM wp_users WHERE user_registered >= NOW() - INTERVAL 1 DAY",
},
'posts': {
'count': "SELECT COUNT(*) FROM wp_posts WHERE post_type LIKE post AND post_modified >= NOW() - INTERVAL 1 DAY",
'header': 'ID | post_author | post_modified | post_title',
'query': "SELECT ID, post_author, post_modified, post_title FROM wp_posts WHERE post_type LIKE post AND post_modified >= NOW() - INTERVAL 1 DAY",
},
'pages': {
'count': "SELECT COUNT(*) FROM wp_posts WHERE post_type LIKE page AND post_modified >= NOW() - INTERVAL 1 DAY",
'header': 'ID | post_author | post_modified | post_title',
'query': 'SELECT ID, post_author, post_modified, post_title FROM wp_posts WHERE post_type LIKE page AND post_modified >= NOW() - INTERVAL 1 DAY',
},
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment