Skip to content

Instantly share code, notes, and snippets.

@cjavad
Last active November 15, 2017 15:42
Show Gist options
  • Select an option

  • Save cjavad/9699efee7e59e609e8169e48e1fe5c31 to your computer and use it in GitHub Desktop.

Select an option

Save cjavad/9699efee7e59e609e8169e48e1fe5c31 to your computer and use it in GitHub Desktop.
A quick script that fetches random data from https://myjson.com (great place by the way)
#!bin/python3
import sys
import random
from string import digits, ascii_uppercase, ascii_lowercase
from requests import get
base_url = "https://api.myjson.com/bins/"
def gen():
return ''.join([random.choice(digits + ascii_lowercase) for x in range(5)])
def a(amount, write = False):
out = list()
i = 0
while i <= amount:
key = gen()
text = get(base_url + key).text
if str(text) == '{"status":404,"message":"Not Found"}':
pass
else:
if write == "write":
open(key + ".json", "w").write(text)
out.append(key + ".json")
else:
out.append(text)
i += 1
return out
if len(sys.argv) < 1:
print("Not enough args")
exit()
else:
print(a(int(sys.argv[1]), sys.argv[2]))
@cjavad
Copy link
Author

cjavad commented Nov 15, 2017

Supports writing to files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment