Automatic Game Scrobbling Script

This morning I put together a python script to log which games I play on my PC and scrobble them to my blog.

It’s a pretty simple system that just checks whether or not processes are running and logs run times, but I thought it was neat.

The only problem I’ve found so far is that the WoW process sometimes lives on as a zombie process after the game is closed.

                                                                                                                                                         
import shelve,os,datetime
from datetime import datetime
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts
import sys, getopt

d = shelve.open("/home/acegiak/.scripts/gamelogstore.db",  flag='c')

games = {}
games['The Elder Scrolls Online'] = ['eso.exe','http://www.videogamegeek.com/videogame/124451/elder-scrolls-online']
games['Minecraft'] = ['Minecraft','http://www.videogamegeek.com/videogame/81960/minecraft']
games["Don\'t Starve Together"] = ['dontstarve_steam','http://www.videogamegeek.com/videogame/170660/dont-starve-together']
games["World of Warcraft"] = ['Wow.exe','http://www.videogamegeek.com/videogame/69309/world-warcraft']
games["Civilization V"] = ['Civ5XP','http://www.videogamegeek.com/videogame/80945/civilization-v']
games["Oddworld: Abe's Oddysee: New 'N Tasty"] = ['NNT.x86','http://www.videogamegeek.com/videogame/80272/oddworld-abes-oddysee']

print datetime.now()
print "checking games"

for k in games:
        if os.system("pidof "+games[k][0]) == 256 and os.system("jps | grep "+games[k][0]):
                if k in d:
                        delta =  datetime.now() - d[k]
                        seconds = delta.seconds
                        formatted = '{:02}h{:02}m'.format(seconds // 3600, seconds % 3600 // 60, seconds % 60)
                        print k+" ran for "+formatted
                        del d[k]
                        d.sync()
                        wp = Client('http://acegiak.net/xmlrpc.php', 'USERNAME', 'PASSWORD')
                        post = WordPressPost()
                        post.title = "Played "+k+" for "+formatted
                        post.custom_fields= []
                        mf2cite = {"url":games[k][1],"name":k,"duration":formatted}
                        post.custom_fields.append({
                                'key': 'mf2_cite',
                                'value': mf2cite
                        })
                        post.terms_names = {"kind":["play"],"category":["scrobbles"]}
                        post.post_status = "publish"
                        print wp.call(NewPost(post))
                        print "wordpress call complete"
                        os.system("notify-send \"Logged "+formatted+" "+k+" session to acegiak.net\"")
                else:
                        print k+" is still not running"
        else:
                if k not in d:
                        d[k] = datetime.now()
                        print k+" is now running"
                else:
                        print k+" is still running"

d.sync()
d.close()



Leave a Reply

Your email address will not be published. Required fields are marked *