Skip to content

Instantly share code, notes, and snippets.

@stfj
Created June 8, 2023 14:36
Show Gist options
  • Save stfj/2b8772ace7006e1e5359083cbbbfa9c0 to your computer and use it in GitHub Desktop.
Save stfj/2b8772ace7006e1e5359083cbbbfa9c0 to your computer and use it in GitHub Desktop.
parses reviews from appfigures
import poplib
import smtplib
from email import message_from_bytes
from email.message import EmailMessage
import openai
import re
import html
openai.api_key = ''
def process_with_gpt(email_body):
print("processing email")
body = ""
if parsed_email.is_multipart():
for part in email_body:
#print(part.get_payload())
body += part.get_payload()
body = re.sub('<[^<]+?>', '', body)
body = html.unescape(body)
body = body.replace('\t', '')
lines = body.split('\n')
# Compile a regular expression pattern that matches lines composed only of spaces, tabs, and equal signs
pattern = re.compile(r'^[=\s\tA-Fa-f0-9]*$')
cleaned_lines = [line for line in lines if not pattern.match(line)]
body = '\n'.join(cleaned_lines)
#print(body)
prompt = 'Please look at this email and let me know what the bugs reported in the feedback are (but tell them to me in a neutral way), and also let me know any very positive encouragement from reviews (give me the exact quotes for encouragement, only if they are very positive). Please make sure to also let me know which app each comment is about. Tell me the app name (and platform), and then the feedback and bugs in a bulleted list underneath: \n\n' + body
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": prompt
}
],
temperature=0
)
#print(response.choices[0].message['content'])
return response.choices[0].message['content']
print("logging into email")
# Connect to the POP3 server, user and pass
pop3_server = poplib.POP3('')
pop3_server.user('')
pop3_server.pass_('')
print("logged into pop")
# Connect to the SMTP server for sending email
smtp_server = smtplib.SMTP_SSL('')
smtp_server.login('', '')
print("logged into smtp")
# Fetch email and process each one
num_messages = len(pop3_server.list()[1])
print(f"Num messages: {num_messages}")
for i in range(num_messages):
raw_email = b'\n'.join(pop3_server.retr(i+1)[1])
parsed_email = message_from_bytes(raw_email)
print(parsed_email['From'])
if parsed_email['From'].__contains__('[email protected]'):
print("Found review, parsing with gpt")
processed_content = process_with_gpt(parsed_email.get_payload())
message = EmailMessage()
message['From'] = ''
message['To'] = ''
message['Subject'] = 'Latest reviews'
message.set_content(processed_content)
smtp_server.send_message(message)
# Delete the email
pop3_server.dele(i+1)
pop3_server.quit()
smtp_server.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment