Skip to content

Commit

Permalink
v1.4.1
Browse files Browse the repository at this point in the history
  - Added ranktier.Player
  - Updated long_description in setup.py
  • Loading branch information
marcusmunch committed Sep 19, 2019
1 parent 7f250ab commit 546257d
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 31 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
language: python
python:
- "2.7"
- "3.3"
- "3.4"
- "3.5"
- "3.6"
- "3.7"

script:
- pip install .
- "python test/test.py"
- "python test/test.py $steam_id"
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,23 @@ Usage
```python
>>> import ranktier
>>> r = ranktier.Rank(42)
>>> print r
>>> print(r)
Archon [2]
>>> p = ranktier.Player(86745912)
>>> print(p.rank)
Immortal rank 3
```
`Rank.name` can also be used, but returns the same as above.

`r.name` can also be used, but returns the same as above.
`rank` has to be a two-digit number for ranktier to work. Ranktier works regardless of `rank` being a `str`-type or
`int`-type variable.

`rank` has to be a two-digit number for ranktier to work. Ranktier works regardless of `rank` being a `str`-type or `int`-type variable.
As of 1.4, Player objects also have names:
```python
>>> p.personaname
天鸽
>>> p.name
Arteezy
```

`personaname` refers to a profile's most recent alias, while `name` is the tag used by pros in-game.
81 changes: 62 additions & 19 deletions ranktier/__init__.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,83 @@
#!/usr/bin/env python
import json
import socket
import sys
import urllib.request

VERSION = "1.4.1"
USER_AGENT = "ranktier/{}".format(VERSION)


class Rank(object):

def __init__(self, rank):
def __init__(self, rank, leaderboard=False):
self.leaderboard = leaderboard

if rank == 'null':
print("This profile has not yet calibrated!")
sys.stderr.write("This profile has not yet calibrated!\n")
self.rank = None
return

if type(rank) is int or type(rank) is float:
rank = str(rank)

if not all([rank.isdigit(), len(rank) == 2]):
print("Something went wrong!\nRank input: %s" % rank)
if not all([rank.isdigit(), len(rank) == 2]) and not leaderboard:
sys.stderr.write("Something went wrong!\nRank input: %s\n" % rank)
self.rank = None
return

self.rank = rank


def __str__(cls):
return str(cls.name)

def __str__(self):
return str(self.name)

@property
def name(cls):
if cls.rank is None:
def name(self):
if self.rank is None:
return None

if self.leaderboard:
return "Immortal rank {}".format(self.rank)

if self.rank[0] == "8":
return "Immortal"

ranks = {"1": "Herald",
"2": "Guardian",
"3": "Crusader",
"4": "Archon",
"5": "Legend",
"6": "Ancient",
"7": "Divine"}

return "{} [{}]".format(ranks[self.rank[0]], self.rank[1])


class Player:

def __init__(self, friend_id):

self.friend_id = friend_id
try:
profile = self.get_profile_data()

if profile["profile"]["name"]: # Name of pros
self.name = profile["profile"]["name"]
self.personaname = profile["profile"]["personaname"] # Steam handle
if profile["leaderboard_rank"]:
self.rank = Rank(profile["leaderboard_rank"], leaderboard=True)
else:
if not profile["rank_tier"]:
self.rank = Rank("null")
else:
self.rank = Rank(profile["rank_tier"])

except socket.timeout: # API connection timed out
sys.stderr.write("Connection to OpenDota API timed out!\n")

ranks = { "1" : "Herald"
, "2" : "Guardian"
, "3" : "Crusader"
, "4" : "Archon"
, "5" : "Legend"
, "6" : "Ancient"
, "7" : "Divine" }
def get_profile_data(self):
req = urllib.request.Request(url="https://api.opendota.com/api/players/{}".format(self.friend_id),
headers={"User-Agent": USER_AGENT})

return "{} [{}]".format(ranks[cls.rank[0]], cls.rank[1])
with urllib.request.urlopen(req, timeout=20) as resp:
data = json.loads(resp.read().decode("utf-8"))
return data
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description-file = README.md
11 changes: 6 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import ranktier
from setuptools import setup

with open("README.md", "r") as f:
readme = f.read()

setup(name="ranktier",
version="1.2",
version=ranktier.VERSION,
description="A Dota rank tier converter",
long_description="Ranktier turns two-digit rank tier numbers into human-readable output.",
long_description=readme,
url="https://github.com/marcusmunch/ranktier",
license="GPLv3",
classifiers=[
Expand All @@ -16,13 +19,11 @@
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Natural Language :: English",
"Operating System :: OS Independent",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7"
],
author="Marcus Grünewald",
author_email="[email protected]",
Expand Down
6 changes: 5 additions & 1 deletion test/test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#!/usr/bin/env python
import sys

import ranktier

for n in [42, '42', 'null', 404]:
r = ranktier.Rank(n)

print(r)
print(r)

for steamid in sys.argv[1:]:
p = ranktier.Player(steamid)

0 comments on commit 546257d

Please sign in to comment.