root avatar

请教一下网易云音乐的问题

🕧 by root

@ego008 我想添加网易云音乐,但是这样好像不成功?能否给点指导~谢谢~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var (
	codeRegexp    = regexp.MustCompile("(?s:` ``(.+?)` ``)")
	imgRegexp     = regexp.MustCompile(`(https?://[\w./:]+/[\w./]+\.(jpg|jpe|jpeg|gif|png))`)
	gistRegexp    = regexp.MustCompile(`(https?://gist\.github\.com/([a-zA-Z0-9-]+/)?[\d]+)`)
	mentionRegexp = regexp.MustCompile(`\B@([a-zA-Z0-9\p{Han}]{1,32})\s?`)
	urlRegexp     = regexp.MustCompile(`([^;"='>])(https?://[^\s<]+[^\s<.)])`)
	nlineRegexp   = regexp.MustCompile(`\s{2,}`)
	youku1Regexp  = regexp.MustCompile(`https?://player\.youku\.com/player\.php/sid/([a-zA-Z0-9=]+)/v\.swf`)
	youku2Regexp  = regexp.MustCompile(`https?://v\.youku\.com/v_show/id_([a-zA-Z0-9=]+)(/|\.html?)?`)
	musicRegexp   = regexp.MustCompile(`https?://music\.163\.com/#/song?id=/([a-zA-Z0-9=]+)`)
)

// 文本格式化
func ContentFmt(db *youdb.DB, input string) string {
	if strings.Index(input, "` ``") >= 0 {
		sepNum := strings.Count(input, "` ``")
		if sepNum < 2 {
			return input
		}
		codeMap := map[string]string{}
		input = codeRegexp.ReplaceAllStringFunc(input, func(m string) string {
			m = strings.Trim(m, "` ``")
			m = strings.Trim(m, "\n")
			m = strings.TrimSpace(m)
			m = strings.Replace(m, "<", "<", -1)
			m = strings.Replace(m, ">", ">", -1)

			codeTag := "[mspctag_" + strconv.FormatInt(int64(len(codeMap)+1), 10) + "]"
			codeMap[codeTag] = "<pre><code>" + m + "</code></pre>"
			return codeTag
		})

		input = ContentRich(db, input)
		// replace tmp code tag
		if len(codeMap) > 0 {
			for k, v := range codeMap {
				input = strings.Replace(input, k, v, -1)
			}
		}
		//
		input = strings.Replace(input, "<p><pre>", "<pre>", -1)
		input = strings.Replace(input, "</pre></p>", "</pre>", -1)
		return input
	}
	return ContentRich(db, input)
}

type urlInfo struct {
	Href  string
	Click string
}

func ContentRich(db *youdb.DB, input string) string {
	input = strings.TrimSpace(input)
	input = " " + input // fix Has url Prefix
	input = strings.Replace(input, "<", "<", -1)
	input = strings.Replace(input, ">", ">", -1)
	input = imgRegexp.ReplaceAllString(input, `<img src="$1" />`)

	// video
	// youku
	if strings.Index(input, "player.youku.com") >= 0 {
		input = youku1Regexp.ReplaceAllString(input, `<embed src="https://player.youku.com/player.php/sid/$1/v.swf" quality="high" width="590" height="492" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"></embed>`)
	}
	if strings.Index(input, "v.youku.com") >= 0 {
		input = youku2Regexp.ReplaceAllString(input, `<embed src="https://player.youku.com/player.php/sid/$1/v.swf" quality="high" width="590" height="492" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"></embed>`)
	}

	if strings.Index(input, "music.163.com") >= 0 {
		input = musicRegexp.ReplaceAllString(input, `<embed src="https://music.163.com/style/swf/widget.swf?sid=$1&type=2&auto=1&width=320&height=66" width="340" height="86"  allowNetworking="all"></embed>`)
	}

💘 相关文章

评论

共2条关于"请教一下网易云音乐的问题"的评论

ego008 avatar
#1 ego008 回复

1
musicRegexp = regexp.MustCompile(`https?://music\.163\.com/#/song\?id=([a-zA-Z0-9]+)`)

写一条评论

Based on Golang + fastHTTP + sdb | go1.16.5 Processed in 1ms