mirror of
https://github.com/creyD/asiimov.git
synced 2026-06-12 00:52:23 +02:00
Started fixing the inventory retrieval
- Merged steam_api methods to views - Added inventory2 for items like cases - Started fixing the inventory retrieval
This commit is contained in:
18
src/core/migrations/0009_gamer_inventory_2.py
Normal file
18
src/core/migrations/0009_gamer_inventory_2.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 3.0.2 on 2020-01-24 22:59
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('core', '0008_auto_20200124_0556'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='gamer',
|
||||||
|
name='inventory_2',
|
||||||
|
field=models.ManyToManyField(to='core.ItemType'),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -95,7 +95,8 @@ class Gamer(models.Model):
|
|||||||
tradeurl = models.URLField(max_length=256, null=True)
|
tradeurl = models.URLField(max_length=256, null=True)
|
||||||
|
|
||||||
# Asiimov specific information
|
# Asiimov specific information
|
||||||
inventory = models.ManyToManyField(ItemInstance) # Temporary storage of items for improving site performance
|
inventory = models.ManyToManyField(ItemInstance) # For skins
|
||||||
|
inventory_2 = models.ManyToManyField(ItemType) # For cases, badges usw
|
||||||
badges = models.ManyToManyField(Badge)
|
badges = models.ManyToManyField(Badge)
|
||||||
system_user = models.OneToOneField(User, on_delete=models.CASCADE)
|
system_user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||||
offer_count = models.IntegerField(default=0)
|
offer_count = models.IntegerField(default=0)
|
||||||
|
|||||||
@@ -1,89 +0,0 @@
|
|||||||
# For communication with the Steam API
|
|
||||||
import urllib.request
|
|
||||||
import json
|
|
||||||
# For getting the STEAM_API_KEY
|
|
||||||
from django.conf import settings
|
|
||||||
from .models import Gamer, ItemType, ItemInstance
|
|
||||||
from django.shortcuts import get_object_or_404
|
|
||||||
|
|
||||||
# Official Steam Servers
|
|
||||||
STEAM_SERVER = 'https://api.steampowered.com/'
|
|
||||||
USER_METHOD = 'ISteamUser/GetPlayerSummaries/v2'
|
|
||||||
INVENTORY_SERVER = 'https://steamcommunity.com/inventory/'
|
|
||||||
|
|
||||||
# CSGOFloats API Server (https://csgofloat.com)
|
|
||||||
FLOAT_SERVER = 'https://api.csgofloat.com/?url='
|
|
||||||
|
|
||||||
|
|
||||||
# Get the mandatory gamer info for a gamer
|
|
||||||
def getUserInfo(steamID, API_KEY=settings.STEAM_API_KEY):
|
|
||||||
QUERY = STEAM_SERVER + USER_METHOD + '/?key=' + str(API_KEY) + '&format=json&steamids=' + str(steamID)
|
|
||||||
player_object = json.load(urllib.request.urlopen(QUERY))
|
|
||||||
return player_object['response']['players'][0]
|
|
||||||
|
|
||||||
|
|
||||||
def getInstanceData(inventory_object, instance_id):
|
|
||||||
for key in inventory_object['descriptions']:
|
|
||||||
if key['instanceid'] == instance_id:
|
|
||||||
return key
|
|
||||||
|
|
||||||
|
|
||||||
def getFloat(inspect_link):
|
|
||||||
QUERY = FLOAT_SERVER + inspect_link
|
|
||||||
float_object = json.load(urllib.request.urlopen(QUERY))
|
|
||||||
if 'error' in float_object:
|
|
||||||
return False
|
|
||||||
return float_object
|
|
||||||
|
|
||||||
|
|
||||||
def getSubTag(scope, search_key, search_value):
|
|
||||||
for tag in scope:
|
|
||||||
if tag.search_key == search_value:
|
|
||||||
return tag
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
# Get the CS:GO inventory of a gamer
|
|
||||||
def updateInventory(steamID, GAME_ID=730):
|
|
||||||
gamer = get_object_or_404(Gamer, steamid=steamID)
|
|
||||||
QUERY = INVENTORY_SERVER + str(steamID) + '/' + str(GAME_ID) + '/2?l=english&count=5000'
|
|
||||||
inventory_object = json.load(urllib.request.urlopen(QUERY))
|
|
||||||
if 'success' in inventory_object:
|
|
||||||
for item in inventory_object['assets']:
|
|
||||||
if item['instanceid'] == 0:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
instance_data = getInstanceData(inventory_object, item['instanceid'])
|
|
||||||
link = instance_data['actions'][0]['link'].replace("%owner_steamid%", str(steamID)).replace("%assetid%", str(item['instanceid']))
|
|
||||||
while (True):
|
|
||||||
item_infos = getFloat(link)
|
|
||||||
if item_infos:
|
|
||||||
break
|
|
||||||
item_class = ItemType.objects.get_or_create(
|
|
||||||
paint_index=item_infos['iteminfo']['paintindex'],
|
|
||||||
wear=item_infos['iteminfo']['wear_name'],
|
|
||||||
classid=item['classid'],
|
|
||||||
appid=item['appid'],
|
|
||||||
tradable=(True if item['marketable'] == 1 else False),
|
|
||||||
icon_url=item_infos['iteminfo']['imageurl'],
|
|
||||||
name=item['name'],
|
|
||||||
name_color=item['name_color'],
|
|
||||||
type=item['type'],
|
|
||||||
rarity=getSubTag(item['tags'], 'category', 'Rarity').localized_tag_name,
|
|
||||||
min_float=item_infos['iteminfo']['min'],
|
|
||||||
max_float=item_infos['iteminfo']['max']
|
|
||||||
)
|
|
||||||
new_item = ItemInstance.objects.get_or_create(
|
|
||||||
item_class=item_class[0],
|
|
||||||
instanceid=item['instanceid'],
|
|
||||||
market_tradable_restriction=(item['owner_descriptions'][1]['value']
|
|
||||||
if 'owner_descriptions' in item else None),
|
|
||||||
inspect_link=instance_data['actions'][0]['link'],
|
|
||||||
float=item_infos['iteminfo']['floatvalue'],
|
|
||||||
paintseed=item_infos['iteminfo']['paintseed'],
|
|
||||||
killeatervalue=(item_infos['iteminfo']['killeatervalue'] if 'killeatervalue' in item_infos['iteminfo'] else None),
|
|
||||||
customname=(item_infos['iteminfo']['customname'] if 'customname' in item_infos['iteminfo'] else None)
|
|
||||||
)
|
|
||||||
gamer.inventory.add(new_item)
|
|
||||||
return inventory_object
|
|
||||||
return False
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
# Import the local models
|
# Import the local models
|
||||||
from .models import Offer, Gamer
|
from .models import Offer, Gamer, ItemType, ItemInstance
|
||||||
# Django shortcuts for certain things
|
# Django shortcuts for certain things
|
||||||
from django.shortcuts import render, get_object_or_404, redirect
|
from django.shortcuts import render, get_object_or_404, redirect
|
||||||
# For catching permission errors
|
# For catching permission errors
|
||||||
@@ -12,12 +12,22 @@ from urllib import parse
|
|||||||
import requests
|
import requests
|
||||||
# For manually creating system users
|
# For manually creating system users
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
# For getting the API interaction methods
|
|
||||||
from .steam_api import getUserInfo, updateInventory
|
|
||||||
# Import for manually logging in user after creation
|
# Import for manually logging in user after creation
|
||||||
from django.contrib.auth import login
|
from django.contrib.auth import login
|
||||||
|
# For communication with the steam api
|
||||||
|
import json
|
||||||
|
import urllib.request
|
||||||
from .forms import ChangeTradeUrl, CreateOffer
|
from .forms import ChangeTradeUrl, CreateOffer
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
# STATIC VARIABLES
|
||||||
|
# Official Steam Servers
|
||||||
|
STEAM_SERVER = 'https://api.steampowered.com/'
|
||||||
|
USER_METHOD = 'ISteamUser/GetPlayerSummaries/v2'
|
||||||
|
INVENTORY_SERVER = 'https://steamcommunity.com/inventory/'
|
||||||
|
|
||||||
|
# CSGOFloats API Server (https://csgofloat.com)
|
||||||
|
FLOAT_SERVER = 'https://api.csgofloat.com/?url='
|
||||||
|
|
||||||
|
|
||||||
# HELPER
|
# HELPER
|
||||||
@@ -34,6 +44,34 @@ def validate_steam_login(params):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def getInstanceData(inventory_object, instance_id):
|
||||||
|
for key in inventory_object['descriptions']:
|
||||||
|
if key['instanceid'] == instance_id:
|
||||||
|
return key
|
||||||
|
|
||||||
|
|
||||||
|
def getFloat(inspect_link):
|
||||||
|
QUERY = FLOAT_SERVER + inspect_link
|
||||||
|
print(QUERY)
|
||||||
|
float_object = json.load(urllib.request.urlopen(QUERY))
|
||||||
|
if 'error' in float_object:
|
||||||
|
return False
|
||||||
|
return float_object
|
||||||
|
|
||||||
|
|
||||||
|
def getSubTag(scope, search_key, search_value):
|
||||||
|
for tag in scope:
|
||||||
|
if tag.search_key == search_value:
|
||||||
|
return tag
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def getUserInfo(steamID, API_KEY=settings.STEAM_API_KEY):
|
||||||
|
QUERY = STEAM_SERVER + USER_METHOD + '/?key=' + str(API_KEY) + '&format=json&steamids=' + str(steamID)
|
||||||
|
player_object = json.load(urllib.request.urlopen(QUERY))
|
||||||
|
return player_object['response']['players'][0]
|
||||||
|
|
||||||
|
|
||||||
# STATIC PAGES
|
# STATIC PAGES
|
||||||
def help(request):
|
def help(request):
|
||||||
return render(request, 'static_pages/help.html')
|
return render(request, 'static_pages/help.html')
|
||||||
@@ -73,7 +111,6 @@ def signup(request):
|
|||||||
'openid.return_to': 'http://' + request.META['HTTP_HOST'] + '/signup_confirm',
|
'openid.return_to': 'http://' + request.META['HTTP_HOST'] + '/signup_confirm',
|
||||||
'openid.realm': 'http://' + request.META['HTTP_HOST'] + ''
|
'openid.realm': 'http://' + request.META['HTTP_HOST'] + ''
|
||||||
}
|
}
|
||||||
|
|
||||||
query_string = parse.urlencode(u)
|
query_string = parse.urlencode(u)
|
||||||
auth_url = steam_openid_url + '?' + query_string
|
auth_url = steam_openid_url + '?' + query_string
|
||||||
return redirect(auth_url)
|
return redirect(auth_url)
|
||||||
@@ -180,7 +217,60 @@ def me_inventory(request):
|
|||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def profile_inventory_update(request, steamID):
|
def profile_inventory_update(request, steamID):
|
||||||
updateInventory(steamID)
|
gamer = get_object_or_404(Gamer, steamid=steamID)
|
||||||
|
QUERY = INVENTORY_SERVER + str(steamID) + '/' + str(730) + '/2?l=english&count=5000'
|
||||||
|
try:
|
||||||
|
inventory_object = json.load(urllib.request.urlopen(QUERY))
|
||||||
|
except urllib.URLError:
|
||||||
|
print("Error accessing Steam")
|
||||||
|
if 'success' in inventory_object:
|
||||||
|
for item in inventory_object['assets']:
|
||||||
|
if item['instanceid'] == 0:
|
||||||
|
item_class = ItemType.objects.get_or_create(
|
||||||
|
classid=item['classid'],
|
||||||
|
appid=item['appid'],
|
||||||
|
tradable=(True if item['marketable'] == 1 else False),
|
||||||
|
rarity=getSubTag(item['tags'], 'category', 'Rarity').localized_tag_name
|
||||||
|
)
|
||||||
|
gamer.inventory_2.add(item_class[0])
|
||||||
|
else:
|
||||||
|
print("Getting item: " + str(item))
|
||||||
|
instance_data = getInstanceData(inventory_object, item['instanceid']) # TODO Fix Bug Here
|
||||||
|
link = FLOAT_SERVER + instance_data['actions'][0]['link'].replace("%owner_steamid%",
|
||||||
|
str(steamID)).replace("%assetid%", str(item['instanceid']))
|
||||||
|
|
||||||
|
try:
|
||||||
|
item_infos = json.load(urllib.request.urlopen(link))
|
||||||
|
item_class = ItemType.objects.get_or_create(
|
||||||
|
paint_index=item_infos['iteminfo']['paintindex'],
|
||||||
|
wear=item_infos['iteminfo']['wear_name'],
|
||||||
|
classid=item['classid'],
|
||||||
|
appid=item['appid'],
|
||||||
|
tradable=(True if item['marketable'] == 1 else False),
|
||||||
|
icon_url=item_infos['iteminfo']['imageurl'],
|
||||||
|
name=item_infos['name'],
|
||||||
|
name_color=item_infos['name_color'],
|
||||||
|
type=item_infos['type'],
|
||||||
|
rarity=getSubTag(item['tags'], 'category', 'Rarity').localized_tag_name,
|
||||||
|
min_float=item_infos['iteminfo']['min'],
|
||||||
|
max_float=item_infos['iteminfo']['max']
|
||||||
|
)
|
||||||
|
new_item, created = ItemInstance.objects.get_or_create(
|
||||||
|
item_class=item_class[0],
|
||||||
|
instanceid=item['instanceid'],
|
||||||
|
market_tradable_restriction=(item['owner_descriptions'][1]['value']
|
||||||
|
if 'owner_descriptions' in item else None),
|
||||||
|
inspect_link=instance_data['actions'][0]['link'],
|
||||||
|
float=item_infos['iteminfo']['floatvalue'],
|
||||||
|
paintseed=item_infos['iteminfo']['paintseed'],
|
||||||
|
killeatervalue=(item_infos['iteminfo']['killeatervalue'] if 'killeatervalue' in item_infos['iteminfo'] else None),
|
||||||
|
customname=(item_infos['iteminfo']['customname'] if 'customname' in item_infos['iteminfo'] else None)
|
||||||
|
)
|
||||||
|
gamer.inventory.add(new_item)
|
||||||
|
except:
|
||||||
|
print("Error Adding Item")
|
||||||
|
else:
|
||||||
|
print("STEAM API CALL NOT SUCCESSFULL!")
|
||||||
return redirect(profile_inventory, steamID=steamID)
|
return redirect(profile_inventory, steamID=steamID)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user