Advertisement
Guest User

Path of Exile Index Parser

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