Skip to content
TORNLIFE More

Writing api calls in python

Started by Phillip_J_Fry [2184575] on in API Development.

5 replies · 152 views · thread synced · 5 days ago · View on torn.com
About this thread

Posts archived: 6 / 6 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
6
Discussion span
→
People posting
6
Likes on archived posts
3
Authority score
51 / 100
Historical score
20 / 100
Story score
28 / 100
Engagement score
49 / 100
Phillip_J_Fry [2184575]
So I recently started learning python rewrite and building a bot

I've been able to make a bunch of commands that are embedded

But I can not figure out how make an api call.....

Could some one give me an example

Please and thank you
tonyuh [2093883]
I've never used python before but I know that cURL works, so there should be a way to call that from python. If not then you can try urllib2 here, but as I said I have never used it before.

Just fetch the api.torn.com url with the correct syntax.
Mephiles [2087524]
Also not familiar with Python HTTP requests but check this out

https://stackoverflow.com/questions/645312/what-is-the-quickest-way-to-http-get-in-python
shro [1332742]
 Basically do this from a command prompt after installing python and pip

 pip install requests
 python
 >>import requests
 >>response = requests.get('https://api.torn.com/...')
 >>data = response.json()
 >>print(data)
Afwas [1337627]
import urllib.request
import json
from pprint import pprint

this_url = "http://api.torn.com/company/?selections=stock&key=APIKEYHERE"
user_agent = 'Relentlessbot/0.1 (+https://lt.relentless.pw/bot.html)'
headers = {'User-Agent': user_agent, }
request = urllib.request.Request(this_url, None, headers)
with urllib.request.urlopen(request) as url:
    data = json.loads(url.read().decode())
    pprint(data)
KayDeeArgh [1990157]
The discord.py rewrite has a module you can use for async api calls called aiohttp. Hit me up if you need an example.

source: I build all my discord bots in python.