Skip to content
TORNLIFE More

Sharing completed API apps/scripts.

Started by Cryptogeist [2221179] on in API Development.

12 replies · 370 views · thread synced · 6 days ago · View on torn.com
About this thread

Posts archived: 13 / 13 posts (100%) · the total is Torn's reply count + the opening post at the last fetch

Counted by TornLife from the archived posts.

Archived posts
13
Discussion span
→
People posting
9
Likes on archived posts
6
Authority score
53 / 100
Historical score
24 / 100
Story score
34 / 100
Engagement score
59 / 100

Most-liked replies

Cryptogeist [2221179]

Someone has probably  already asked this question, but I'm really looking forward to playing with the API, and hopefully creating something fun or useful. Or both. If I do, how do I make the code available to other API coders so it can be used, improved etc....basically, how do I make API apps open source?
Kivou [2000607]

Several ways I guess. And it depends also on the type of code.

I 'm using  github which is a nice website interface for a git server (versioning tool).

I know that the user script guys are using something else (go check their theads). Edit: https://greasyfork.org
Meme_Raven [1891116]

You probably wont find it people like to profit from their scripts, you could always "decompile" scripts - most don't even set a license so knock yesen out. like CRX and XPI just install 7-zip and extract (just a zip wrapper) and you'll get the Cancerscript source.
Stiny [992186]

If you find software that doesn’t have a license, that generally means you have no permission from the creators of the software to use, modify, or share the software. Although a code host such as GitHub may allow you to view and fork the code, this does not imply that you are permitted to use, modify, or share the software for any purpose.

The addons that end up in the browser stores are licensed for the stores such as chrome or firefox to distribute them, but you as a user are not allowed to decompile, modify, and then distribute said code.
tos [1976582]

All of that said, if it's a script for Torn and the author didn't bother to obfuscate the code then go hog wild. Many of the scripts you find are from people who don't know what a liscense is or like myself just don't care enough to set one.
cockroach [1338804]

Mine is in github (private repo - talk to me if you want to see it).  But with the current breakdown of the API for non-browser clients there's no use in having one.
Meme_Raven [1891116]

I dont know why you got downvotes, thats the point of scripts. Javahipsters misunderstand it.

Go wild "decompiling" them.
Meme_Raven [1891116]

If yours is something you can use on the desktop without "NODE.(NOT)JS and ELECTRON(deffo not Chrome Browser)" go for it - we need more stuff **not** made in Cancer script. it's made to be kept in Browser containers for a reason. Keep low-level to  the desktop not high-level Javahipster stuff.
cockroach [1338804]

This copy/paste won't work after losing the Pythonic indentation but here's a taste.  Contact me with your github name if you want to see the full works.   The API harvesting stuffs data into sqlite3 and the interactive web display uses Flask and Postgresql all on Linux.

tornapi@tornutopia:/home/tornapi/bin> cat web_api.py
import requests
import time
import signal
import random


class Tornapi:

def __init__(self, c):
self.c = c # db cursor
self.count = [0, 0, 0]
self.good_user_key = {}
self.good_faction_key = {}
self.suggest_faction_key = {}
self.pid2ak = {}
self.default_apikey = None
self.api_is_enabled = True # assume this and change it if necessary
#
c.execute("""select default_apikey from admin""")
for row in c:
self.default_apikey = row[0]
#
c.execute("""select player_id,short_err,long_err,key from apikeys""")
for row in c:
if row[2]: # long-lasting error on this key
continue
if (3600 + row[1]) > int(time.time()):
continue
self.pid2ak[row[0]] = row[3]
#
c.execute("""select ignore,faction_id,player_id from factionwatch""")
for row in c:
ignore, faction_id, player_id = row
if ignore:
continue
if faction_id not in self.suggest_faction_key:
self.suggest_faction_key[faction_id] = []
if player_id in self.pid2ak:
self.suggest_faction_key[faction_id].append(player_id)

def apistats(self):
print("API stats: good, fail, error =", self.count)
self.pid2ak = []

# user/faction selection property
def torn(self, what, which, how):
key_id = None
ak = None
if not self.api_is_enabled:
return ["API ERROR what=" + str(what) + " which=" + str(which) + " how=" + str(how), 'api disabled already']
if ('player' == what) or ('user' == what):
what = 'user'
choose_from = self.good_user_key.keys()
if len(choose_from):
key_id = random.choice(list(self.good_user_key.keys()))
elif 'faction' == what:
if which in self.good_faction_key:
self.suggest_faction_key[which].insert(0,self.good_faction_key[which])
print("Suggestions: ", self.suggest_faction_key)
if which in self.suggest_faction_key:
print("look for key in ", self.suggest_faction_key[which])
try:
key_id = self.suggest_faction_key[which].pop()
except:
return ["No key to query faction"]
else:
return ["EPARM what"]

if not key_id:
print("Taking key of last resort ...")
key_id = self.default_apikey

if key_id in self.pid2ak:
ak = self.pid2ak[key_id]

if 'basic' == how:
# do on player or faction
pass
elif ('attacknews' == how) or ('attacknewsfull' == how) or ('armorynews' == how) or ('armorynewsfull' == how) or ('medical' == how):
if 'faction' != what:
return ["EPARM what/how"]
elif 'crimes' == how:
# do on player or faction
pass
elif (('profile' == how) or ('events' == how)):
if 'user' != what:
return ["EPARM what/how"]
elif ('bars' == how) or ('personalstats' == how):
if 'user' != what:
return ["EPARM what/how"]
if which in self.pid2ak:
ak = self.pid2ak[which]
key_id = which
else:
return ["EPARM need right apikey for bars or personalstats"]
else:
return ["EPARM how"]

if not ak:
return ["EPARM no key available"]
print("about to query ", what, repr(which), " for ", how)

apiurl = "https://api.torn.com/" + what + "/" + str(which) + "?selections=" + how + "&key=" + ak
time.sleep(1)
try:
signal.alarm(20)
r = requests.get(apiurl, timeout=10)
signal.alarm(0)
if not r:
self.count[1] += 1
return ["FAIL API REQUEST r is None what=" + str(what) + " which=" + str(which) + " how=" + str(how)]
except:
self.count[1] += 1
return ["FAIL API REQUEST what=" + str(what) + " which=" + str(which) + " how=" + str(how)]
try:
data = r.json()
except:
self.count[1] += 1
return ["FAIL API JSON"]

if what == 'faction' and how == 'crimes':
print("faction crime data recieved as", r)

if 'error' in data:
# handle Torn API error
e = data['error']
code = e['code']
error_msg = e['error']
et = int(time.time())
self.c.execute("""insert into error values (?,?,?,?, ?,?,?)""",
(et, key_id, what, which, how, code, error_msg,))
# short key ban for 5 (7 if faction)
# long key ban 1 2 10
if (5 == code) or ((7 == code) and ('faction' == what)):
self.c.execute("""update apikeys set short_err=? where player_id=?""", (et, key_id,))
elif (1 == code) or (2 == code) or (10 == code):
self.c.execute("""update apikeys set long_err=? where player_id=?""", (et, key_id,))

if key_id in self.good_user_key:
del self.good_user_key[key_id]
self.count[2] += 1
if 9 == code:
self.api_is_enabled = False # need to stop making requests
return ["API ERROR what=" + str(what) + " which=" + str(which) + " how=" + str(how), code]

if 'user' == what:
if 'basic' == how:
if 'level' not in data:
print("level not found", data)
return ['Bad data']
if 'profile' == how:
if 'level' not in data:
print("level not found", data)
return ['Bad data']
if 'bars' == how:
if 'nerve' not in data:
print("nerve not found", data)
return ['Bad data']
if 'personalstats' == how:
if 'personalstats' not in data:
print("personalstats not found", data)
return ['Bad data']
if 'crimes' == how:
if 'criminalrecord' not in data:
print("criminalrecord not found", data)
return ['Bad data']
elif 'faction' == what:
if 'basic' == how:
if 'members' not in data:
print("Faction members not found", data)
return ['Bad data']
if 'crimes' == how:
if 'crimes' not in data:
print("Faction crimes not found", data)
return ['Bad data']
if ('attacknews' == how) or ('attacknewsfull' == how):
if 'attacknews' not in data:
print("Faction attacknews not found", data)
return ['Bad data']
if ('armorynews' == how) or ('armorynewsfull' == how):
if 'armorynews' not in data:
print("Faction armorynews not found", data)
return ['Bad data']

self.good_user_key[key_id] = 1
if 'faction' == what:
self.good_faction_key[which] = key_id
self.count[0] += 1
return ["OK", data, key_id]