|
a |
|
b/development/paraphrase/paraphrase.py |
|
|
1 |
import time |
|
|
2 |
import json |
|
|
3 |
import requests |
|
|
4 |
from decouple import config |
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
class wordtune(): |
|
|
9 |
def __init__(self, token, draftId): |
|
|
10 |
super(wordtune, self).__init__() |
|
|
11 |
self.token = token |
|
|
12 |
self.draftId = draftId |
|
|
13 |
self.url = "https://api.wordtune.com/rewrite" |
|
|
14 |
|
|
|
15 |
# Generate Payload |
|
|
16 |
def payload_generator(self, inp): |
|
|
17 |
inp_len = len(inp) |
|
|
18 |
|
|
|
19 |
payload = json.dumps({ |
|
|
20 |
"text": inp, |
|
|
21 |
"action": "REWRITE", |
|
|
22 |
"start": 0, |
|
|
23 |
"end": inp_len, |
|
|
24 |
"selection": { |
|
|
25 |
"wholeText": inp, |
|
|
26 |
"bulletText": "", |
|
|
27 |
"start": 0, |
|
|
28 |
"end": inp_len |
|
|
29 |
}, |
|
|
30 |
"draftId": self.draftId, |
|
|
31 |
"emailAccount": None, |
|
|
32 |
"emailMetadata": {}, |
|
|
33 |
"lookaheadIndex": 0 |
|
|
34 |
}) |
|
|
35 |
|
|
|
36 |
payload_without_whitespace = payload.replace( |
|
|
37 |
": ", ":").replace(", ", ",") |
|
|
38 |
# Calulate content_length header |
|
|
39 |
self.content_length = str(len(payload_without_whitespace)) |
|
|
40 |
|
|
|
41 |
return payload |
|
|
42 |
|
|
|
43 |
# Generate Headers |
|
|
44 |
def headers_generator(self): |
|
|
45 |
headers = { |
|
|
46 |
'host': 'api.wordtune.com', |
|
|
47 |
'connection': 'keep-alive', |
|
|
48 |
'content-length': self.content_length, |
|
|
49 |
'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"', |
|
|
50 |
'dnt': '1', |
|
|
51 |
'x-wordtune-origin': 'null', |
|
|
52 |
'x-wordtune-version': '0.0.1', |
|
|
53 |
'sec-ch-ua-mobile': '?0', |
|
|
54 |
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36', |
|
|
55 |
'content-type': 'application/json', |
|
|
56 |
'x-wordtune': '1', |
|
|
57 |
'token': self.token, |
|
|
58 |
'accept': '*/*', |
|
|
59 |
'origin': 'https://app.wordtune.com', |
|
|
60 |
'sec-fetch-site': 'same-site', |
|
|
61 |
'sec-fetch-mode': 'cors', |
|
|
62 |
'sec-fetch-dest': 'empty', |
|
|
63 |
'referer': 'https://app.wordtune.com/', |
|
|
64 |
'accept-encoding': 'gzip, deflate', |
|
|
65 |
'accept-language': 'en-US,en;q=0.9' |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
return headers |
|
|
69 |
|
|
|
70 |
# Performing Post HTTP request |
|
|
71 |
def requests(self, payload, headers): |
|
|
72 |
# Make delay for security reason - Account Suspension Prevention |
|
|
73 |
time.sleep(2) |
|
|
74 |
response = requests.request( |
|
|
75 |
"POST", self.url, headers=headers, data=payload) |
|
|
76 |
response_json = response.json() |
|
|
77 |
|
|
|
78 |
return response_json |
|
|
79 |
|
|
|
80 |
# Get list of suggestions |
|
|
81 |
def get_suggestion(self, response_json, inp): |
|
|
82 |
suggestion_list = [] |
|
|
83 |
|
|
|
84 |
for suggestion in response_json['suggestions']: |
|
|
85 |
wordtune_changedText = suggestion[0] |
|
|
86 |
changedText_start = suggestion[1][0] |
|
|
87 |
changedText_end = suggestion[1][1] |
|
|
88 |
rephrased = inp[0:changedText_start] + \ |
|
|
89 |
wordtune_changedText + inp[changedText_end:-1] |
|
|
90 |
suggestion_list.append(rephrased) |
|
|
91 |
|
|
|
92 |
return suggestion_list |
|
|
93 |
|
|
|
94 |
|
|
|
95 |
def main(): |
|
|
96 |
# environmental variables must be in .env file in the same dir as this file |
|
|
97 |
token = config('TOKEN') |
|
|
98 |
draftId = config('DRAFTID') |
|
|
99 |
paraphrase = wordtune(token, draftId) |
|
|
100 |
|
|
|
101 |
inps = ['How are you?', 'How should I take Acetaminophen?'] |
|
|
102 |
for inp in inps: |
|
|
103 |
payload = paraphrase.payload_generator(inp) |
|
|
104 |
headers = paraphrase.headers_generator() |
|
|
105 |
response = paraphrase.requests(payload, headers) |
|
|
106 |
suggestion_list = paraphrase.get_suggestion(response, inp) |
|
|
107 |
print(suggestion_list) |
|
|
108 |
|
|
|
109 |
|
|
|
110 |
if __name__ == '__main__': |
|
|
111 |
main() |