Created
March 10, 2022 17:19
-
-
Save dropout1337/f1a8387e224556948d10ec8786360629 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import names | |
import cfscrape | |
class ShortlyAI(): | |
def __init__(self): | |
self.session = cfscrape.create_scraper() | |
self.token = None | |
def get_headers(self, token: str = None): | |
headers = { | |
"accept": "application/json, text/plain, */*", | |
"accept-encoding": "gzip, deflate, br", | |
"accept-language": "en-GB,en;q=0.9", | |
"origin": "https://www.shortlyai.com", | |
"referer": "https://www.shortlyai.com/", | |
"sec-fetch-dest": "empty", | |
"sec-fetch-mode": "cors", | |
"sec-fetch-site": "same-site", | |
"sec-gpc": "1", | |
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36" | |
} | |
if token: headers["authorization"] = "JWT %s" % (token) | |
return headers | |
def login(self, username: str, password: str): | |
data = { | |
"email": username, | |
"password": password | |
} | |
response = self.session.post("https://api.shortlyai.com/auth/login/", json=data, headers=self.get_headers()) | |
if response.status_code == 200: | |
self.token = response.json()["token"] | |
return response.json()["token"] | |
else: | |
return response.json() | |
def register(self): | |
email = "%s@gmail.com" % (os.urandom(5).hex()) | |
password = os.urandom(10).hex() | |
data = { | |
"email": email, | |
"first_name": names.get_first_name(), | |
"last_name": names.get_last_name(), | |
"password1": password, | |
"password2": password | |
} | |
response = self.session.post("https://api.shortlyai.com/auth/register/", json=data, headers=self.get_headers()) | |
if response.status_code == 201: | |
self.token = response.json()["token"] | |
return email, password, response.json()["token"] | |
else: | |
return response.json() | |
def story(self, context: str): | |
if self.token == None: | |
return "please login or register first." | |
data = { | |
"document_type": "story", | |
"prompt": context, | |
"content": "", | |
"story_background": "", | |
"ai_instructions": None, | |
"output_length": 1, | |
"is_command": False | |
} | |
response = self.session.post("https://api.shortlyai.com/stories/write-for-me/", json=data, headers=self.get_headers(token=self.token)) | |
if response.status_code == 200: | |
return response.json()["text"] | |
else: | |
return response.json() | |
def article(self, context: str): | |
if self.token == None: | |
return "please login or register first." | |
data = { | |
"document_type": "article", | |
"prompt": context, | |
"content": "", | |
"story_background": "", | |
"ai_instructions": None, | |
"output_length": 1, | |
"is_command": False | |
} | |
response = self.session.post("https://api.shortlyai.com/stories/write-for-me/", json=data, headers=self.get_headers(token=self.token)) | |
if response.status_code == 200: | |
return response.json()["text"] | |
else: | |
return response.json() | |
def run(self): | |
self.register() | |
text = self.story("what is shortlyai?") | |
print(text.replace("\n\n", "\n")) | |
if __name__ == "__main__": | |
client = ShortlyAI() | |
client.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
W