LexManos

FantasyGrounds_DLC_Updater.py

Jul 6th, 2016
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. import os, json, urllib2, sys
  2. reload(sys)
  3. sys.setdefaultencoding("utf-8")
  4. from pprint import pprint
  5.  
  6. def load_list():
  7.     if not os.path.isfile('DLC_LIST.txt'):
  8.         return {}
  9.    
  10.     ret = {}
  11.     with open('DLC_LIST.txt') as f:
  12.         for line in f.readlines():
  13.             if not line[0] == '#':
  14.                 pts = line.split(' ', 1)
  15.                 ret[pts[0]] = pts[1].rstrip('\n')
  16.    
  17.     return ret
  18.  
  19. def getData(id):
  20.     try:
  21.         response = urllib2.urlopen('http://store.steampowered.com/api/appdetails/?appids=%s' % id)
  22.         ret = json.loads(response.read())
  23.         if ret == None:
  24.             print('Temporarly blocked from steam API, try again later')
  25.             return None
  26.         return ret['%s' % id]['data']
  27.     except:
  28.         print('Temporarly blocked from steam API, try again later')
  29.         return None        
  30.  
  31. list = load_list()
  32. print('# Known DLC: %s' % len(list))
  33.  
  34. parent = getData('252690')
  35. if not parent == None:
  36.     count = 0
  37.     for num in parent['dlc']:
  38.         id = '%s' % num
  39.         if not id in list:
  40.             dlc = None #getData(num)
  41.            
  42.             if dlc is None:
  43.                 print('')
  44.                 print('API Ban while iterating new DLC, updating list.')
  45.                 print('Run this script again in a few mins to get the rest')
  46.                 print('')
  47.                 break
  48.                
  49.             if (dlc['type'] == 'dlc'):
  50.                 print('New DLC: %s %s' % (dlc['steam_appid'], dlc['name']))
  51.                 count += 1
  52.                 list[id] = dlc['name']
  53.  
  54.     print ('# New DLC %s' % count)
  55.     print ('# Total DLC %s' % len(list))    
  56.    
  57.     with open('DLC_LIST.txt', 'w') as f:
  58.         f.write('# Add DLC here, Format is "APPID App Name" APPID MUST be a valid APPID\n')
  59.         f.write('# See the following website for a complete list:\n')
  60.         f.write('# https://steamdb.info/app/252690/dlc/\n')
  61.         f.write('# Total DLC: %s\n' % len(list))
  62.         f.write('#\n')
  63.         f.write('\n'.join(['%s %s' % (id, list[id]) for id in sorted(list.keys())]))
  64.         f.write('\n')
Advertisement
Add Comment
Please, Sign In to add comment