Advertisement
Azelphur

Untitled

Feb 12th, 2012
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # =============================================================================
  3. # Steam ID Converter
  4. # Copyright (C) 2008-2010 Zach "theY4Kman" Kanzler
  5. # =============================================================================
  6. #
  7. # This program is free software; you can redistribute it and/or modify it under
  8. # the terms of the GNU General Public License, version 3.0, as published by the
  9. # Free Software Foundation.
  10. #
  11. # This program is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU General Public License along with
  17. # this program. If not, see <http://www.gnu.org/licenses/>.
  18.  
  19. import re
  20. import urllib
  21. from xml.dom import minidom
  22. from xml.parsers.expat import ExpatError
  23.  
  24. class SteamIDError(Exception):
  25. pass
  26.  
  27.  
  28. class SteamCommunityProfile:
  29. '''Class that, given necessary details, returns all information on a Steam
  30. Community profile.'''
  31.  
  32. rgx_steamid = re.compile("STEAM_[0-5]:([01]):([0-9]+)", re.I) # group 1, 2
  33. rgx_commid = re.compile("(https?:\/\/steamcommunity\.com\/(profiles?|id)\/)?([0-9]{17,18})/?", re.I) # group 3
  34. rgx_userid = re.compile("(https?:\/\/steamcommunity\.com\/(profiles?|id)\/)?([^\/]+)/?", re.I) # group 3
  35. #rgx_addfriend = re.compile(".*<a href=\"steam:\/\/friends\/add\/([0-9]{17,18})\">.*", re.I|re.U) # group 1 BROKEN
  36. rgx_addfriend = re.compile(".*\<a class=\"linkAction\" href=\"http:\/\/steamcommunity\.com\/actions\/AddFriend\/([0-9]{17,18})\"\>Add to your friends list\<\/a\>.*", re.I|re.U) # group 1
  37.  
  38. rgx_player_search = re.compile('''<div class="resultItem">\s*<div class="pgtag">Player</div>\s*<div class="groupBlockMedium">\s*<div class="mediumHolder_default"><div class="avatarMedium"><a href="(?P<url>[^"]+)">''')
  39.  
  40.  
  41. @staticmethod
  42. def STEAMID2COMMID(match):
  43. iServer = int(match.group(1))
  44. iFriend = int(match.group(2)) * 2
  45. iFriend += 76561197960265728 + iServer
  46.  
  47. return iFriend
  48.  
  49. @staticmethod
  50. def COMMID2STEAMID(commid):
  51. iFriend = int(commid)
  52.  
  53. iServer = iFriend % 2
  54. iFriend -= iServer + 76561197960265728
  55. iFriend /= 2
  56.  
  57. return "STEAM_0:%d:%d" % ( iServer, iFriend )
  58.  
  59. @staticmethod
  60. def USERID2COMMID(userid):
  61. url = "http://steamcommunity.com/id/%s" % userid
  62. page = urllib.urlopen(url)
  63. if not page:
  64. raise SteamIDError("error connecting to profile page.")
  65.  
  66. match = SteamCommunityProfile.rgx_addfriend.search(page.read())
  67. if match is None:
  68. url = "http://steamcommunity.com/actions/Search?K=%s" % userid
  69. page = urllib.urlopen(url)
  70. if not page:
  71. raise SteamIDError("error connecting to the player search page.")
  72.  
  73. search = SteamCommunityProfile.rgx_player_search.search(page.read())
  74. if search is None:
  75. raise SteamIDError("name does not exist: http://steamcommunity.com/id/\x02%s\x02/" % userid)
  76.  
  77. scurl = search.group(1)
  78. comm = SteamCommunityProfile.rgx_commid.match(scurl)
  79. if comm is not None:
  80. return comm.group(3)
  81.  
  82. uid = SteamCommunityProfile.rgx_userid.match(scurl)
  83. if uid is not None:
  84. return SteamCommunityProfile.USERID2COMMID(uid.group(3))
  85.  
  86. raise SteamIDError("name does not exist: http://steamcommunity.com/id/\x02%s\x02/" % userid)
  87.  
  88. return match.group(1)
  89.  
  90. @staticmethod
  91. def URL2FRIENDSNAME(url):
  92. """
  93. Accepts a profile URL and turns it into a Steam Friends name, e.g.
  94. http://steamcommunity.com/id/theY4Kman/
  95. """
  96.  
  97. @staticmethod
  98. def input_to_profile(text):
  99. name = None
  100. # First check if input is a Steam ID
  101. match = SteamCommunityProfile.rgx_steamid.match(text)
  102. if match is not None:
  103. return SteamCommunityProfile(commid=SteamCommunityProfile.STEAMID2COMMID(match))
  104.  
  105. # Next we try for a Community ID
  106. else:
  107. match = SteamCommunityProfile.rgx_commid.match(text)
  108. if match is not None:
  109. return SteamCommunityProfile(commid=match.group(3))
  110.  
  111. # And lastly for a URL User ID
  112. else:
  113. match = SteamCommunityProfile.rgx_userid.match(text)
  114. if match is not None:
  115. return SteamCommunityProfile(commid=SteamCommunityProfile.USERID2COMMID(match.group(3)))
  116.  
  117. return None
  118.  
  119. def __init__(self, commid):
  120. self.commid = long(commid)
  121.  
  122. self.name = None
  123. self.steamid = None
  124. self.url = None
  125.  
  126. self.update_data()
  127.  
  128. def __str__(self):
  129. fmt = {
  130. 'steamid': self.steamid,
  131. 'name': self.name,
  132. 'url': self.url,
  133. 'commid': self.commid,
  134. 'commurl': "http://steamcommunity.com/profiles/%d/" % self.commid
  135. }
  136.  
  137. if self.name is None:
  138. return '%(steamid)s: %(url)s (or %(commurl)s )' % fmt
  139. else:
  140. return '%(name)s (%(steamid)s): %(url)s (or %(commurl)s )' % fmt
  141.  
  142. def update_data(self):
  143. # Easiest: convert commid to steamid
  144. self.steamid = SteamCommunityProfile.COMMID2STEAMID(self.commid)
  145.  
  146. url = "http://steamcommunity.com/profiles/%d/?xml=1" % self.commid
  147. try:
  148. page = urllib.urlopen(url)
  149. except IOError,e:
  150. raise SteamIDError(e)
  151.  
  152. # Retrieve the profile's name from the URL's XML output
  153. try:
  154. xmlpage = urllib.urlopen(url)
  155. except IOError,e:
  156. return
  157. if xmlpage is None:
  158. return
  159.  
  160. try:
  161. doc = minidom.parse(xmlpage)
  162. except ExpatError,e:
  163. raise SteamIDError(e)
  164. if doc is None:
  165. return
  166.  
  167. for node in doc.firstChild.childNodes:
  168. if node.nodeType != minidom.Node.ELEMENT_NODE: continue
  169. if node.nodeName != 'customURL': continue
  170.  
  171. if node.firstChild is None or node.firstChild.nodeValue == '':
  172. self.url = "http://steamcommunity.com/profiles/%d/" % self.commid
  173. else:
  174. self.url = 'http://steamcommunity.com/id/%s/' % node.firstChild.nodeValue
  175. break
  176.  
  177. elem = doc.getElementsByTagName("steamID")
  178. if len(elem) == 0:
  179. return
  180. name = elem[0]
  181. if name == "" or name.firstChild is None:
  182. return
  183. self.name = name.firstChild.nodeValue
  184.  
  185.  
  186. if __name__ == '__main__':
  187. #profile = SteamCommunityProfile.input_to_profile('76561197981373609')
  188. profile = SteamCommunityProfile.input_to_profile('javalia')
  189. print str(profile).encode('ascii', 'replace')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement