feeds.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. from django.conf import settings
  2. from django.contrib.postgres.search import SearchQuery
  3. from django.contrib.syndication.views import Feed
  4. from django.urls import reverse
  5. from django.utils.feedgenerator import Rss201rev2Feed
  6. from . import helpers
  7. from .models import Media
  8. from .stop_words import STOP_WORDS
  9. class MediaRSSFeed(Rss201rev2Feed):
  10. def rss_attributes(self):
  11. attrs = super(MediaRSSFeed, self).rss_attributes()
  12. attrs["xmlns:media"] = "http://search.yahoo.com/mrss/"
  13. attrs["xmlns:atom"] = "http://www.w3.org/2005/Atom"
  14. return attrs
  15. def add_item_elements(self, handler, item):
  16. """Callback to add elements to each item (item/entry) element."""
  17. super(MediaRSSFeed, self).add_item_elements(handler, item)
  18. if "media:title" in item:
  19. handler.addQuickElement("media:title", item["title"])
  20. if "media:description" in item:
  21. handler.addQuickElement("media:description", item["description"])
  22. if "content_url" in item:
  23. content = dict(url=item["content_url"])
  24. if "content_width" in item:
  25. content["width"] = str(item["content_width"])
  26. if "content_height" in item:
  27. content["height"] = str(item["content_height"])
  28. handler.addQuickElement("media:content", "", content)
  29. if "thumbnail_url" in item:
  30. thumbnail = dict(url=item["thumbnail_url"])
  31. if "thumbnail_width" in item:
  32. thumbnail["width"] = str(item["thumbnail_width"])
  33. if "thumbnail_height" in item:
  34. thumbnail["height"] = str(item["thumbnail_height"])
  35. handler.addQuickElement("media:thumbnail", "", thumbnail)
  36. if "keywords" in item:
  37. handler.addQuickElement("media:keywords", item["keywords"])
  38. def add_root_elements(self, handler):
  39. super().add_root_elements(handler)
  40. if self.feed["author_name"] is not None:
  41. handler.startElement("author", {})
  42. handler.addQuickElement("name", self.feed["author_name"])
  43. handler.endElement("author")
  44. if self.feed.get("published") is not None:
  45. handler.startElement("published", {})
  46. handler.addQuickElement("name", self.feed["published"])
  47. handler.endElement("published")
  48. class IndexRSSFeed(Feed):
  49. feed_type = MediaRSSFeed
  50. title = "Latest Media"
  51. link = "/rss"
  52. description = "Latest Media RSS feed"
  53. def items(self):
  54. media = Media.objects.filter(listable=True).order_by("-add_date")
  55. media = media.prefetch_related("user")
  56. return media[:20]
  57. def item_title(self, item):
  58. return item.title
  59. def item_description(self, item):
  60. return item.description
  61. def item_author_name(self, item):
  62. return item.user.username
  63. def item_pubdate(self, item):
  64. return item.add_date
  65. def item_updateddate(self, item):
  66. return item.edit_date
  67. def item_link(self, item):
  68. return reverse("get_media") + "?m={0}".format(item.friendly_token)
  69. def item_extra_kwargs(self, item):
  70. item = {
  71. "media:title": item.title,
  72. "media:description": item.description,
  73. "content_width": 720,
  74. "thumbnail_url": f"{settings.SSL_FRONTEND_HOST}/{item.poster_url}",
  75. "content_url": f"{settings.SSL_FRONTEND_HOST}/{item.get_absolute_url()}",
  76. "thumbnail_width": 720,
  77. }
  78. return item
  79. class SearchRSSFeed(Feed):
  80. feed_type = MediaRSSFeed
  81. description = "Latest Media RSS feed"
  82. def link(self, obj):
  83. return "/rss/search"
  84. def get_object(self, request):
  85. category = request.GET.get("c", "")
  86. tag = request.GET.get("t", "")
  87. query = request.GET.get("q", "")
  88. media = Media.objects.filter(listable=True)
  89. if category:
  90. media = media.filter(category__title=category)
  91. elif tag:
  92. media = media.filter(tags__title=tag)
  93. elif query:
  94. # same as on files.views.MediaSearch: move this processing to a prepare_query function
  95. query = helpers.clean_query(query)
  96. q_parts = [q_part.rstrip("y") for q_part in query.split() if q_part not in STOP_WORDS]
  97. if q_parts:
  98. query = SearchQuery(q_parts[0] + ":*", search_type="raw")
  99. for part in q_parts[1:]:
  100. query &= SearchQuery(part + ":*", search_type="raw")
  101. else:
  102. query = None
  103. if query:
  104. media = media.filter(search=query)
  105. media = media.order_by("-add_date").prefetch_related("user")
  106. return media
  107. def items(self, objects):
  108. return objects[:20]
  109. def item_title(self, item):
  110. return item.title
  111. def item_description(self, item):
  112. return item.description
  113. def item_author_name(self, item):
  114. return item.user.username
  115. def item_pubdate(self, item):
  116. return item.add_date
  117. def item_updateddate(self, item):
  118. return item.edit_date
  119. def item_link(self, item):
  120. return reverse("get_media") + "?m={0}".format(item.friendly_token)
  121. def item_extra_kwargs(self, item):
  122. item = {
  123. "media:title": item.title,
  124. "media:description": item.description,
  125. "content_width": 720,
  126. "thumbnail_url": f"{settings.SSL_FRONTEND_HOST}/{item.poster_url}",
  127. "content_url": f"{settings.SSL_FRONTEND_HOST}/{item.get_absolute_url()}",
  128. "thumbnail_width": 720,
  129. }
  130. return item