本地化WordPress 的新浪微博图片外链
作者:matrix 发布时间:2024-09-30 分类:Golang 零零星星
😀 免费的才是最贵的。新浪微博的图床早就挂了,目前的图片会限制请求头 referer
。
今天空了才把这部分图片迁移到本地。记录下这个临时脚本。
脚本下载 WordPress 文章中的新浪图片到本地,然后数据库中的图片链接会执行替换。
配置好信息之后正式执行记得放开 #94行的TODO。 自行测试~
package main
import (
"database/sql"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
_ "github.com/go-sql-driver/mysql"
)
const (
//TODO
domain = "www.hhtjim.com" //WordPress 域名
db_user = "root" //db 信息
db_password = "root" //db 信息
db_host_port = "127.0.0.1:33060" //db 信息
db_name = "wordpress_db" //db 信息
savePathBase = "/home/www/htdoc/wordpress/wp-content/uploads/2024/sinaimg/" //图片下载保存位置
newBase = "https://" + domain + "/wp-content/uploads/2024/sinaimg/" // 替换后的图片 url path
referer = "https://m.weibo.cn"
dsn = db_user + ":" + db_password + "@tcp(" + db_host_port + ")/" + db_name
query = "SELECT ID, post_content,post_content_filtered FROM wp_posts WHERE post_type = 'post' AND post_content LIKE '%sinaimg.cn%' ORDER BY ID DESC;"
)
var (
re = regexp.MustCompile(`//([a-zA-Z\d]+.sinaimg.cn)/([\w]+/[\w_-]+.(?:jpg|png|gif))`)
)
func main() {
// 连接数据库
db, err := sql.Open("mysql", dsn)
if err != nil {
fmt.Println("Failed to connect to database:", err)
return
}
defer db.Close()
rows, err := db.Query(query)
if err != nil {
fmt.Println("Failed to query database:", err)
return
}
defer rows.Close()
for rows.Next() {
var id int
var postContent string
var post_content_filtered string
if err := rows.Scan(&id, &postContent, &post_content_filtered); err != nil {
fmt.Println("Failed to scan row:", err)
continue
}
updatedContent := re.ReplaceAllStringFunc(postContent, func(match string) string {
matches := re.FindStringSubmatch(match)
if len(matches) != 3 {
return match
}
imgURL := "https:" + match
savePath := matches[2]
if _, err := os.Stat(savePath); os.IsNotExist(err) {
if err := downloadImage(imgURL, savePath); err != nil {
fmt.Println("Failed to download image:", err)
return match
}
}
return newBase + savePath
})
updatedContentFiltered := re.ReplaceAllStringFunc(post_content_filtered, func(match string) string {
matches := re.FindStringSubmatch(match)
if len(matches) != 3 {
return match
}
imgURL := "https:" + match
savePath := matches[2]
if _, err := os.Stat(savePath); os.IsNotExist(err) {
if err := downloadImage(imgURL, savePathBase+savePath); err != nil {
fmt.Println("Failed to download image:", err)
return match
}
}
return newBase + savePath
})
fmt.Println(id, updatedContent, updatedContentFiltered)
// fmt.Printf("https://%s?p=%d\n", domain, id)
continue //TODO
// break
_, err := db.Exec("UPDATE wp_posts SET post_content = ?,post_content_filtered=? WHERE ID = ?", updatedContent, updatedContentFiltered, id)
if err != nil {
fmt.Println("Failed to update database:", err)
continue
}
}
}
func downloadImage(imgURL, savePath string) error {
resp, err := http.Get(imgURL)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download image: %s", resp.Status)
}
dir := filepath.Dir(savePath)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
file, err := os.Create(savePath)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, resp.Body)
return err
}