From ThirdMartini

The attached python script will generate a xbox Live gamer tag like the example below for any given xbox Live user. I run the script from a 5 minute crontab file to keep the status up to date. The script uses a public xbox API bridge provided by Duncan Mackenzie.


Example:


#
#    Copyright (C) 2009  Sebastian Sobolewski (sebshop@thirdmartini.com)
#
#    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
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    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/>.
#
import Image;
import ImageDraw;
import ImageFont;
import urllib2;
import xml.etree.ElementTree;

optionFont = "FreeSans.ttf";


class Gamertag:
   """XBOX Gamertag class"""

   def __init__(self, gamertag):
      self.gamertag = gamertag;
      self.info = ""
      self.info2 = ""
      self.status = "Offline"
      self.avatarUrl = "";
      self.lastPlayed = "";
      self.lastPlayedUrl = ""
      self.current = "";
      self.allPlayed = {};
      self.last = "";
      self.score = "";

      request = urllib2.Request("http://duncanmackenzie.net/services/GetXboxInfo.aspx?GamerTag="+self.gamertag);
      fd = urllib2.urlopen(request);
      data = fd.read();
      fd.close();

      tree = xml.etree.ElementTree.fromstring(data);
      element = tree.find('XboxInfo');
      for child in tree.getiterator() :
         if child.tag == "Title" :
            self.current = child.text;
         if child.tag == "Info" :
            if child.text :
               self.info = child.text;
         if child.tag == "Info2" :
            if child.text :
               self.info2 = child.text;
         if child.tag == "StatusText":
            self.status = child.text;
         if child.tag == "TileUrl":
            self.avatarUrl = child.text;
         if child.tag == "GamerScore" :
            self.score = child.text;
         if child.tag == "Name":
            self.last = child.text;
            self.allPlayed[child.text] = "";
         if child.tag == "Image32Url":
            self.allPlayed[self.last] = child.text;
            break;

   def getName(self) :
      return self.gamertag;

   def getInfo(self) :
      return self.info;

   def getInfo2(self) :
      return self.info2;

   def getGamerScore(self) :
      return self.score;

   def getStatus(self) :
      return self.status;

   def getAvatarUrl(self) :
      return self.avatarUrl;

   def getCurrent(self) :
      return self.current;

   def getCurrentImage(self) :
      imageFile = "dashboard.jpg";

      if self.allPlayed.has_key(self.getCurrent()) :
         imageFile = "last.jpg";

         url = self.allPlayed[self.getCurrent()]
         request = urllib2.Request(url);
         fd = urllib2.urlopen(request);
         data = fd.read();
         fd.close();

         fd = open(imageFile,"w");
         fd.write(data);
         fd.close();

      if self.current == "Xbox 360 Dashboard" :
         imageFile = "dashboard.jpg"

      last = Image.open(imageFile);
      return last;

   def getAvatarImage(self) :
      request = urllib2.Request(self.getAvatarUrl());
      fd = urllib2.urlopen(request);
      data = fd.read();
      fd.close();

      fd = open("avatar.png","w");
      fd.write(data);
      fd.close();

      avatar = Image.open("avatar.png");
      return avatar;

   def isOnline(self) :
      if self.status == "Online" :
         return True;
      return False;


   def getGamertagBanner(self) :
      xoff = 10;

      last   = self.getCurrentImage();

      im = Image.new("RGBA", (440, 64));
      draw = ImageDraw.Draw(im);
      font12 = ImageFont.truetype(optionFont, 12)
      font20 = ImageFont.truetype(optionFont, 20)

      last   = self.getCurrentImage();
      avatar = self.getAvatarImage();

      x,y = avatar.size;
      im.paste(avatar, (xoff+0,0,xoff+x,y))

      x1,y1 = last.size;
      im.paste(last, (xoff+x, 0, xoff+x+x1, y1 ))


      if self.isOnline() :
         draw.text((xoff+x+x1+10,1), self.getCurrent(), font=font20,fill=(0,0,0));
         draw.text((xoff+x+x1+10,20), self.getInfo2(), font=font12,fill=(0,0,0));
      else :
         draw.text((xoff+x+x1+10,1), "Offline", font=font20,fill=(0,0,0));
         draw.text((xoff+x+x1+10,20), self.getInfo(), font=font12,fill=(0,0,0));

      draw.rectangle((xoff+x+5, 45, 395, 64), fill=(128,128,128));
      draw.text((xoff+x+7, 50), self.getName(), font=font12, fill=(255,255,255));

      (w,h) = font12.getsize(self.getGamerScore());
      draw.text((393-w, 50),  self.getGamerScore(), font=font12, fill=(255,255,255));

      draw.line( ((0,63),(405, 63)),fill=(0,0,0));
      draw.line( ((xoff+x+5,63),(xoff+x+5, 45), (395, 45), (395, 63) ),fill=(0,0,0));

      im.save(self.gamertag+".png", "png");
      return "";



gamertag = Gamertag("MyGamerTagHere");
gamertag.getGamertagBanner();



Personal tools