I'm attempting to format json from the Roblox API. I have a names.txt that stores all of the names. This is how the file looks
rip_robson0007
Abobausrip
app_58230
kakoytochelik123
Ameliathebest727
Sherri0708
HixPlayk
mekayla_091
ddddorffg
ghfgrgt7nfdbfj
TheWolfylikedog
paquita12345jeje
hfsgfhsgfhgfhds
It stores a bunch of usernames seperated by a new line. The code is suppose to use the names and for each name get the JSON from this endpoint https://api.roblox.com/users/get-by-username?username={name} & format it as I have in my code. It always returns error 429 and doesn't save any of the data.
This is the code:
import json
import requests
import time
# Read the names from the text file
with open("./txt/names.txt", "r") as f:
names = f.read().split("\n")
# Initialize an empty dictionary to store the users
users = {}
# Iterate through the names
for name in names:
time.sleep(5)
response = requests.get(f"https://api.roblox.com/users/get-by-username?username={name}")
# Check the status code of the response
if response.status_code != 200:
print(f"Failed to get data for {name}: {response.status_code}")
continue
# Try to parse the response as JSON
try:
user_data = response.json()
except ValueError:
print(f"Failed to parse JSON for {name}")
continue
# Extract the necessary information from the response
user_id = user_data["Id"]
username = user_data["Username"]
avatar_uri = user_data["AvatarUri"]
avatar_final = user_data["AvatarFinal"]
is_online = user_data["IsOnline"]
# Add the user's information to the dictionary
users[user_id] = {
"Id": user_id,
"Username": username,
"AvatarUri": avatar_uri,
"AvatarFinal": avatar_final,
"IsOnline": is_online
}
# Save the dictionary to a JSON file
with open("users.json", "w") as f:
json.dump(users, f)
You can often overcome this with judicious use of proxies.
Start by getting a list of proxies from which you will make random selections. I have a scraper that acquires proxies from https://free-proxy-list.net
My list (extract) looks like this:-
http://103.197.71.7:80 - no
http://163.116.177.33:808 - yes
'yes' means that HTTPS is supported. This list currently contains 95 proxies. It varies depending on the response from my scraper
So we start by parsing the proxy list. Subsequently we choose proxies at random before trying to access the Roblox API. This may not run quickly because the proxies are not necessarily reliable. They are free after all.
from requests import get as GET, packages as PACKAGES
from random import choice as CHOICE
from concurrent.futures import ThreadPoolExecutor as TPE
PACKAGES.urllib3.util.ssl_.DEFAULT_CIPHERS = 'ALL:#SECLEVEL=1'
ROBLOX_API = 'https://api.roblox.com/users/get-by-username'
TIMEOUT = 1
def get_proxies():
http, https = list(), list()
with open('proxylist.txt') as p:
for line in p:
proxy_url, _, supports_https = line.split()
_list = https if supports_https == 'yes' else http
_list.append(proxy_url)
return http, https
http, https = get_proxies()
def process(name):
params = {'username': name.strip()}
while True:
try:
proxy = {'http': CHOICE(http), 'https': CHOICE(https)}
(r := GET(ROBLOX_API, params=params, proxies=proxy, timeout=TIMEOUT)).raise_for_status()
if (j := r.json()).get('success', True):
print(j)
break
except Exception as e:
pass
with open('names.txt') as names:
with TPE() as executor:
executor.map(process, names)
In principle, the while loop in process() could get stuck so it might make sense to limit the number of retries.
This produces the following output:
{'Id': 4082578648, 'Username': 'paquita12345jeje', 'AvatarUri': None, 'AvatarFinal': False, 'IsOnline': False}
{'Id': 2965702542, 'Username': 'mekayla_091', 'AvatarUri': None, 'AvatarFinal': False, 'IsOnline': False}
{'Id': 4079018794, 'Username': 'app_58230', 'AvatarUri': None, 'AvatarFinal': False, 'IsOnline': False}
{'Id': 3437922948, 'Username': 'kakoytochelik123', 'AvatarUri': None, 'AvatarFinal': False, 'IsOnline': False}
{'Id': 4082346906, 'Username': 'Abobausrip', 'AvatarUri': None, 'AvatarFinal': False, 'IsOnline': False}
{'Id': 2988555289, 'Username': 'HixPlayk', 'AvatarUri': None, 'AvatarFinal': False, 'IsOnline': False}
{'Id': 3286921649, 'Username': 'Sherri0708', 'AvatarUri': None, 'AvatarFinal': False, 'IsOnline': False}
{'Id': 1441252794, 'Username': 'ghfgrgt7nfdbfj', 'AvatarUri': None, 'AvatarFinal': False, 'IsOnline': False}
{'Id': 4088896225, 'Username': 'ddddorffg', 'AvatarUri': None, 'AvatarFinal': False, 'IsOnline': False}
{'Id': 3443374919, 'Username': 'TheWolfylikedog', 'AvatarUri': None, 'AvatarFinal': False, 'IsOnline': False}
{'Id': 3980932331, 'Username': 'Ameliathebest727', 'AvatarUri': None, 'AvatarFinal': False, 'IsOnline': False}
{'Id': 3773237135, 'Username': 'rip_robson0007', 'AvatarUri': None, 'AvatarFinal': False, 'IsOnline': False}
{'Id': 4082991447, 'Username': 'hfsgfhsgfhgfhds', 'AvatarUri': None, 'AvatarFinal': False, 'IsOnline': False}
Related
So I'm trying to get a track from the spotify API by searching for it by using the search endpoint of the API (See documentation). First, I authorize myself so I can send GET requests. This happens without issues, I added the code for reproducibility.
import requests
CLIENT_ID = "your_id_here"
CLIENT_SECRET = "your_secret_here"
AUTH_URL = "https://accounts.spotify.com/api/token"
auth_response = requests.post(AUTH_URL, {
'grant_type': 'client_credentials',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
})
#Convert response to JSON
auth_response_data = auth_response.json()
#Save the access token
access_token = auth_response_data['access_token']
#Need to pass access token into header to send properly formed GET request to API server
headers = {
'Authorization': 'Bearer {token}'.format(token=access_token)
}
Then, I want to use the search endpoint of the API to find a track by using the track name + artist (I need the track ID later on). When I use the example provided in the documentation, everything works fine and an artist object is returned by using the following query:
BASE_URL = 'https://api.spotify.com/v1/'
r = requests.get(BASE_URL + 'search?q=tania%20bowra&type=artist', headers=headers)
r = r.json()
This is the response, which looks exactly like the one in documentation:
{'artists': {'href': 'https://api.spotify.com/v1/search?query=tania+bowra&type=artist&offset=0&limit=20',
'items': [{'external_urls': {'spotify': 'https://open.spotify.com/artist/08td7MxkoHQkXnWAYD8d6Q'},
'followers': {'href': None, 'total': 235},
'genres': [],
'href': 'https://api.spotify.com/v1/artists/08td7MxkoHQkXnWAYD8d6Q',
'id': '08td7MxkoHQkXnWAYD8d6Q',
'images': [{'height': 640,
'url': 'https://i.scdn.co/image/ab67616d0000b2731ae2bdc1378da1b440e1f610',
'width': 640},
{'height': 300,
'url': 'https://i.scdn.co/image/ab67616d00001e021ae2bdc1378da1b440e1f610',
'width': 300},
{'height': 64,
'url': 'https://i.scdn.co/image/ab67616d000048511ae2bdc1378da1b440e1f610',
'width': 64}],
'name': 'Tania Bowra',
'popularity': 1,
'type': 'artist',
'uri': 'spotify:artist:08td7MxkoHQkXnWAYD8d6Q'}],
'limit': 20,
'next': None,
'offset': 0,
'previous': None,
'total': 1}}
Applying the same logic, I tried to get a track object from the api by using an artist and a track name, like so:
BASE_URL = 'https://api.spotify.com/v1/'
r = requests.get(BASE_URL + 'search?q=artist:queen%20track:bohemian%20rapsody&type=track', headers=headers)
r = r.json()
Even though I do get a valid response (statuscode==200), it seems to be empty:
{'tracks': {'href': 'https://api.spotify.com/v1/search?query=artist%3Aqueen+track%3Abohemian+rapsody&type=track&offset=0&limit=20',
'items': [],
'limit': 20,
'next': None,
'offset': 0,
'previous': None,
'total': 0}}
My question is: why is this happening?
You are now searching for the query: artist:queen%20track:bohemian%20rapsody while this should just be queen%20bohemian%20rapsody instead. the type afterwards shows what items you want to return. You dont have to determine the artist and track name seperately in the query. Interpret the query just like typing something into the spotify search bar.
Problem solved. It was rhapsody instead of rapsody... Sucks be a non-native english speaker sometimes =)
From this example, how do I get the IP address from the response?
import requests
URL = "https://security.cloudflare-dns.com/dns-query?name=test.com"
session = requests.session()
r = session.get(URL, headers={"Accept": "application/dns-json"})
r = r.json()
print("Type:", type(r))
print("Len:", len(r))
print("Content:", r)
IP = r['Answer'][-1]
print("IP:", IP)
Output:
Type: <class 'dict'>
Len: 8
Content: {'Status': 0, 'TC': False, 'RD': True, 'RA': True, 'AD': False, 'CD': False, 'Question': [{'name': 'test.com', 'type': 1}], 'Answer': [{'name': 'test.com', 'type': 1, 'TTL': 3261, 'data': '69.172.200.235'}]}
IP: {'name': 'test.com', 'type': 1, 'TTL': 3261, 'data': '69.172.200.235'}
Like this?:
IP = r['Answer'][-1]['data']
print("IP:", IP)
Output:
IP: 69.172.200.235
The reply is:
r['Answer'][0]['data']
But I would like to give you a tip on how you could find this.
I like to use the code module to run an interactive console inside my script:
import requests
import code
URL = "https://security.cloudflare-dns.com/dns-query?name=test.com"
session = requests.session()
r = session.get(URL, headers={"Accept": "application/dns-json"})
r = r.json()
print("Type:", type(r))
print("Len:", len(r))
print("Content:", r)
code.interact(banner="after get request", locals=locals()) #interactive console is created here
IP = r['Answer'][-1]
print("IP:", IP)
From there, you can play with local variables and test until you find what you need.
I hope that will helps.
This question already has answers here:
Why am I seeing "TypeError: string indices must be integers"?
(9 answers)
Closed 2 years ago.
I accessed Roblox API to get the list of friends. Everything seems to work fine until I tried printing the username..
Console Error:
Please enter Username/UserID of the player: 1517232281
Status: 200
Response:
[{"Id":189675950,"Username":"alona_0627","AvatarUri":"https://tr.rbxcdn.com/01f752ec70c25b9c6144cca6f81b410e/30/30/Avatar/Jpeg","AvatarFinal":true,"IsOnline":true},{"Id":970243365,"Username":"fgarfgasFdffs","AvatarUri":"https://tr.rbxcdn.com/58fc9b6c4f1059f2d3634df36f37f12d/30/30/Avatar/Jpeg","AvatarFinal":true,"IsOnline":false},{"Id":1499755935,"Username":"muhmuhmh","AvatarUri":"https://tr.rbxcdn.com/8ed4d50a0c080c2ddce960d3c65dc506/30/30/Avatar/Jpeg","AvatarFinal":true,"IsOnline":false},{"Id":158543991,"Username":"trikydragon_master2","AvatarUri":"https://tr.rbxcdn.com/565ad229fd89f62ca679d43ada4dd8ba/30/30/Avatar/Jpeg","AvatarFinal":true,"IsOnline":false}]
[{'Id': 189675950, 'Username': 'alona_0627', 'AvatarUri': 'https://tr.rbxcdn.com/01f752ec70c25b9c6144cca6f81b410e/30/30/Avatar/Jpeg', 'AvatarFinal': True, 'IsOnline': True}, {'Id': 970243365, 'Username': 'fgarfgasFdffs', 'AvatarUri': 'https://tr.rbxcdn.com/58fc9b6c4f1059f2d3634df36f37f12d/30/30/Avatar/Jpeg', 'AvatarFinal': True, 'IsOnline': False}, {'Id': 1499755935, 'Username': 'muhmuhmh', 'AvatarUri': 'https://tr.rbxcdn.com/8ed4d50a0c080c2ddce960d3c65dc506/30/30/Avatar/Jpeg', 'AvatarFinal': True, 'IsOnline': False}, {'Id': 158543991, 'Username': 'trikydragon_master2', 'AvatarUri': 'https://tr.rbxcdn.com/565ad229fd89f62ca679d43ada4dd8ba/30/30/Avatar/Jpeg', 'AvatarFinal': True, 'IsOnline': False}]
Traceback (most recent call last):
File "main.py", line 17, in <module>
options()
File "main.py", line 13, in options
rbxl()
File "E:\ESpolit\options\roblox.py", line 42, in rbxl
gatherlist()
File "E:\ESpolit\options\roblox.py", line 22, in gatherlist
print(jsonl["Username"][0])
TypeError: string indices must be integers
Code:
def gatherlist():
clear()
art("Friend Lister", "true")
print("\n\n\n")
userni = input("Please enter Username/UserID of the player: ")
if strnum(userni) == "id":
r = requests.get('https://api.roblox.com/users/'+userni+'/friends')
print("Status: "+str(r.status_code))
print("Response: "+ r.text)
rese21 = r.json()
for user in rese21:
jsonl = json.dumps(rese21)
print(rese21, "\n")
print(jsonl["Username"])
You don't need change user at all, since r.json() already returns a dictionary:
def gatherlist():
clear()
art("Friend Lister", "true")
print("\n\n\n")
userni = input("Please enter Username/UserID of the player: ")
if strnum(userni) == "id":
r = requests.get('https://api.roblox.com/users/'+userni+'/friends')
print("Status: "+str(r.status_code))
print("Response: "+ r.text)
rese21 = r.json()
for user in rese21:
print(user, "\n")
print(user["Username"])
I have the following the following code
response = requests.get(url, headers=headers)
response_dict = response.json()
print(response_dict)
user_name = response_dict['data']['username']
password = response_dict['data']['password']
My print returns:
{'request_id': 'hidden', 'lease_id': 'hidden', 'renewable': True, 'lease_duration': 3600, 'data': {'password': 'hidden', 'username': 'hidden'}, 'wrap_info': None, 'warnings': None, 'auth': None}
I get a key error
'data': KeyError
What seems to be the mistake here?
The issue was with my VPC once the code was pushed to a lambda, I did not have permission to move forward with requests sometimes due to race condition.
I am trying to make a call to the SugarCRM v10 api to get the output of a report without having to log into the web interface and click the export button. I would like to get this report as data that can be written into csv format using python and the requests library.
I can authenticate successfully and get a token but whatever I try all I get as a response from reports is Error Method does not exist, by which they mean that you cannot use /csv at the end of the second url in this code block.
url = "https://mydomain.sugarondemand.com/rest/v10/oauth2/token"
payload = {"grant_type":"password","username":"ursername","password":"password","client_id":"sugar", "platform":"myspecialapp"}
r = requests.post(url, data=json.dumps(payload))
response = json.loads(r.text)
token = response[u'access_token']
print 'Success! OAuth token is ' + token
#What export methods are available? ###################################
#WRONG url = "https://mydomain.sugarondemand.com/rest/v10/Reports/report_id/csv"
#Following paquino's suggestion I used Base64
url = "https://mydomain.sugarondemand.com/rest/v10/Reports/report_id/Base64"
headers = { "Content-Type" : "application/json", "OAuth-Token": token }
r = requests.get(url, headers=headers);
response = r.text.decode('base64')
print response`
My question is this: what Export Methods are available via an api call to v10 of the SugarCRM api.
Edit: Using Base64 in the request url unfortunately returns ab object that I don't know how to parse...
%PDF-1.7
3 0 obj
<</Type /Page
/Parent 1 0 R
/MediaBox [0 0 792.00 612.00]
/Resources 2 0 R
/Contents 4 0 R>>
endobj
4 0 obj
<</Length 37217>>
stream
8.cܬR≈`ä║dàQöWºáW╙µ
The Reports Api accepts "Base64" and "Pdf"
Python Wrapper for SugarCRM REST API v10
https://github.com/Feverup/pysugarcrm
Quickstart
pip install pysugarcrm
from pysugarcrm import SugarCRM
api = SugarCRM('https://yourdomain.sugaropencloud.e', 'youruser', 'yourpassword')
# Return info about current user
api.me
# A more complex query requesting employees
api.get('/Employees', query_params={'max_num': 2, 'offset': 2, 'fields': 'user_name,email'})
{'next_offset': 4,
'records': [{'_acl': {'fields': {}},
'_module': 'Employees',
'date_modified': '2015-09-09T13:40:32+02:00',
'email': [{'email_address': 'John.doe#domain.com',
'invalid_email': False,
'opt_out': False,
'primary_address': True,
'reply_to_address': False}],
'id': '12364218-7d79-80e0-4f6d-35ed99a8419d',
'user_name': 'john.doe'},
{'_acl': {'fields': {}},
'_module': 'Employees',
'date_modified': '2015-09-09T13:39:54+02:00',
'email': [{'email_address': 'alice#domain.com',
'invalid_email': False,
'opt_out': False,
'primary_address': True,
'reply_to_address': False}],
'id': 'a0e117c0-9e46-aebf-f71a-55ed9a2b4731',
'user_name': 'alice'}]}
# Generate a Lead
api.post('/Leads', json={'first_name': 'John', 'last_name': 'Smith', 'business_name_c': 'Test John', 'contact_email_c': 'john#smith.com'})
from pysugarcrm import sugar_api
with sugar_api('http://testserver.com/', "admin", "12345") as api:
data = api.get('/Employees', query_params={'max_num': 2, 'offset': 2, 'fields': 'user_name,email'})
api.post('/Leads', json={'first_name': 'John', 'last_name': 'Smith', 'business_name_c': 'Test John', 'contact_email_c': 'john#smith.com'})
# Once we exit the context manager the sugar connection is closed and the user is logged out