2023-04-27 14:43:59 +00:00
from random import choice
from time import time
from colorama import Fore , init ;
from names import get_first_name , get_last_name
from requests import Session
from requests import post
init ( )
2023-04-06 19:57:16 +00:00
class logger :
@staticmethod
def info ( string ) - > print :
import datetime
now = datetime . datetime . now ( )
2023-04-27 14:43:59 +00:00
return print (
f " { Fore . CYAN } { now . strftime ( ' % Y- % m- %d % H: % M: % S ' ) } { Fore . BLUE } INFO { Fore . MAGENTA } __main__ -> { Fore . RESET } { string } " )
2023-04-06 19:57:16 +00:00
class SonicResponse :
class Completion :
class Choices :
def __init__ ( self , choice : dict ) - > None :
2023-04-27 14:43:59 +00:00
self . text = choice [ ' text ' ]
self . content = self . text . encode ( )
self . index = choice [ ' index ' ]
self . logprobs = choice [ ' logprobs ' ]
self . finish_reason = choice [ ' finish_reason ' ]
2023-04-06 19:57:16 +00:00
def __repr__ ( self ) - > str :
return f ''' <__main__.APIResponse.Completion.Choices( \n text = { self . text . encode ( ) } , \n index = { self . index } , \n logprobs = { self . logprobs } , \n finish_reason = { self . finish_reason } )object at 0x1337> '''
def __init__ ( self , choices : dict ) - > None :
self . choices = [ self . Choices ( choice ) for choice in choices ]
class Usage :
def __init__ ( self , usage_dict : dict ) - > None :
2023-04-27 14:43:59 +00:00
self . prompt_tokens = usage_dict [ ' prompt_chars ' ]
self . completion_tokens = usage_dict [ ' completion_chars ' ]
self . total_tokens = usage_dict [ ' total_chars ' ]
2023-04-06 19:57:16 +00:00
def __repr__ ( self ) :
return f ''' <__main__.APIResponse.Usage( \n prompt_tokens = { self . prompt_tokens } , \n completion_tokens = { self . completion_tokens } , \n total_tokens = { self . total_tokens } )object at 0x1337> '''
2023-04-27 14:43:59 +00:00
2023-04-06 19:57:16 +00:00
def __init__ ( self , response_dict : dict ) - > None :
2023-04-27 14:43:59 +00:00
self . response_dict = response_dict
self . id = response_dict [ ' id ' ]
self . object = response_dict [ ' object ' ]
self . created = response_dict [ ' created ' ]
self . model = response_dict [ ' model ' ]
self . completion = self . Completion ( response_dict [ ' choices ' ] )
self . usage = self . Usage ( response_dict [ ' usage ' ] )
2023-04-06 19:57:16 +00:00
def json ( self ) - > dict :
return self . response_dict
2023-04-27 14:43:59 +00:00
2023-04-06 19:57:16 +00:00
class Account :
session = Session ( )
session . headers = {
2023-04-27 14:43:59 +00:00
" connection " : " keep-alive " ,
" sec-ch-ua " : " \" Not_A Brand \" ;v= \" 99 \" , \" Google Chrome \" ;v= \" 109 \" , \" Chromium \" ;v= \" 109 \" " ,
" accept " : " application/json, text/plain, */* " ,
" content-type " : " application/json " ,
" sec-ch-ua-mobile " : " ?0 " ,
" user-agent " : " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 " ,
2023-04-06 19:57:16 +00:00
" sec-ch-ua-platform " : " \" Windows \" " ,
2023-04-27 14:43:59 +00:00
" sec-fetch-site " : " same-origin " ,
" sec-fetch-mode " : " cors " ,
" sec-fetch-dest " : " empty " ,
2023-04-06 19:57:16 +00:00
# "accept-encoding" : "gzip, deflate, br",
2023-04-27 14:43:59 +00:00
" accept-language " : " en-GB,en-US;q=0.9,en;q=0.8 " ,
" cookie " : " "
2023-04-06 19:57:16 +00:00
}
2023-04-27 14:43:59 +00:00
2023-04-06 19:57:16 +00:00
@staticmethod
def get_user ( ) :
password = f ' 0opsYouGoTme@1234 '
2023-04-27 14:43:59 +00:00
f_name = get_first_name ( )
l_name = get_last_name ( )
hosts = [ ' gmail.com ' , ' protonmail.com ' , ' proton.me ' , ' outlook.com ' ]
2023-04-06 19:57:16 +00:00
return {
2023-04-27 14:43:59 +00:00
" email " : f " { f_name . lower ( ) } . { l_name . lower ( ) } @ { choice ( hosts ) } " ,
" password " : password ,
" confirm_password " : password ,
" full_name " : f ' { f_name } { l_name } '
2023-04-06 19:57:16 +00:00
}
@staticmethod
def create ( logging : bool = False ) :
while True :
try :
2023-04-27 14:43:59 +00:00
user = Account . get_user ( )
start = time ( )
response = Account . session . post ( " https://app.writesonic.com/api/session-login " , json = user | {
" utmParams " : " {} " ,
" visitorId " : " 0 " ,
" locale " : " en " ,
" userAgent " : " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 " ,
" signInWith " : " password " ,
" request_type " : " signup " ,
2023-04-06 19:57:16 +00:00
} )
2023-04-27 14:43:59 +00:00
2023-04-06 19:57:16 +00:00
if logging :
logger . info ( f " \x1b [31mregister success \x1b [0m : ' { response . text [ : 30 ] } ... ' ( { int ( time ( ) - start ) } s) " )
logger . info ( f " \x1b [31mid \x1b [0m : ' { response . json ( ) [ ' id ' ] } ' " )
logger . info ( f " \x1b [31mtoken \x1b [0m : ' { response . json ( ) [ ' token ' ] [ : 30 ] } ... ' " )
2023-04-27 14:43:59 +00:00
2023-04-06 19:57:16 +00:00
start = time ( )
2023-04-27 14:43:59 +00:00
response = Account . session . post ( " https://api.writesonic.com/v1/business/set-business-active " ,
headers = { " authorization " : " Bearer " + response . json ( ) [ ' token ' ] } )
2023-04-06 19:57:16 +00:00
key = response . json ( ) [ " business " ] [ " api_key " ]
if logging : logger . info ( f " \x1b [31mgot key \x1b [0m : ' { key } ' ( { int ( time ( ) - start ) } s) " )
return Account . AccountResponse ( user [ ' email ' ] , user [ ' password ' ] , key )
2023-04-27 14:43:59 +00:00
2023-04-06 19:57:16 +00:00
except Exception as e :
if logging : logger . info ( f " \x1b [31merror \x1b [0m : ' { e } ' " )
continue
2023-04-27 14:43:59 +00:00
2023-04-06 19:57:16 +00:00
class AccountResponse :
def __init__ ( self , email , password , key ) :
2023-04-27 14:43:59 +00:00
self . email = email
2023-04-06 19:57:16 +00:00
self . password = password
2023-04-27 14:43:59 +00:00
self . key = key
2023-04-06 19:57:16 +00:00
class Completion :
def create (
2023-04-27 14:43:59 +00:00
api_key : str ,
prompt : str ,
enable_memory : bool = False ,
enable_google_results : bool = False ,
history_data : list = [ ] ) - > SonicResponse :
response = post ( ' https://api.writesonic.com/v2/business/content/chatsonic?engine=premium ' ,
headers = { " X-API-KEY " : api_key } ,
json = {
" enable_memory " : enable_memory ,
" enable_google_results " : enable_google_results ,
" input_text " : prompt ,
" history_data " : history_data } ) . json ( )
2023-04-06 19:57:16 +00:00
return SonicResponse ( {
2023-04-27 14:43:59 +00:00
' id ' : f ' cmpl-premium- { int ( time ( ) ) } ' ,
' object ' : ' text_completion ' ,
' created ' : int ( time ( ) ) ,
' model ' : ' premium ' ,
' choices ' : [ {
' text ' : response [ ' message ' ] ,
' index ' : 0 ,
' logprobs ' : None ,
' finish_reason ' : ' stop '
} ] ,
' usage ' : {
' prompt_chars ' : len ( prompt ) ,
' completion_chars ' : len ( response [ ' message ' ] ) ,
' total_chars ' : len ( prompt ) + len ( response [ ' message ' ] )
}
} )