Advertisement
Guest User

Path of Exile Index Parser

a guest
Aug 22nd, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. A basic script used to consume the Path of Exile public stash tab api and find items based on a search parameter.
  4. """
  5. import re
  6. import requests
  7. __name__ = '__main__'
  8.  
  9.  
  10. def main():
  11.     url = 'http://www.pathofexile.com/api/public-stash-tabs'
  12.     head = {'Content-type': 'application/json', 'Accept-Encoding': 'gzip'}
  13.     next_change_id = get_next_change_id(head)
  14.     search_param = input('Please enter an item name: ').strip().upper()
  15.  
  16.     parse_river(url, head, next_change_id, search_param)
  17.  
  18.  
  19. def get_next_change_id(head):
  20.     """
  21.    Consumed the poe.ninja api for the latest next_change_id
  22.  
  23.    :return: The next_change_id
  24.    """
  25.     url = 'http://api.poe.ninja/api/Data/GetStats'
  26.     result = requests.get(url, headers=head).json()
  27.     return result['nextChangeId']
  28.  
  29.  
  30. def parse_river(url, head, next_change_id, search_param):
  31.     """
  32.    Parses the public stash tab api river and searches for any items whose name contains the search_param
  33.  
  34.    :param url: Path of Exile public stash tab api url
  35.    :param head: dictionary containing request headers
  36.    :param next_change_id: the id of the api page being requested
  37.    :param search_param: a search parameter used to limit the displayed items
  38.    """
  39.     while next_change_id != '0-0-0-0-0':
  40.         result = requests.get(url + '?id=' + next_change_id, headers=head).json()
  41.         next_change_id = result['next_change_id']
  42.         for stash in result['stashes']:
  43.             for item in stash['items']:
  44.                 if search_param in item['name'].upper():
  45.                     print(get_whisper_message(item, stash))
  46.         print('')
  47.     print('Reached live.')
  48.  
  49.  
  50. def get_whisper_message(item, stash):
  51.     """
  52.    Generates a trade whisper message for the found item
  53.  
  54.    :param item: dictionary containing information on the found item
  55.    :param stash: dictionary containing information on the stash where the item was found
  56.    :return: trade whisper message
  57.    """
  58.     character_name = stash['lastCharacterName']
  59.     item_name = re.sub('<<.*?>>', '', item['name'])
  60.     try:
  61.         price = ' listed for ' + item['note'].replace('~b/o ', '')
  62.     except KeyError:
  63.         price = ''
  64.     league = item['league']
  65.     tab_name = stash['stash']
  66.     left_pos = str(item['x'])
  67.     top_pos = str(item['y'])
  68.     return '@' + character_name + ' Hi, I would like to buy your ' + item_name + price + ' in ' + league + \
  69.            ' (stash tab "' + tab_name + '"; position: left ' + left_pos + ', top ' + top_pos + ')'
  70.  
  71. if __name__ == '__main__':
  72.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement