1 Star 0 Fork 0

linngc / central-mirror

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
python_opt.go 2.93 KB
一键复制 编辑 原始数据 按行查看 历史
linngc 提交于 2024-03-08 17:17 . add:格式化注释
// Package python
// @Link https://gitee.com/linngc/central-mirror
// @Copyright Copyright (c) 2024 central-mirror CLI
// @Author linngc
// @License
package python
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
"path"
"regexp"
)
var (
aTag = `<a[^>]*>(.*)</a>`
//超链接值
reTag = `<a[^>]+href="([^"]*)".*>`
headerTag = `"python":"([^"]*)"`
)
// getPythonVersion 根据请求头部获取python版本
func (c *PythonProxy) getPythonVersion(header string) string {
// 使用正则表达式匹配"python"的值
re := regexp.MustCompile(headerTag)
match := re.FindStringSubmatch(header)
if len(match) > 1 {
return match[1]
} else {
return ""
}
}
// compare python版本对比方法
func (c *PythonProxy) compare(symbol, version string) bool {
start := garray.NewStrArrayFrom(gstr.Split(symbol[2:], ".")).Pad(3, "0").Join("")
end := garray.NewStrArrayFrom(gstr.Split(version, ".")).Pad(3, "0").Join("")
s := gconv.Float32(start)
e := gconv.Float32(end)
if s >= e {
return true
}
return false
}
// replaceHref2Local 替换原始a标签数据为缓存相对路径
func (c *PythonProxy) replaceHref2Local(uri, div string) string {
urlsMap := c.getHrefLocal(uri, div)
for href, local := range urlsMap {
div = gstr.ReplaceI(div, href, fmt.Sprintf("%s%s", "../../packages/", local))
}
return div
}
// replaceHref2Local 替换原始a标签数据全路径
func (c *PythonProxy) replaceHref2Mirror(uri, html string) string {
aMatch := regexp.MustCompile(reTag).FindAllStringSubmatch(html, -1)
for _, a := range aMatch {
reMatch := regexp.MustCompile(reTag).FindStringSubmatch(gconv.String(a[0]))
if nil == reMatch || len(reMatch) < 2 {
continue
}
href := gconv.String(reMatch[1])
html = gstr.ReplaceI(html, href, fmt.Sprintf("%s/%s", uri, href))
}
return html
}
// getHrefMirrors 获取当前请求的href路径
// @param uri http://pypi.tuna.tsinghua.edu.cn/simple/django/
// @param div 当前html文本
// @return key 原始a标签数据,value 缓存数据的相对路径
func (c *PythonProxy) getHrefLocal(uri, div string) map[string]string {
artifactId := gstr.SubStr(uri, gstr.PosR(uri, "/")+1)
//key为url全路径,value为存储的相对路径
urlsMap := make(map[string]string)
//获取a标签数据
aMatch := regexp.MustCompile(reTag).FindAllStringSubmatch(div, -1)
for _, a := range aMatch {
reMatch := regexp.MustCompile(reTag).FindStringSubmatch(gconv.String(a[0]))
if nil == reMatch || len(reMatch) < 2 {
continue
}
href := gconv.String(reMatch[1])
urlsMap[href] = path.Join(artifactId, c.getArtifactIdPathName(href))
}
return urlsMap
}
// getArtifactIdPathName 根据url请求获取文件名称
func (c *PythonProxy) getArtifactIdPathName(href string) string {
beginI := gstr.PosR(href, "/")
endI := gstr.PosR(href, "#")
if beginI == -1 || endI == -1 {
return ""
}
return gstr.SubStr(href, beginI+1, endI-beginI-1)
}
Go
1
https://gitee.com/linngc/central-mirror.git
git@gitee.com:linngc/central-mirror.git
linngc
central-mirror
central-mirror
bf43de0cf88e

搜索帮助

53164aa7 5694891 3bd8fe86 5694891