API Example: Python

From SoylentNews
Jump to navigation Jump to search
import requests

# URL and parameters for login
login_url = 'https://dev.soylentnews.org/api.pl?m=auth&op=login'
login_payload = {
    'nick': 'replic8tor',
    'pass': 'apassword'
}

# Post the JSON payload to the login URL
response = requests.post(login_url, json=login_payload)
print(response)
# Check if the login request was successful
if response.status_code == 200:
    # Extract the cookie from the response
    cookie = response.cookies.get_dict()
    print(f'Successfully logged in. Cookie: {cookie}')
    
    # URL and parameters for the subsequent API call
    subsequent_url = 'https://dev.soylentnews.org/api.pl'
    subsequent_params = {
        'redacts': '(?<=test of ).*?(?= This is only a)',
        'm': 'admin',
        'op': 'flag_spam',
        'cid': '31675',
        'spam_flag': '1',
        'mod_reason': 'test for jan'
    }
    
    # Make the subsequent API call with the cookie
    subsequent_response = requests.get(subsequent_url, params=subsequent_params, cookies=cookie)
    
    # Check if the subsequent request was successful
    if subsequent_response.status_code == 200:
        print('Successfully made subsequent API call.')
        print(subsequent_response.json())
    else:
        print(f'Failed to make subsequent API call. Status code: {subsequent_response.status_code}')
else:
    print(f'Failed to log in. Status code: {response.status_code}')