-
Notifications
You must be signed in to change notification settings - Fork 0
/
steam.py
43 lines (31 loc) · 1.18 KB
/
steam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
from urllib import request
from bs4 import BeautifulSoup
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/listings/<int:app_id>/<item>', methods=['GET'])
def search_items(app_id, item):
url = "http://steamcommunity.com/market/listings/{}/{}".format(app_id, encode(item))
req = request.urlopen(url).read()
soup = BeautifulSoup(req, 'html.parser')
result = {'success': True, 'items': []}
name = soup.select('.market_listing_nav > a')[0].text.strip()
if name:
result['app_name'] = name
for row in soup.find_all('div', class_='market_listing_row'):
price = row.find('span', class_='market_listing_price_with_fee').text.strip()
item_id = clean_id(row['id'])
result['items'].append({'id': item_id, 'price': price})
if not result['items']:
result['success'] = False
result['message'] = 'Item not found.'
del result['items']
return jsonify(result)
def encode(str):
return str.replace(' ', '%20')
def clean_id(str):
return str.replace('listing_', '')
def clean_price(str):
return float(str.strip().split(' ')[1])
if __name__ == '__main__':
app.run(debug=True)