def archive_old_blacklist_mails(email_id, token, use_psync): blacklist = token_store.get_new_blacklist(email_id) if use_psync: print token client = nylas.APIClient(PLANCK_APP_ID, PLANCK_APP_SECRET, token, api_server=psync_url) else: client = nylas.APIClient(APP_ID, APP_SECRET, token) ns = client label_flag = use_labels(ns) if (label_flag): #as per new rules, mails from blacklisted ids should be deleted blacklist_id = get_id(ns, 'Trash') else: blacklist_id = get_folder_id(ns, 'Trash') if (blacklist_id is None): blacklist_id = get_folder_id(ns, 'Deleted Items') if blacklist_id is None: blacklist_id = add_folder(ns, 'Trash') for black_email in blacklist: for message in ns.messages.where(**{'from': black_email}): try: if (label_flag): message.update_labels([blacklist_id]) else: message.update_folder(blacklist_id) except Exception as e: pass # print 'Could not update label for '+black_email+'. Must be in "sent". Skipping' token_store.remove_from_new_blacklist(email_id, black_email)
def get_msg_count_in_range(future_time_stamp, past_time_stamp, email, token, direction, use_psync): if use_psync: client = nylas.APIClient(PLANCK_APP_ID, PLANCK_APP_SECRET, token, api_server=psync_url) else: client = nylas.APIClient(APP_ID, APP_SECRET, token) # print "get_msg_count_in_range" # message_list = client.namespaces[0].messages.where(**{direction:email,'last_message_before' : future_time_stamp,\ # 'last_message_after' : past_time_stamp}) message_list = client.messages.where(**{direction:email,'last_message_before' : future_time_stamp,\ 'last_message_after' : past_time_stamp}) # print future_time_stamp,time.strftime("%D %H:%M", time.localtime(int(future_time_stamp))) # print past_time_stamp,time.strftime("%D %H:%M", time.localtime(int(past_time_stamp))) sent_people_stat = {} for message in message_list: # print message for sent_address in message['to']: sent_address_email = sent_address['email'] if sent_address_email in sent_people_stat: sent_people_stat[sent_address_email] += 1 else: sent_people_stat[sent_address_email] = 1 with open('temp_data.json', 'wb') as fp: json.dump(sent_people_stat, fp=fp, indent=4) return sent_people_stat
def get_nylas_client_(token, psync_use): if psync_use: client = nylas.APIClient(PLANCK_APP_ID, PLANCK_APP_SECRET, token, api_server=psync_url) else: client = nylas.APIClient(APP_ID, APP_SECRET, token) return client
def blacklist_senders(email_id, token, use_psync): blacklist = token_store.get_blacklist(email_id) if use_psync: print token client = nylas.APIClient(PLANCK_APP_ID, PLANCK_APP_SECRET, token, api_server=psync_url) else: client = nylas.APIClient(APP_ID, APP_SECRET, token) ns = client label_flag = use_labels(ns) if (label_flag): #as per new rules, mails from blacklisted ids should be deleted trash_id = get_id(ns, 'Trash') sent_id = get_id(ns, 'Sent Mail') print 'sent_id, trash_id', sent_id, trash_id else: trash_id = get_folder_id(ns, 'Trash') if (trash_id is None): trash_id = get_folder_id(ns, 'Deleted Items') if trash_id is None: trash_id = add_folder(ns, 'Trash') black_threads = ns.threads.where(**{'in': 'Black Hole'}) black_senders = set() for black_thread in black_threads: print 'black_thread', black_thread['id'] if len(black_thread['message_ids']) > 0: black_msg = ns.messages.find(black_thread['message_ids'][0]) for sender in black_msg['from']: black_senders.add(sender['email']) if label_flag: updates = [trash_id] if has_sent_label(black_thread, sent_id): updates.append(sent_id) black_thread.update_labels(updates) else: black_thread.update_folder(trash_id) for sender in black_senders: if sender not in blacklist: token_store.add_to_blacklist(email_id, sender)
def send_nylas_email(self, token): self.is_sent = True client = nylas.APIClient(NYLAS_OAUTH_CLIENT_ID, NYLAS_OAUTH_CLIENT_SECRET, token) all_attachments = [] if self.attachments.count() > 0: attachments = self.attachments.all() for file_attachment in attachments: if file_attachment.content_type != '': body = file_attachment.file.read() base_64_body = base64.b64encode(body) # Create the attachment myfile = client.files.create() myfile.filename = file_attachment.original_name myfile.data = base_64_body all_attachments.append(myfile) contact_full_name = ' '.join([self.first_name, self.last_name]) draft = client.drafts.create() draft.to = [{'name': contact_full_name, 'email': self.to}] draft.subject = self.subject draft.body = self.body draft.tracking = { 'links': 'true', 'opens': 'true', 'thread_replies': 'true', 'payload': str(self.pk) } for attachment in all_attachments: draft.attach(attachment) try: resp = draft.send() self.delivered = True self.nylas_account_id = resp['account_id'] self.nylas_id = resp['id'] self.nylas_thread_id = resp['thread_id'] return resp except nylas.client.errors.ConnectionError as e: print('Unable to connect to the SMTP server.') raise Exception(e) except nylas.client.errors.MessageRejectedError as e: print('Message got rejected by the SMTP server!') print(e.message) print(e.server_error) raise Exception(e)
def get_thread_participant_score_with_tags(future_time_stamp, past_time_stamp, token, tag, self_email, use_psync): if use_psync: client = nylas.APIClient(PLANCK_APP_ID, PLANCK_APP_SECRET, token, api_server=psync_url) else: client = nylas.APIClient(APP_ID, APP_SECRET, token) # thread_list = client.namespaces[0].threads.where(**{'tag':tag,'last_message_before' : future_time_stamp,\ # 'last_message_after' : past_time_stamp}) # thread_list = client.threads.where(**{'tag':tag,'last_message_before' : future_time_stamp,\ # 'last_message_after' : past_time_stamp}) # depreciated tags api thread_list = client.threads.where(**{tag:True,'last_message_before' : future_time_stamp,\ 'last_message_after' : past_time_stamp}) # here we are directly using "high level flags" inspead of tags exp_count = 0 nexp_count = 0 people_stat = {} for thread in thread_list: # pprint.pprint(thread) participant_set = set([x['email'] for x in thread['participants']]) try: participant_set.remove(self_email) nexp_count += 1 except: exp_count += 1 for participant in participant_set: if participant in people_stat: people_stat[participant] += 1 else: people_stat[participant] = 1 # print 'exp_count : ',exp_count # print 'nexp_count : ',nexp_count return people_stat
def tag_unread_mails_in_time_range(email_id, token, now_time, old_time, white_list, social_list=[], use_psync=False): if use_psync: client = nylas.APIClient(PLANCK_APP_ID, PLANCK_APP_SECRET, token, api_server=psync_url) else: client = nylas.APIClient(APP_ID, APP_SECRET, token) ns = client recent_threads = ns.threads.where( **{ 'last_message_after': old_time - 600, 'last_message_before': now_time, 'in': 'inbox' }) # recent_threads = ns.threads.where(**{'from':'*****@*****.**', 'in':'inbox'}) recent_threads_list = [x for x in recent_threads] request_set = set([]) for thread in recent_threads_list: # print thread['participants'] plist = get_other_participants_in_thread(thread, email_id) for participant in plist: request_set.add(participant) score_dict = token_store.get_contact_score_list(email_id, list(request_set)) if use_labels(ns): inbox_id = get_id(ns, 'Inbox') read_now_id = get_id(ns, 'Read Now') read_later_id = get_id(ns, 'Read Later') if read_later_id is None: read_later_id = get_id(ns, 'Read Later') social_id = get_id(ns, 'Social') if social_id is None: social_id = add_label(ns, 'Social') #as per new rules, mails from blacklisted email ids should be deleted. blacklist_id = get_id(ns, 'Trash') label_flag = True else: inbox_id = get_folder_id(ns, 'Inbox') read_now_id = get_folder_id(ns, 'Read Now') read_later_id = get_folder_id(ns, 'Read Later') if read_later_id is None: read_later_id = add_folder(ns, 'Read Later') social_id = get_folder_id(ns, 'Social') if social_id is None: social_id = add_folder(ns, 'Social') blacklist_id = get_folder_id(client, 'Trash') if blacklist_id is None: blacklist_id = get_folder_id(client, 'Deleted Items') if blacklist_id is None: blacklist_id = add_folder(client, 'Trash') print 'Created Trash folder for', email_id label_flag = False for thread in recent_threads_list: # TODO refactor using is_object_important plist = get_other_participants_in_thread(thread, email_id) blacklist = token_store.get_blacklist(email_id) blacklist_flag = False if overlap(plist, blacklist) > 0: blacklist_flag = True if (blacklist_flag): # print 'INFO:', email_id, thread['id'], "B" add_thread_to_blacklist(thread, label_flag, blacklist_id) continue social_list_flag = is_social_mail(email_id, thread['subject'], thread['participants'], social_list) important_flag = is_from_planck(plist) if not important_flag: important_flag = is_white_listed_mail(thread['subject'], white_list) if not important_flag: important_flag = contains_contact(email_id, plist) if not important_flag: for p_id in plist: important_flag = is_new_contact(ns, old_time - 600, p_id, thread) if important_flag: break score = 0.0 for participant in plist: score += score_dict[participant.encode('ascii', 'ignore').lower()] if score > 0 or important_flag: #TODO: check if the email is not marked as something else add_thread_to_readnow(thread, label_flag, inbox_id, read_now_id, read_later_id) # print 'INFO:',email_id, thread['id'],"N" else: if (social_list_flag): # print 'INFO:',email_id,thread['id'],"S", add_thread_to_social(thread, label_flag, inbox_id, read_now_id, social_id) continue add_thread_to_clutter(thread, label_flag, inbox_id, read_now_id, read_later_id) # tag_thread_given_condition(thread,label_flag,read_now_id,read_later_id,score,boolean_flags) # print 'INFO:',email_id, thread['id'],"L" return
import nylas APP_ID = '5girg6tjmjuenujbsg0lnatlq' APP_SECRET = '8fokx1yoht10ypwdgev3rqqlp' token = 'mdAIdH097TNhRWF7GEMqaMi9tGH1Jy' # client = nylas.APIClient(APP_ID, APP_SECRET, token) # account = client.accounts # print account client = nylas.APIClient(APP_ID, APP_SECRET, token) account = client.accounts # Fetch the first thread thread = account.threads.first()