Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
V0.1.1 - More MiscUtils
  • Loading branch information
davidteather committed Oct 6, 2019
commit d3e1c4d9d16025b67a64a48433b81ed319a76c6a
Binary file modified BackpackTF/__pycache__/account.cpython-37.pyc
Binary file not shown.
Binary file modified BackpackTF/__pycache__/miscutils.cpython-37.pyc
Binary file not shown.
40 changes: 39 additions & 1 deletion BackpackTF/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,42 @@ def search_Classifieds(self, intent="dual", page_size=10, fold=1, item_name="Sod
"https://backpack.tf/api/classifieds/search/v1?" + encoded)
jsondata = json.loads(r.text)

return jsondata
return jsondata


#
# This function extracts the trade url.
#
# listingJSON - the JSON object from the search_listings under whatever the listing thing you use is.
#
def extract_trade_url(self, listingJSON, proxy=None):
from lxml import html
import requests
import urllib.parse
import json

payload = {
"item": listingJSON['item']['name'],
"steamid": listingJSON['steamid'],
"quality": listingJSON['item']['quality']
}

encoded = urllib.parse.urlencode(payload)

if proxy == None:
r = requests.get(
"https://backpack.tf/classifieds?" + encoded.replace("+", "%20"))
else:
r = requests.get(
"https://backpack.tf/classifieds?" + encoded.replace("+", "%20"), proxies=proxy)

with open('test.html', 'w+', encoding='utf-8') as thing:
thing.write(r.text)

tree = html.fromstring(r.text)

try:
url = tree.xpath("//li[@id='listing-" + listingJSON['id'] + "']/div[@class='listing-item']/div")[0].get('data-listing_offers_url')
return url
except:
raise IndexError('List index out of range')
193 changes: 127 additions & 66 deletions BackpackTF/miscutils.py
Original file line number Diff line number Diff line change
@@ -1,91 +1,152 @@
class MiscUtils:
def __init__(self):
self.active = 1
import requests
import json

r = requests.get("https://backpack.tf/filters")

obj = json.loads(r.text)

particles = obj['particle']
qualities = obj['quality']
rarities = obj['rarity']
paints = obj['paint']
origins = obj['origin']
wear_tiers = obj['wear_tiers']
killstreakers = obj['killstreakers']
sheens = obj['sheens']
killstreak_tiers = obj['killstreak_tiers']
strange_parts = obj['strange_parts']

self.particleObj = {}
self.qualitiesObj = {}
self.raritiesObj = {}
self.paintsObj = {}
self.originsObj = {}
self.wear_tiersObj = {}
self.killstreakers = {}
self.sheensObj = {}
self.killstreak_tiers = {}
self.strange_partsObj = {}

for particle in particles:
self.particleObj[particle['name'].lower()] = int(particle['id'])

for quality in qualities:
self.qualitiesObj[quality['name'].lower()] = int(quality['id'])

for rarity in rarities:
self.raritiesObj[rarity['name'].lower()] = int(rarity['id'])

for paint in paints:
self.paintsObj[paint['name'].lower()] = int(paint['id'])

for particle in origins:
self.originsObj[particle['name'].lower()] = int(particle['id'])

for particle in wear_tiers:
self.wear_tiersObj[wear_tiers[particle]['name'].lower()] = int(wear_tiers[particle]['id'])

for particle in killstreakers:
self.killstreakers[particle['name'].lower()] = int(particle['id'])

for particle in sheens:
self.sheensObj[particle['name'].lower()] = int(particle['id'])

for particle in killstreak_tiers:
self.killstreak_tiers[particle['name'].lower()] = int(particle['id'])

for particle in strange_parts:
self.strange_partsObj[particle['name'].lower()] = int(particle['id'])


#
# Converts quality string to quality int
#
def quality_String_To_Int(self, string):
checkStr = string.lower()

if checkStr == "normal":
return 0
elif checkStr == "genuine":
return 1
elif checkStr == "vintage":
return 3
elif checkStr == "unusual":
return 5
elif checkStr == "unique":
return 6
elif checkStr == "community":
return 7
elif checkStr == "developer":
return 8
elif checkStr == "selfmade":
return 9
elif checkStr == "strange":
return 11
elif checkStr == "haunted":
return 13
elif checkStr == "collectors":
return 14
elif checkStr == "paintkitweapon":
return 15
try:
return self.qualitiesObj[string.lower()]
except:
return ""

#
# Converts wear_tier string to wear_tier int
# Converts particle string to particle int
#
def particle_String_To_Int(self, string):
try:
return self.particleObj[string.lower()]
except:
return ""

#
# Converts rarity string to rarity int
#
def rarity_String_To_Int(self, string):
try:
return self.raritiesObj[string.lower()]
except:
return ""

def wear_Tier_String_To_Int(self, string):
checkStr = string.lower()

#
# Origin quality string to origin int
#
def origin_String_To_Int(self, string):
try:
return self.originsObj[string.lower()]
except:
return ""

if checkStr == "factory new":
return 1
elif checkStr == "minimal wear":
return 2
elif checkStr == "field-tested":
return 3
elif checkStr == "well-worn":
return 4
elif checkStr == "battle scarred":
return 5

#
# Converts killstreaker tier string to killstreaker int
# Converts wear_tier string to wear_tier int
#
def wear_tier_String_To_Int(self, string):
try:
return self.wear_tiersObj[string.lower()]
except:
return ""

def killstreaker_Tier_String_To_Int(self, string):
checkStr = string.lower()

if checkStr == "standard":
return 1
elif checkStr == "specialized":
return 2
elif checkStr == "professional":
return 3
#
# Converts killstreaker string to killstreaker int
#
def killstreaker_String_To_Int(self, string):
try:
return self.killstreakers[string.lower()]
except:
return ""



#
# Converts sheen string to sheen int
#

def sheen_String_To_Int(self, string):
checkStr = string.lower()

if checkStr == "team shine":
return 1
elif checkStr == "deadly daffodil":
return 2
elif checkStr == "manndarin":
return 3
elif checkStr == "mean green":
return 4
elif checkStr == "agonizing emerald":
return 5
elif checkStr == "villainous violet":
return 6
elif checkStr == "hot rod":
return 7
try:
return self.sheensObj[string.lower()]
except:
return ""


#
# Converts killstreak_tier string to killstreak_tier int
#
def killstreak_tier_String_To_Int(self, string):
try:
return self.killstreak_tiers[string.lower()]
except:
return ""


#
# Converts strange_part string to strange_part int
#
def strange_parts_String_To_Int(self, string):
try:
return self.strange_partsObj[string.lower()]
except:
return ""


#
Expand Down
59 changes: 46 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ returns 0 or 1 for success or failure.

returns a dictionary. [Here](https://gist.github.com/davidteather/109acc0acd7e7d59f8192d8d8cfcba7c)'s an example json


#### extract_trade_url - extracts the trade url with token from a listing


| Attributes | Description |
| ------------- | ------------- |
| listingJSON | This is the json object of a classified listing on backpack.tf. You can get this using the method above. |
| proxy | This is an optional field, provide a dictionary that fits the python requests module requirements. See [here](https://stackoverflow.com/questions/8287628/proxies-with-python-requests-module) |


returns the trade url as a string.


### The Currency Class

__init__
Expand Down Expand Up @@ -162,51 +175,71 @@ __init__

| Attributes | Description |
| ------------- | ------------- |
| None | None |
| None | None |


#### quality_String_To_Int


| Attributes | Description |
| ------------- | ------------- |
| string | The string of the quality. Ex: "unique" |
| string | The string of the quality. Ex: "unique" |


#### particle_String_To_Int


| Attributes | Description |
| ------------- | ------------- |
| string | The string of the particle effect. |


#### rarity_String_To_Int


| Attributes | Description |
| ------------- | ------------- |
| string | The string of the rarity. |


#### origin_String_To_Int


| Attributes | Description |
| ------------- | ------------- |
| string | The string of the origin. |

returns 0-15

#### wear_Tier_String_To_Int
#### wear_tier_String_To_Int


| Attributes | Description |
| ------------- | ------------- |
| string | The string of the wear_tier. Ex: "factory new" |
| string | The string of the wear_tier. |

returns 1-5

##### killstreaker_Tier_String_To_Int
#### killstreaker_String_To_Int


| Attributes | Description |
| ------------- | ------------- |
| string | The string of the killstreaker tier. Ex: "professional" |
| string | The string of the killstreaker. |

returns 1-3

##### sheen_String_To_Int
#### strange_parts_String_To_Int


| Attributes | Description |
| ------------- | ------------- |
| string | The string of the sheen. Ex: "mean green" |
| string | The string of the strange part. |

return 1-7

#### steam_id_to_account_id


| Attributes | Description |
| ------------- | ------------- |
| string | The string of the steam_id. |
| string | The string of the steam_id. |

returns an int of the account id.

Expand Down
9 changes: 9 additions & 0 deletions examples/extractTradeUrl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from BackpackTF.account import Account

user = Account(client_id="xxxxxx", client_secret="xxxxxx", api_key="xxxxxx")

classifieds = user.search_Classifieds()['buy']['listings']

for classified in classifieds:
trade_url = user.extract_trade_url(classified)
print(trade_url)
Loading