/MailingList.py

https://github.com/cherokee/web · Python · 140 lines · 78 code · 26 blank · 36 comment · 16 complexity · 0a2c67d937593a559da247e3bb915915 MD5 · raw file

  1. # -*- Mode: python; coding: utf-8 -*-
  2. #
  3. # Cherokee Web Site
  4. #
  5. # Authors:
  6. # Alvaro Lopez Ortega <alvaro@alobbs.com>
  7. #
  8. # Copyright (C) 2001-2011 Alvaro Lopez Ortega
  9. #
  10. # This program is free software; you can redistribute it and/or
  11. # modify it under the terms of version 2 of the GNU General Public
  12. # License as published by the Free Software Foundation.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  22. # 02110-1301, USA.
  23. #
  24. import os
  25. import CTK
  26. import time
  27. import urllib
  28. import feedparser
  29. ML_RSS_URL = "http://groups.google.com/group/cherokee-http/feed/atom_v1_0_msgs.xml?num=50"
  30. CACHE_EXPIRATION = 30 * 60 #30mins
  31. #
  32. # Internals
  33. #
  34. def get_mailing_list_subjects():
  35. # Fetch
  36. d = feedparser.parse (ML_RSS_URL)
  37. # Parse
  38. subjects = {}
  39. subject_list = []
  40. month_link = ''
  41. month_page = ''
  42. for entry in d['entries']:
  43. date = entry['updated'][:10]
  44. author = entry['author']
  45. nick = author.split(' ')[0]
  46. link = 'http://lists.octality.com/pipermail/cherokee/%s/thread.html' % (time.strftime("%Y-%B", time.strptime(date, "%Y-%m-%d")))
  47. if month_link != link:
  48. month_link = link
  49. month_page = urllib.urlopen(month_link).read()
  50. # Clean title
  51. title = entry['title']
  52. if title.lower().startswith("re: "):
  53. title = title[4:]
  54. if title.lower().startswith("[cherokee] "):
  55. title = title[11:]
  56. if len(title) > 45:
  57. title = title[:45] + " .."
  58. # Clean author
  59. n = author.find ('(')
  60. if n != -1:
  61. author = author[:n-1]
  62. if not subjects.has_key(title):
  63. subjects[title] = {'hits':1}
  64. subject_list.append(title)
  65. else:
  66. subjects[title]['hits'] += 1
  67. if not subjects[title].has_key('date'):
  68. subjects[title]['date'] = time.strftime("%b %d", time.strptime(date, "%Y-%m-%d"))
  69. if not subjects[title].has_key('authors'):
  70. subjects[title]['authors'] = []
  71. if not nick in subjects[title]['authors']:
  72. subjects[title]['authors'] += [nick]
  73. if not subjects[title].has_key('link'):
  74. try:
  75. thread_link = get_thread_link (month_page, title[:45])
  76. link = link.split('thread.html')[0] + thread_link
  77. except:
  78. pass
  79. subjects[title]['link'] = link
  80. return (subjects, subject_list)
  81. #
  82. # Widget
  83. #
  84. class Latest_Mailing_List_Widget (CTK.Box):
  85. def __init__ (self, limit=6):
  86. CTK.Box.__init__ (self, {'id': 'mailing-list'})
  87. self += CTK.Box({'class': 'bar3-title'}, CTK.RawHTML('<a href="http://lists.octality.com/listinfo/cherokee" target="_blank">Mailing List</a>'))
  88. ret = get_mailing_list_subjects()
  89. subjects, subject_list = ret
  90. for s in subject_list[:limit]:
  91. subject = subjects[s]
  92. authors = '(%s)' %(', '.join(subject['authors']))
  93. content_box = CTK.Box({'class': 'mail'})
  94. date_box = CTK.Box({'class': 'date'})
  95. date_box += CTK.RawHTML(subject['date'])
  96. content_box += date_box
  97. box = CTK.Box({'class': 'mail-txt'})
  98. box += CTK.LinkWindow (subject['link'], CTK.RawHTML(s))
  99. box += CTK.RawHTML ('<br/>by %s, %s messages'%(authors, subject['hits']))
  100. content_box += box
  101. self += content_box
  102. self += CTK.Box({'class': 'bar3-bottom-link'}, CTK.RawHTML('<a href="http://lists.octality.com/listinfo/cherokee" target="_blank">Subscribe to Cherokee Mailing List &raquo;</a>'))
  103. #
  104. # Factory and cache
  105. #
  106. latest_widget = None
  107. latest_widget_expiration = None
  108. def Latest_Mailing_List():
  109. global latest_widget
  110. global latest_widget_expiration
  111. if not latest_widget or time.time() > latest_widget_expiration:
  112. latest_widget = Latest_Mailing_List_Widget()
  113. latest_widget_expiration = time.time() + CACHE_EXPIRATION
  114. return latest_widget