root
root
3168 2 0

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

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

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>`)
	}
0

See Also

Nearby


Discussion (2)

ego008
ego008 2018-02-13 12:28
musicRegexp = regexp.MustCompile(`https?://music\.163\.com/#/song\?id=([a-zA-Z0-9]+)`)
0
root
root 2018-02-13 12:41

@ego008 已解决!谢谢您~

0
Login Topics