Autotagger
Yet another script from the bad-hack-department. I wrote this script to add tags to my articles based on the text in the body of the article. So all articles containing the string “oracle” are tagged with the tag “oracle”. It directly accesses the database behind the blog software i’m using for c0t0d0s0.org:
import MySQLdb
import sys
try:
conn = MySQLdb.connect (host = "localhost",
user = "user",
passwd = "password",
db = "database")
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
if sys.argv[1] == '':
print "no tag"
sys.exit(2)
tag2masstag=sys.argv[1]
print tag2masstag
cursor=conn.cursor()
cursor.execute ("""SELECT id FROM serendipity_entries
WHERE lower(body) REGEXP ' %s '""" % (tag2masstag))
rows = cursor.fetchall()
for row in rows:
print "found in %s" % (row[0])
cursor2=conn.cursor()
cursor2.execute ("""SELECT * FROM serendipity_entrytags
WHERE lower(tag)='%s'
AND entryid=%s""" % (tag2masstag,row[0]))
if cursor2.rowcount != 0:
print "Already tagged"
else:
print "Not tagged"
cursor3=conn.cursor()
cursor3.execute("""INSERT INTO serendipity_entrytags (entryid,tag)
VALUES(%s,'%s')""" % (row[0],tag2masstag))
cursor3.close
cursor2.close
print "----executed"