index.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
  2. import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
  3. const corsHeaders = {
  4. 'Access-Control-Allow-Origin': '*',
  5. 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
  6. }
  7. serve(async (req) => {
  8. if (req.method === 'OPTIONS') {
  9. return new Response(null, { headers: corsHeaders })
  10. }
  11. try {
  12. const { feedId, url } = await req.json()
  13. if (!feedId || !url) {
  14. return new Response(
  15. JSON.stringify({ error: 'Feed ID and URL are required' }),
  16. { status: 400, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
  17. )
  18. }
  19. // Initialize Supabase client
  20. const supabase = createClient(
  21. Deno.env.get('SUPABASE_URL') ?? '',
  22. Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
  23. )
  24. console.log(`Fetching RSS feed from: ${url}`)
  25. // Fetch RSS feed
  26. const response = await fetch(url, {
  27. headers: {
  28. 'User-Agent': 'Mozilla/5.0 (compatible; RSS Feed Updater/1.0)'
  29. }
  30. })
  31. if (!response.ok) {
  32. throw new Error(`HTTP error! status: ${response.status}`)
  33. }
  34. const rssText = await response.text()
  35. console.log('RSS content fetched successfully')
  36. // Parse RSS content to extract metadata
  37. const titleMatch = rssText.match(/<title[^>]*>(.*?)<\/title>/i)
  38. const descriptionMatch = rssText.match(/<description[^>]*>(.*?)<\/description>/i) ||
  39. rssText.match(/<subtitle[^>]*>(.*?)<\/subtitle>/i)
  40. // Count items in the feed
  41. const itemMatches = rssText.match(/<item[^>]*>/gi) || rssText.match(/<entry[^>]*>/gi) || []
  42. const articleCount = itemMatches.length
  43. // Clean up extracted text (remove HTML tags and decode entities)
  44. const cleanText = (text: string) => {
  45. if (!text) return ''
  46. return text
  47. .replace(/<!\[CDATA\[(.*?)\]\]>/g, '$1')
  48. .replace(/<[^>]+>/g, '')
  49. .replace(/&lt;/g, '<')
  50. .replace(/&gt;/g, '>')
  51. .replace(/&amp;/g, '&')
  52. .replace(/&quot;/g, '"')
  53. .replace(/&#39;/g, "'")
  54. .trim()
  55. }
  56. const extractedTitle = titleMatch ? cleanText(titleMatch[1]) : null
  57. const extractedDescription = descriptionMatch ? cleanText(descriptionMatch[1]) : null
  58. console.log(`Extracted data: title="${extractedTitle}", description="${extractedDescription}", articles=${articleCount}`)
  59. // Update feed in database
  60. const updateData: any = {
  61. last_updated: new Date().toISOString(),
  62. article_count: articleCount,
  63. status: 'active'
  64. }
  65. // Only update title and description if we found them and they're different
  66. if (extractedTitle) {
  67. updateData.name = extractedTitle
  68. }
  69. if (extractedDescription) {
  70. updateData.description = extractedDescription
  71. }
  72. const { data, error } = await supabase
  73. .from('feeds')
  74. .update(updateData)
  75. .eq('id', feedId)
  76. .select()
  77. if (error) {
  78. console.error('Database update error:', error)
  79. return new Response(
  80. JSON.stringify({ error: 'Failed to update feed in database' }),
  81. { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
  82. )
  83. }
  84. console.log('Feed updated successfully')
  85. return new Response(
  86. JSON.stringify({
  87. success: true,
  88. data: data[0],
  89. extracted: {
  90. title: extractedTitle,
  91. description: extractedDescription,
  92. articleCount
  93. }
  94. }),
  95. { headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
  96. )
  97. } catch (error: any) {
  98. console.error('Error updating feed:', error)
  99. return new Response(
  100. JSON.stringify({ error: error?.message || 'Unknown error' }),
  101. { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
  102. )
  103. }
  104. })