Guest User

Untitled

a guest
Oct 3rd, 2018
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. from models.py import Email
  2. import getpass, imaplib, email, sys
  3.  
  4. from django.http import HttpResponse
  5.  
  6.  
  7. class NGramSummer(object): #for tallying total ngram count from someone
  8. def __init__(self, from_person): #constructor constructor... whats your...
  9. self.from_person = from_person #self is tagging with the "who" it's from
  10. self.ngrams = dict()
  11.  
  12. def add_ngrams(self, new_ngrams): #this is where the counting is going on
  13. for word in new_ngrams:
  14. if word in self.ngrams:
  15. self.ngrams[word] += new_ngrams[word]
  16. else:
  17. self.ngrams[word] = new_ngrams[word]
  18.  
  19. def get_ngrams(self):
  20. return self.ngrams
  21.  
  22. # NGramCounter builds a dictionary relating ngrams (as tuples) to the number
  23. # of times that ngram occurs in a text (as integers)
  24. class NGramCounter(object):
  25.  
  26. # parameter n is the 'order' (length) of the desired n-gram
  27. def __init__(self, text):
  28. self.text = text
  29. self.ngrams = dict()
  30.  
  31. # feed method calls tokenize to break the given string up into units
  32. def tokenize(self):
  33. return self.text.split(" ")
  34.  
  35. # feed method takes text, tokenizes it, and visits every group of n tokens
  36. # in turn, adding the group to self.ngrams or incrementing count in same
  37. def parse(self):
  38.  
  39. tokens = self.tokenize()
  40. #Moves through every individual word in the text, increments counter if already found
  41. #else sets count to 1
  42. for word in tokens:
  43. if word in self.ngrams:
  44. self.ngrams[word] += 1
  45. else:
  46. self.ngrams[word] = 1
  47.  
  48. def get_ngrams(self):
  49. return self.ngrams
  50.  
  51. def get_first_text_part(msg): #setup to cleanup all text
  52. maintype = msg.get_content_maintype()
  53. if maintype == 'multipart':
  54. for part in msg.get_payload():
  55. if part.get_content_maintype() == 'text':
  56. return part.get_payload()
  57. elif maintype == 'text':
  58. return msg.get_payload()
  59.  
  60.  
  61. def index(request):
  62.  
  63. certain_file = open("lists/list_certain.txt", "r")
  64. negation_file = open("lists/list_negation.txt", "r")
  65. warm_file = open("lists/list_warm.txt", "r")
  66. cold_file = open("lists/list_cold.txt", "r")
  67. firstPOV_file = open("lists/list_1POV.txt", "r")
  68. secondPOV_file = open("lists/list_2POV.txt", "r")
  69. thirdPOV_file = open("lists/list_3POV.txt", "r")
  70.  
  71. certain_list = [word.strip() for word in certain_file.readlines()]
  72. negation_list = [word.strip() for word in negation_file.readlines()]
  73. warm_list = [word.strip() for word in warm_file.readlines()]
  74. cold_list = [word.strip() for word in cold_file.readlines()]
  75. firstPOV_list = [word.strip() for word in firstPOV_file.readlines()]
  76. secondPOV_list = [word.strip() for word in secondPOV_file.readlines()]
  77. thirdPOV_list = [word.strip() for word in thirdPOV_file.readlines()]
  78.  
  79.  
  80.  
  81. #loading profile for login
  82. M = imaplib.IMAP4_SSL('imap.gmail.com')
  83. M.login("wilo@gmail.com", "pass")
  84. # M.select("[Gmail]/Sent Mail")
  85. M.select("[Gmail]/All Mail")
  86.  
  87. xml_template = "<email><from>{sender}</from><to>{to}</to><date>{date}</date><subject>{subject}</subject><body>{body}</body><certain>{certain}</certain><negation>{negation}</negation><warm>{warm}</warm><cold>{cold}</cold><firstPOV>{firstPOV}</firstPOV><secondPOV>{secondPOV}</secondPOV><thirdPOV>{thirdPOV}</thirdPOV><ngrams>{ngrams}</ngrams></email>"
  88.  
  89. person_summary = "<Summary><ngramSUM>{summery}</ngramSUM></Summary>"
  90.  
  91. theperson = ["alinjen@bellsouth.net", "ajarnp@gmail.com", "alinjen@me.com", "dlbergman@gmail.com", "donjen@bellsouth.net", "hljen@bellsouth.net", "trixietree@hotmail.com", "wendycantdrive@hotmail.com", "avery@averymax.com", "chris.laniosz@gmail.com", "frannie.hall@gmail.com", "ian.oliver@flawlessfuture.com", "jasonaston@gmail.com", "seatubers@gmail.com", "seatubers@gmail.com", "idralcar@hotmail.com", "matthew.d.rader@gmail.com", "rader@matthewrader.com", "mslyssa@gmail.com", "guerrajmichael@gmail.com", "pamela@pamelareed.com", "justkeepgoing@excite.com", "studio@reedandrader.com", "ryan@letsneverdie.net", "4stepan@gmail.com", "cmae.oliver@gmail.com", "conniemae.olive@gmail.com", "larissa_bemis@yahoo.com", "larissarbemis@gmail.com", "lbemis@apple.com", "sushionthego@gmail.com", "acm466@nyu.edu", "lia.martinez@nyu.edu", "lia@potiondesign.com", "kiwi@smirkyplop.com", "roisin.stack@gmail.com", "ohannamarie@gmail.com", "bryan.baxter@gmail.com", "davidestici@hotmail.com", "genny.hoffman@gmail.com", "sheenamcneal@gmail.com", "daniel.shiffman@gmail.com", "daniel.shiffman@nyu.edu", "dan.osullivan@nyu.edu", "edward.gordon@nyu.edu", "marianne.petit@nyu.edu", "midori.yasuda@nyu.edu", "nh19@nyu.edu", "rob.ryan@nyu.edu", "EFarnon@wsgc.com", "lori@scoreatthetop.com"]
  92.  
  93. for person in theperson:
  94. print "Searching for:", repr(person)
  95. type, data = M.search(None, 'FROM', person) #Gets ALL messages
  96. summer = NGramSummer(person) #a NGramSummer object,this will sum all the ngrams together
  97. new = open(person+"_from.xml", 'w')
  98. for num in data[0].split(): #Loops through all messages
  99. yp, data = M.fetch(num, '(RFC822)') #Pulls Message
  100. msg = email.message_from_string(data[0][1]) #Puts message into easy to use python objects
  101. _from = msg['from'] #pull from
  102. _to = msg['to'] #pull to
  103. _subject = msg['subject'] #pull subject
  104. _date = msg['date']
  105. _body = get_first_text_part(msg) #pull body
  106. certain_sum = 0
  107. negation_sum = 0
  108. warm_sum = 0
  109. cold_sum = 0
  110. firstPOV_sum = 0
  111. secondPOV_sum = 0
  112. thirdPOV_sum = 0
  113. for word in _body.split(' '):
  114. word = word.lower()
  115. if word in certain_list:
  116. certain_sum += 1
  117. if word in negation_list:
  118. negation_sum += 1
  119. if word in warm_list:
  120. warm_sum += 1
  121. if word in cold_list:
  122. cold_sum += 1
  123. if word in firstPOV_list:
  124. firstPOV_sum += 1
  125. if word in secondPOV_list:
  126. secondPOV_sum += 1
  127. if word in thirdPOV_list:
  128. thirdPOV_sum += 1
  129. if _body:
  130. ngrams = NGramCounter(" ".join(_body.strip(">").split()))
  131. ngrams.parse()
  132. _feed = ngrams.get_ngrams()
  133. summer.add_ngrams(_feed)
  134. email_obj = Email()
  135. email_obj.from = _from
  136. email_obj.to = _to
  137. email_obj.subject = _subject
  138. email_obj.date = _date
  139. email_obj.body = _body
  140. email_obj.certain = certain_sum
  141. email_obj.negation = negation_list
  142. email_obj.warm = warm_sum
  143. email_obj.cold = cold_sum
  144. email_obj.firstPOV = firstPOV_sum
  145. email_obj.secondPOV = secondPOV_sum
  146. email_obj.thirdPOV = thirdPOV_sum
  147. email_obj.save()
  148. #print _feed
  149.  
  150. my_message = xml_template.format(sender=_from, to=_to, date=_date, subject=_subject, ngrams=_feed, body=_body, certain=certain_sum, negation=negation_sum, warm=warm_sum, cold=cold_sum, firstPOV=firstPOV_sum, secondPOV=secondPOV_sum, thirdPOV=thirdPOV_sum)
  151. new.write(my_message)
  152. print repr(my_message)
  153.  
  154. # print 'Content-Type:',msg.get_content_type()
  155. # last_message = xml_template.format(sender="_from", to=_to, date=_date, subject=_subject, body=_body, ngrams=_summer.get_ngrams())
  156.  
  157. # new.write('------summmary----------')
  158. # new.write(str(summer.get_ngrams()))
  159. # print '------summmary----------'
  160. the_summary = summer.get_ngrams()
  161. sum_persons = person_summary.format(summery=the_summary)
  162. new.write(sum_persons)
  163. print repr(summer.get_ngrams())
  164.  
  165. new.close()
  166. M.close()
  167. M.logout()
  168.  
  169. return HttpResponse("Hello, world. You're at the mailerz index")
Add Comment
Please, Sign In to add comment