Skip to content

Instantly share code, notes, and snippets.

Created October 3, 2011 02:24
Show Gist options
  • Save anonymous/1258304 to your computer and use it in GitHub Desktop.
Save anonymous/1258304 to your computer and use it in GitHub Desktop.
Python script to get Xbox Live friends
#!/usr/local/bin/python
import urllib
import urllib2
import re
import json
import htmlentitydefs
import cookielib
LOGIN,PASSWD = "yourlogin@someemail.com", "yourpassword"
def unescape(text):
def fixup(m):
text = m.group(0)
if text[:2] == "&#":
# character reference
try:
if text[:3] == "&#x":
return unichr(int(text[3:-1], 16))
else:
return unichr(int(text[2:-1]))
except ValueError:
pass
else:
# named entity
try:
text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
except KeyError:
pass
return text # leave as is
return re.sub("&#?\w+;", fixup, text)
URL = "http://live.xbox.com/en-US/friendcenter"
HEADERS = [("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)"),
("Referer", "http://live.xbox.com/en-US/friendcenter?xr=shellnav"), ("X-Requested-With", "XMLHttpRequest")]
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = HEADERS
urllib2.install_opener(opener)
resp = urllib2.urlopen(URL)
data = resp.read()
action = re.findall(r"srf_uPost=\'(.*?)\'", data)[0]
params = re.findall(r"<input.*?name=\"(.*?)\".*?value=\"(.*?)\"", data)
params = filter(lambda p: p[0] != "login" and p[1] != "passwd", params)
params.append(("login", LOGIN))
params.append(("passwd", PASSWD))
resp = urllib2.urlopen(action, urllib.urlencode(params))
data = resp.read()
action = re.findall(r"action=\"(.*?)\"", data)[0]
params = re.findall(r"<input.*?name=\"(.*?)\".*?value=\"(.*?)\"", data)
resp = urllib2.urlopen(action, urllib.urlencode(params))
data = resp.read()
params = re.findall(r"<input.*?name=\"(.*?)\".*?value=\"(.*?)\"", data)
resp = urllib2.urlopen("http://live.xbox.com/en-US/friendcenter/Friends", urllib.urlencode(params))
data = resp.read()
data = unicode(data, "utf8")
friends = []
current = []
for user in json.loads(data, encoding="utf8")['Users']:
if user["IsOnline"]:
presence = user["Presence"].replace("online playing", "").replace("\r\n", " ")
friends.append((user["Gamertag"], presence))
if not friends:
print "No Xbox Live friends online"
exit(0)
for gamertag, game in sorted(friends, key=lambda x:x[0].lower()):
print "%s :: %s" % tuple([s.encode("utf8").strip() for s in (gamertag, game)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment