Guest User

Steam ID -> Community ID Python Converter

a guest
Apr 10th, 2011
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. import sys, os, re
  2.  
  3. SID_NONE    = 1<<0
  4. SID_PRINT   = 1<<1
  5. SID_PROCESS = 1<<2
  6. SID_QUIET   = 1<<3
  7.  
  8. def about():
  9.     print "Steam Id <-> Community ID Converter\n" \
  10.           "Programmed by theY4Kman, algorithm discovered by voogru\n"
  11.  
  12. def syntax(command=None):
  13.     if command == ["help", "?", "/?"]:
  14.         print "Displays the help message."
  15.     else:
  16.         print "Usage:  ", os.path.basename(sys.argv[0]), " SteamID|CommunityID [/p] [/q]\n" \
  17.               "Options:\n" \
  18.               "         /p  Show the process of deriving the Steam/Community ID\n" \
  19.               "         /q  Quiet mode. Only print out the Steam/Community ID"
  20.  
  21. def proc(message, check=SID_NONE, flags=SID_NONE):
  22.     if flags & check: print message
  23.  
  24. def convert(i, flags=SID_NONE):
  25.     steamid = re.compile(r'STEAM_0:([01]):(\d{1,10})').match(i)
  26.     commid  = re.compile(r"(\d{17})").match(i)
  27.     if steamid:
  28.         proc("Steam ID -> Community ID\n\n%s" % i, SID_PROCESS, flags)
  29.         tok = steamid.groups()
  30.         auth = int(tok[1]) * 2
  31.         server = int(tok[0])
  32.         proc("%s * 2 = %d" % (tok[1], auth), SID_PROCESS, flags)
  33.         tok = auth + 76561197960265728 + server
  34.         proc("%d + 76561197960265728 + %d = %d" % (auth, server, tok), SID_PROCESS, flags)
  35.         if flags & SID_PRINT: print tok
  36.         else: return tok
  37.     else:
  38.         proc("Community ID -> Steam ID\n", SID_PROCESS, flags)
  39.         tok = int(i)
  40.         server = tok % 2
  41.         proc("%s is %s, so the auth server is %d" % (i, (server and "odd") or "even", server), SID_PROCESS, flags)
  42.         auth = tok - server
  43.         proc("%d - %d = %d" % (tok, server, auth), SID_PROCESS, flags)
  44.         auth -= 76561197960265728
  45.         proc("%d - 76561197960265728 = %d" % (tok - server, auth), SID_PROCESS, flags)
  46.         proc("%d / 2 = %d" % (auth, auth/2), SID_PROCESS, flags)
  47.         auth /= 2
  48.         if flags & SID_PRINT: print "\nSteam ID: STEAM_0:%d:%d" % (server, auth)
  49.         else: return "STEAM_0:%d:%d" % (server, auth)
  50.     return
  51. if __name__ == '__main__':
  52.     if len(sys.argv) < 2:
  53.         syntax()
  54.         exit()
  55.     if sys.argv[1] == "about":
  56.         about()
  57.         exit()
  58.     if sys.argv[1] in ["help", "?", "/?"]:
  59.         syntax((len(sys.argv) >= 3) and sys.argv[2] or None)
  60.         exit()
  61.    
  62.     flags = SID_NONE
  63.     i = None
  64.     for option in sys.argv[1:]:
  65.         if option == "/p": flags |= SID_PROCESS
  66.         if option == "/q": flags |= SID_QUIET
  67.         else:
  68.             if not i and re.compile(r'STEAM_0:[01]:\d{1,10}').match(option) or re.compile(r"\d{17}").match(option): i = option
  69.  
  70.     if not i: # Then whom?
  71.         syntax()
  72.     else:
  73.         if flags & (SID_QUIET|SID_PROCESS) == (SID_QUIET|SID_PROCESS): flags ^= SID_PROCESS
  74.         convert(i, flags|SID_PRINT)
Advertisement
Add Comment
Please, Sign In to add comment