forked from OatmealJester/JobSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_files.py
101 lines (79 loc) · 4 KB
/
parse_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os.path
import email
import base64
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from datetime import datetime, timedelta
import google.generativeai as palm
import os
import json
import ast
def get_data():
palm.configure(api_key='AIzaSyA4Q4S0uYiv_5MFt8FdxGoOwjvEt2DhkyY')
SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"]
creds = None
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
"credentials.json", SCOPES
)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open("token.json", "w") as token:
token.write(creds.to_json())
service = build("gmail", "v1", credentials=creds)
one_week_ago = (datetime.now() - timedelta(days=1)).strftime("%Y/%m/%d")
query = f'after:{one_week_ago} category:primary'
gg=service.users().messages().list(userId='me', q=query).execute().get('messages', [])
bodies = {}#[" "] * len(gg)
j=0
for i in range(len(gg)):
try:
message = service.users().messages().get(userId='me', id=gg[i]['id']).execute()
parts = message['payload']['parts']
for part in parts:
if part['mimeType'] == 'text/plain':
body_data = part['body']['data']
question="Below I have attached the details of emails I received in my inbox. Go through the text and see if it is about job applications and not workshops or tutorials?If so find the job position, company, the status of the job application,classify the status either as accepted, rejected, interviewing, submitted ,online assessment scheduled or None. Pass the information as a python dictionary with the fields company, position and status. Make sure that the status is classified as either accepted, rejected, interviewing, submitted or online assessment scheduled. If it is not a job application, don't write anything."
email_body = base64.urlsafe_b64decode(body_data.encode('UTF-8')).decode('UTF-8')
prompt=question+":"+str(email_body)
# print("p")
response=palm.generate_text(prompt=prompt)
# print("r")
if( response.result==None):
continue
else:
print(response.result)
context=ast.literal_eval( response.result)
no_list=[None,'NA','None','','N/A','n/a','na','Not Found','Unknown','not a job application','Not a job application']
if not context:
continue
if any(context["company"]==c for c in no_list):
continue
if any(context["position"]==c for c in no_list):
continue
if any(context["status"]==c for c in no_list):
continue
bodies[j]=context
j+=1
# print(i)
except Exception as e:
continue
print(f"An error occurred: {str(e)}")
#
### Final format, yet to be decided. So sneak peak of the dictionary outputs after parsing all the emails from your inbox.
print(str(bodies))
f = open("demofile3.txt", "a")
for item in bodies.items():
# print(item)
f.write(str(item[1])+"\n")
f.close()
return bodies
# get_data()