Guest User

Untitled

a guest
Oct 3rd, 2018
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 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. def index(request):
  7.  
  8. certain_file = open("lists/list_certain.txt", "r")
  9. negation_file = open("lists/list_negation.txt", "r")
  10. warm_file = open("lists/list_warm.txt", "r")
  11. cold_file = open("lists/list_cold.txt", "r")
  12. firstPOV_file = open("lists/list_1POV.txt", "r")
  13. secondPOV_file = open("lists/list_2POV.txt", "r")
  14. thirdPOV_file = open("lists/list_3POV.txt", "r")
  15.  
  16. certain_list = [word.strip() for word in certain_file.readlines()]
  17. negation_list = [word.strip() for word in negation_file.readlines()]
  18. warm_list = [word.strip() for word in warm_file.readlines()]
  19. cold_list = [word.strip() for word in cold_file.readlines()]
  20. firstPOV_list = [word.strip() for word in firstPOV_file.readlines()]
  21. secondPOV_list = [word.strip() for word in secondPOV_file.readlines()]
  22. thirdPOV_list = [word.strip() for word in thirdPOV_file.readlines()]
  23.  
  24. class NGramSummer(object): #for tallying total ngram count from someone
  25. def __init__(self, from_person): #constructor constructor... whats your...
  26. self.from_person = from_person #self is tagging with the "who" it's from
  27. self.ngrams = dict()
  28.  
  29. def add_ngrams(self, new_ngrams): #this is where the counting is going on
  30. for word in new_ngrams:
  31. if word in self.ngrams:
  32. self.ngrams[word] += new_ngrams[word]
  33. else:
  34. self.ngrams[word] = new_ngrams[word]
  35.  
  36. def get_ngrams(self):
  37. return self.ngrams
  38.  
  39. # NGramCounter builds a dictionary relating ngrams (as tuples) to the number
  40. # of times that ngram occurs in a text (as integers)
  41. class NGramCounter(object):
  42.  
  43. # parameter n is the 'order' (length) of the desired n-gram
  44. def __init__(self, text):
  45. self.text = text
  46. self.ngrams = dict()
  47.  
  48. # feed method calls tokenize to break the given string up into units
  49. def tokenize(self):
  50. return self.text.split(" ")
  51.  
  52. # feed method takes text, tokenizes it, and visits every group of n tokens
  53. # in turn, adding the group to self.ngrams or incrementing count in same
  54. def parse(self):
  55.  
  56. tokens = self.tokenize()
  57. #Moves through every individual word in the text, increments counter if already found
  58. #else sets count to 1
  59. for word in tokens:
  60. if word in self.ngrams:
  61. self.ngrams[word] += 1
  62. else:
  63. self.ngrams[word] = 1
  64.  
  65. def get_ngrams(self):
  66. return self.ngrams
  67.  
  68. #loading profile for login
  69. M = imaplib.IMAP4_SSL('imap.gmail.com')
  70. M.login("wilo@gmail.com", "pass")
  71. # M.select("[Gmail]/Sent Mail")
  72. M.select("[Gmail]/All Mail")
  73.  
  74. def get_first_text_part(msg): #setup to cleanup all text
  75. maintype = msg.get_content_maintype()
  76. if maintype == 'multipart':
  77. for part in msg.get_payload():
  78. if part.get_content_maintype() == 'text':
  79. return part.get_payload()
  80. elif maintype == 'text':
  81. return msg.get_payload()
  82.  
  83. 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>"
  84.  
  85. person_summary = "<Summary><ngramSUM>{summery}</ngramSUM></Summary>"
  86.  
  87. # my_message = xml_template.format(sender="sushionthego@gmail.com", to="willimite@gmail.com", date="4/13/2012", subject="Did you drink all the milk or did you throw it out b/c Avery is moving in?", body="see subject", ngrams="somengramsgohere")
  88.  
  89. 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"]
  90.  
  91. for person in theperson:
  92. print "Searching for:", repr(person)
  93. type, data = M.search(None, 'FROM', person) #Gets ALL messages
  94. summer = NGramSummer(person) #a NGramSummer object,this will sum all the ngrams together
  95. new = open(person+"_from.xml", 'w')
  96. for num in data[0].split(): #Loops through all messages
  97. yp, data = M.fetch(num, '(RFC822)') #Pulls Message
  98. msg = email.message_from_string(data[0][1]) #Puts message into easy to use python objects
  99. _from = msg['from'] #pull from
  100. _to = msg['to'] #pull to
  101. _subject = msg['subject'] #pull subject
  102. _date = msg['date']
  103. _body = get_first_text_part(msg) #pull body
  104. certain_sum = 0
  105. negation_sum = 0
  106. warm_sum = 0
  107. cold_sum = 0
  108. firstPOV_sum = 0
  109. secondPOV_sum = 0
  110. thirdPOV_sum = 0
  111. for word in _body.split(' '):
  112. word = word.lower()
  113. if word in certain_list:
  114. certain_sum += 1
  115. if word in negation_list:
  116. negation_sum += 1
  117. if word in warm_list:
  118. warm_sum += 1
  119. if word in cold_list:
  120. cold_sum += 1
  121. if word in firstPOV_list:
  122. firstPOV_sum += 1
  123. if word in secondPOV_list:
  124. secondPOV_sum += 1
  125. if word in thirdPOV_list:
  126. thirdPOV_sum += 1
  127. if _body:
  128. ngrams = NGramCounter(" ".join(_body.strip(">").split()))
  129. ngrams.parse()
  130. _feed = ngrams.get_ngrams()
  131. summer.add_ngrams(_feed)
  132. email_obj = Email()
  133. email_obj.from = _from
  134. email_obj.to = _to
  135. email_obj.subject = _subject
  136. email_obj.date = _date
  137. email_obj.body = _body
  138. email_obj.certain = certain_sum
  139. email_obj.negation = negation_list
  140. email_obj.warm = warm_sum
  141. email_obj.cold = cold_sum
  142. email_obj.firstPOV = firstPOV_sum
  143. email_obj.secondPOV = secondPOV_sum
  144. email_obj.thirdPOV = thirdPOV_sum
  145. email_obj.save()
  146. #print _feed
  147.  
  148. 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)
  149. new.write(my_message)
  150. print repr(my_message)
  151.  
  152. # print 'Content-Type:',msg.get_content_type()
  153. # last_message = xml_template.format(sender="_from", to=_to, date=_date, subject=_subject, body=_body, ngrams=_summer.get_ngrams())
  154.  
  155. # new.write('------summmary----------')
  156. # new.write(str(summer.get_ngrams()))
  157. # print '------summmary----------'
  158. the_summary = summer.get_ngrams()
  159. sum_persons = person_summary.format(summery=the_summary)
  160. new.write(sum_persons)
  161. print repr(summer.get_ngrams())
  162.  
  163. new.close()
  164. M.close()
  165. M.logout()
  166.  
  167. return HttpResponse("Hello, world. You're at the mailerz index")
Add Comment
Please, Sign In to add comment