Advertisement
Guest User

Untitled

a guest
May 10th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. from django.shortcuts import render_to_response, get_object_or_404
  2. from django.template import RequestContext
  3. from django.core.cache import cache
  4. from django.conf import settings
  5.  
  6. from datetime import datetime
  7.  
  8. from urllib2 import HTTPError
  9. import twitterext
  10. import pytz
  11.  
  12. from models import *
  13.  
  14. def status_sorter(s1, s2):
  15. if s1.created_at_in_seconds < s2.created_at_in_seconds:
  16. return 1
  17. elif s1.created_at_in_seconds > s2.created_at_in_seconds:
  18. return -1
  19. return 0
  20.  
  21. def parse_twitter_http_error(e):
  22. if e.code == 401:
  23. error = "Unknown Username/Password"
  24. elif e.code == 400:
  25. error = "Twitter API Rate Limit Exceeded - Please try back later"
  26. elif e.code == 404:
  27. error = "Unknown User"
  28. else:
  29. error = str(e)
  30. return error
  31.  
  32. def get_api(tribe):
  33. acc, pwd = tribe.master_account, tribe.master_password
  34. if acc and pwd:
  35. api = twitterext.SearchApi(username=acc, password=pwd)
  36. else:
  37. api = twitterext.SearchApi()
  38. return api
  39.  
  40. def get_search_results(tribe, check_cache=True):
  41. """ This function is used to call the twitter api to seach for a tribe's interest terms
  42. either from a view or in a cronjob to cache the interest search in advance
  43. Note: there is also the utility function 'cache_search' that will prefetch all tribes status updates"""
  44.  
  45. if check_cache:
  46. search_results = cache.get(tribe.slug+'_search')
  47. else:
  48. search_results = None
  49.  
  50. if not search_results:
  51. api=get_api(tribe)
  52. search_results = api.Search(" OR ".join([t.term for t in tribe.interestterm_set.all()]))
  53. cache.set(tribe.slug, search_results, tribe.update_interval)
  54.  
  55. return search_results
  56.  
  57. def cache_search(slug=None):
  58. """ Function to be called in a cronjob to cache the search results for interest terms
  59. for all tribes or one tribe dependent on parameters
  60.  
  61. Calling with slug set to None will retrieve all tribes """
  62. if slug:
  63. tribes = Tribe.objects.filter(slug=slug)
  64. else:
  65. tribes = Tribe.objects.all()
  66.  
  67. for tribe in tribes:
  68. try:
  69. get_search_results(tribe, False)
  70. except HTTPError, e:
  71. # an HTTPError means that the Twitter api returned an error, parse it and pass it back
  72. error = parse_twitter_http_error(e)
  73. return error
  74.  
  75. def get_status_updates(tribe, members, check_cache=True):
  76. """ This function is to be used to fetch all status updates for a particular tribe or in a cronjob
  77. to cache tribe statuses in advance
  78. Note: there is also the utility function 'cache_tribes' that will prefetch all tribes status updates"""
  79.  
  80. if check_cache:
  81. status_list = cache.get(tribe.slug+'_status')
  82. else:
  83. status_list = None
  84.  
  85. if not status_list:
  86. # if the status list isn't in the cache, generate it from the API
  87. api = get_api(tribe)
  88. status_list = []
  89. for m in members:
  90. try:
  91. status_list.extend(api.GetUserTimeline(user=m.twitter_account, count=tribe.max_status))
  92. except HTTPError, e:
  93. # watch for unknown user exceptions as we retrieve the status list
  94. if e.code != 404 and e.code != 401:
  95. raise # reraise the exception if it's not an unknown user or the user is protected
  96.  
  97. # sort the statuses
  98. status_list.sort(status_sorter)
  99.  
  100. # add a datetime to each status (better done on creation but will need to extend python-twitter)
  101. for s in status_list:
  102. s.created_at_as_datetime = pytz.utc.localize(datetime.fromtimestamp(s.created_at_in_seconds)).astimezone(pytz.timezone('US/Central'))
  103.  
  104. cache.set(tribe.slug+'_status', status_list, tribe.update_interval)
  105.  
  106. return status_list
  107.  
  108. def cache_tribes(slug=None):
  109. """ Function to be called in a cronjob to cache the status lists for all tribes or one tribe dependent on parameters
  110. Calling with slug set to None will retrieve all tribes """
  111. if slug:
  112. tribes = Tribe.objects.filter(slug=slug)
  113. else:
  114. tribes = Tribe.objects.all()
  115.  
  116. for tribe in tribes:
  117. try:
  118. get_status_updates(tribe, tribe.member_set.all(), False)
  119. except HTTPError, e:
  120. # an HTTPError means that the Twitter api returned an error, parse it and pass it back
  121. error = parse_twitter_http_error(e)
  122. return error
  123.  
  124. def tribe(request, tribe_slug, tribe_template='twtribes/tribe.html', error_template='twtribes/tribe_error.html'):
  125. """ Django view function that displays collated tribe member statuses
  126. """
  127. tribe = get_object_or_404(Tribe, slug__iexact=tribe_slug)
  128. members = tribe.member_set.order_by('twitter_account')
  129.  
  130. # initialize the API, using the master tribe username and password
  131. # using the username and password for a tribe allows for API rate limit lifting at Twitter
  132. try:
  133. status_list = get_status_updates(tribe, members)
  134. if request.GET and 'filter' in request.GET:
  135. f = request.GET['filter']
  136. f_upper = f.upper()
  137. status_list = [s for s in status_list if s.text.upper().find(f_upper) > -1 or s.user.screen_name.upper().find(f_upper) > -1]
  138. else:
  139. f = None
  140.  
  141. except HTTPError, e:
  142. # an HTTPError means that the Twitter api returned an error, parse it and return it to an error template
  143. error = parse_twitter_http_error(e)
  144.  
  145. return render_to_response(error_template,
  146. { 'error' : error, 'exception' : e },
  147. context_instance=RequestContext(request))
  148.  
  149. return render_to_response(tribe_template,
  150. { 'tribe' : tribe, 'members' : members, 'status_list' : status_list, 'filter' : f },
  151. context_instance=RequestContext(request))
  152.  
  153. def tribe_search(request, tribe_slug, tribe_template='twtribes/tribe_search.html', error_template='twtribes/tribe_error.html'):
  154. """ Django view function that displays search results of tribe interest terms
  155. """
  156. tribe = get_object_or_404(Tribe, slug__iexact=tribe_slug)
  157. members = tribe.member_set.order_by('twitter_account')
  158.  
  159. # initialize the API, using the master tribe username and password
  160. # using the username and password for a tribe allows for API rate limit lifting at Twitter
  161. try:
  162. search_results = get_search_results(tribe)
  163. results = search_results.results
  164. if request.GET and 'filter' in request.GET:
  165. f = request.GET['filter']
  166. f_upper = f.upper()
  167. results = [s for s in results if s.text.upper().find(f_upper) > -1 or s.from_user.upper().find(f_upper) > -1]
  168. else:
  169. f = None
  170.  
  171. except HTTPError, e:
  172. # an HTTPError means that the Twitter api returned an error, parse it and return it to an error template
  173. error = parse_twitter_http_error(e)
  174.  
  175. return render_to_response(error_template,
  176. { 'error' : error, 'exception' : e },
  177. context_instance=RequestContext(request))
  178.  
  179. return render_to_response(tribe_template,
  180. { 'tribe' : tribe, 'members' : members, 'search' : search_results, 'results' : results, 'filter' : f },
  181. context_instance=RequestContext(request))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement