Skip to content

Instantly share code, notes, and snippets.

@HockeyInJune
Last active January 4, 2021 19:29
Show Gist options
  • Save HockeyInJune/88ddeb2389c09ae57c3b5157f36ea5a7 to your computer and use it in GitHub Desktop.
Save HockeyInJune/88ddeb2389c09ae57c3b5157f36ea5a7 to your computer and use it in GitHub Desktop.
wpdc demo
import sys
import json
import hmac
import urllib
import hashlib
import requests
username = '{YOUR-USERNAME}'
key = '{YOUR-API-KEY}'.encode()
baseurl = 'https://wpdc.org'
def main():
if ( len(sys.argv) <= 1 ):
print('python3 ' + sys.argv[0] + ' properties {list|add} [url]')
print('python3 ' + sys.argv[0] + ' {alerts|results} url [date] [verbose]')
exit()
if ( sys.argv[1] == 'properties' ):
url = baseurl + '/properties'
if ( sys.argv[2] == 'list' ):
params = { 'action': 'list' }
elif ( sys.argv[2] == 'add' ):
params = { 'action': 'add', 'url': sys.argv[3] }
else:
print('python3 ' + sys.argv[0] + ' properties username {list|add} [url]')
exit()
elif ( sys.argv[1] == 'results' ):
url = baseurl + '/results'
if ( len(sys.argv) == 3 ):
params = { 'url': sys.argv[2] }
elif ( len(sys.argv) == 4 ):
params = { 'url': sys.argv[2], 'date': sys.argv[3] }
elif ( len(sys.argv) == 5 ):
params = { 'url': sys.argv[2], 'date': sys.argv[3], 'verbose': sys.argv[4] }
else:
print('python3 ' + sys.argv[0] + ' results username url [date] [verbose]')
exit()
elif ( sys.argv[1] == 'alerts' ):
url = baseurl + '/alerts'
if ( len(sys.argv) == 3 ):
params = { 'url': sys.argv[2] }
elif ( len(sys.argv) == 4 ):
params = { 'url': sys.argv[2], 'date': sys.argv[3] }
elif ( len(sys.argv) == 5 ):
params = { 'url': sys.argv[2], 'date': sys.argv[3], 'verbose': sys.argv[4] }
else:
print('python3 ' + sys.argv[0] + ' alerts username url [date] [verbose]')
exit()
else:
print('python3 ' + sys.argv[0] + ' properties username {list|add} [url]')
print('python3 ' + sys.argv[0] + ' {alerts|results} username url [date] [verbose]')
exit()
paramstext = urllib.parse.unquote_plus(urllib.parse.urlencode(params)).encode()
h = hmac.new( key, paramstext, hashlib.sha256 ).hexdigest()
request_headers = {
'X-Username': username,
'X-Authorization': h
}
r = requests.post(url, data=params, headers=request_headers)
try:
print_response_json(r)
except:
print_response(r)
def print_response(res):
print('HTTP/{version} {status_code} {reason}\n{headers}\n\n{body}'.format(
version='1.1' if (res.raw.version == 11) else '1.0' if (res.raw.version == 10) else '2.0' if (res.raw.version == 20) else '?',
status_code=res.status_code,
reason=res.reason,
headers='\n'.join('{}: {}'.format(k, v) for k, v in res.headers.items()),
body=res.content,
))
def print_response_json(res):
print('HTTP/{version} {status_code} {reason}\n{headers}\n\n{body}'.format(
version='1.1' if (res.raw.version == 11) else '1.0' if (res.raw.version == 10) else '2.0' if (res.raw.version == 20) else '?',
status_code=res.status_code,
reason=res.reason,
headers='\n'.join('{}: {}'.format(k, v) for k, v in res.headers.items()),
body=json.dumps(res.json(), indent=2),
))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment