Skip to content

Commit

Permalink
refactor(internal/rss): 简化 HTTP 请求头设置并移除未使用代码
Browse files Browse the repository at this point in the history
- 移除了随机用户代理函数和特定网站的请求头设置
- 添加了通用的浏览器标识和必要的头信息
- 删除了未使用的导入和初始化代码
- 简化了版本号
  • Loading branch information
3377 committed Jan 10, 2025
1 parent 7c20d1b commit bcc0bd0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 38 deletions.
43 changes: 6 additions & 37 deletions internal/rss/rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,15 @@ package rss

import (
"log"
"math/rand"
"net/http"
"strings"
"sync"
"time"
"fmt"

"github.com/mmcdole/gofeed"
"rss2telegram/internal/storage"
)

var userAgents = []string{
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Edge/120.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Safari/605.1.15",
}

func getRandomUserAgent() string {
return userAgents[rand.Intn(len(userAgents))]
}

type MessageHandler func(title, url, group string, pubDate time.Time, matchedKeywords []string) error

type Manager struct {
Expand Down Expand Up @@ -126,25 +113,12 @@ func (m *Manager) checkFeed(feed *Feed) {
return
}

// 根据不同的域名使用不同的请求头
if strings.Contains(feed.URL, "hostloc.com") {
// hostloc 特定的请求头
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
req.Header.Set("Accept", "application/rss+xml,application/xml;q=0.9")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
req.Header.Set("Referer", "https://hostloc.com/forum.php")
} else if strings.Contains(feed.URL, "nodeseek.com") {
// nodeseek 特定的请求头
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
req.Header.Set("Accept", "application/xml,application/rss+xml;q=0.9")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
req.Header.Set("Referer", "https://nodeseek.com/")
} else {
// 其他网站使用通用请求头
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
req.Header.Set("Accept", "application/rss+xml,application/xml;q=0.9,*/*;q=0.8")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
}
// 添加浏览器标识和其他必要的头信息
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Upgrade-Insecure-Requests", "1")

// 使用自定义客户端解析 Feed
fp.Client = client
Expand Down Expand Up @@ -187,8 +161,3 @@ func (m *Manager) matchKeywords(item *gofeed.Item, keywords []string) []string {

return matched
}

func init() {
// 初始化随机数生成器
rand.Seed(time.Now().UnixNano())
}
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
3.0-25.01.10.2
3.0-25.01.10

0 comments on commit bcc0bd0

Please sign in to comment.