123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702 |
- package youngee_talent_service
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "github.com/gogf/gf/os/gtime"
- "io/ioutil"
- "net/http"
- "net/url"
- "regexp"
- "strconv"
- "youngmini_server/app/dao"
- "youngmini_server/app/model/youngee_talent_model"
- "youngmini_server/app/utils"
- "github.com/gogf/gf/frame/g"
- "github.com/gogf/gf/net/ghttp"
- "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
- ocr "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1"
- "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1/model"
- region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1/region"
- "strings"
- )
- func GetTalentAccount(r *ghttp.Request) *TalentHttpResult {
- //达人id获取
- tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
- //定义除了youngee_platform_account_info表结构外额外还有info_third_platform的结构体(用表连接)
- if err != nil {
- return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
- }
- //查找给定达人id之后,该达人绑定了几个平台的账号
- res, err := g.DB().Model("youngee_platform_account_info").All("talent_id", tid)
- fmt.Println("该账号社媒平台绑定结果为__________:", res)
- if err != nil {
- return &TalentHttpResult{Code: -2, Msg: "query database error"}
- }
- if res == nil {
- return &TalentHttpResult{Code: -3, Msg: "未绑定任何平台"}
- }
- return &TalentHttpResult{Code: 0, Msg: "success", Data: res}
- }
- func GetTalentAccountKuaishou(r *ghttp.Request) *TalentHttpResult {
- //达人id获取
- tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
- if err != nil {
- return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
- }
- //查找给定达人id之后,该达人绑定了几个平台的账号
- res, err := g.DB().Model("platform_kuaishou_user_info").All("talent_id", tid)
- if err != nil {
- return &TalentHttpResult{Code: -2, Msg: "query database error"}
- }
- if res == nil {
- return &TalentHttpResult{Code: -3, Msg: "未绑定任何平台"}
- }
- return &TalentHttpResult{Code: 0, Msg: "success", Data: res}
- }
- func DelTalentAccount(r *ghttp.Request) *TalentHttpResult {
- tid, _ := utils.SessionTalentInfo.GetTalentIdFromSession(r)
- var DelAccountReq *youngee_talent_model.DelAccountReq
- err := r.ParseForm(&DelAccountReq)
- if err != nil {
- return &TalentHttpResult{Code: -1, Msg: "Parse form failed"}
- }
- if DelAccountReq.PlatformId == 4 || DelAccountReq.PlatformId == 8 { //删除快手
- _, err := g.Model("platform_kuaishou_user_info").Where("talent_id=? AND platform_id IN (4,8) And open_id=?", tid, DelAccountReq.OpenId).Delete()
- if err != nil {
- return &TalentHttpResult{Code: -1, Msg: "delete account failed"}
- }
- } else {
- _, err := g.Model("platform_kuaishou_user_info").Where("talent_id=? AND platform_id =? And open_id=?", tid, DelAccountReq.PlatformId, DelAccountReq.OpenId).Delete()
- if err != nil {
- return &TalentHttpResult{Code: -1, Msg: "delete account failed"}
- }
- }
- return &TalentHttpResult{Code: 0, Msg: "success"}
- }
- func OnAddTalentAccount(r *ghttp.Request) *TalentHttpResult {
- tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
- if err != nil {
- return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
- }
- account := youngee_talent_model.AddAccountReq{}
- //account := youngee_talent_model.KuaishouUserInfo{}
- err = r.ParseForm(&account)
- if err != nil {
- return &TalentHttpResult{Code: -2, Msg: err.Error()}
- }
- //获取 基本信息,头像昵称、粉丝数、作品数目、点赞数
- //小红书
- if account.PlatformId == 1 {
- //获取用户唯一标识
- shareText := account.HomePageUrl
- //调用MoreAPI
- userDetail := getXHSUserDetail(shareText)
- if userDetail == nil {
- return &TalentHttpResult{Code: -1, Msg: "MoreApi调用出错"}
- }
- //是否被其他人绑定
- BindCode := CheckAccountBind(account.PlatformId, userDetail.RedID)
- if BindCode == -1 {
- return &TalentHttpResult{Code: -1, Msg: "查询账号表出错"}
- } else if BindCode == 1 {
- return &TalentHttpResult{Code: -5, Msg: "此小红书Id已经被绑定"}
- } else { //没有被其他人绑定
- //调用OCR
- var orcInfo = getXHSOCRresult(account.HomePageCaptureUrl, account.PlatformId)
- if orcInfo == nil {
- return &TalentHttpResult{Code: -1, Msg: "ORC接口出错"}
- }
- //小红书id一致且是自己的主页
- if orcInfo.RedId == userDetail.RedID && orcInfo.SelfHome == 1 {
- //获取用户基本信息
- XHSdata := g.Map{
- "talent_id": tid,
- "open_id": userDetail.RedID,
- "platform_id": account.PlatformId,
- "fan": userDetail.Fans,
- "nick_name": userDetail.Nickname,
- "head_uri": userDetail.Images,
- "city": userDetail.IPLocation,
- "gender": userDetail.Gender,
- "like_num": userDetail.LikeNum,
- "create_time": gtime.Now(),
- "update_time": gtime.Now(),
- "home_page_url": account.HomePageUrl,
- "home_page_capture_url": account.HomePageCaptureUrl,
- }
- //入库
- _, err := g.Model("platform_kuaishou_user_info").Insert(XHSdata)
- if err != nil {
- return &TalentHttpResult{Code: -1, Msg: "数据入库失败"}
- }
- // 修改talent表字段is_bind_account值为1
- _, err = g.Model(dao.YoungeeTalentInfo.Table).Data("is_bind_account=1").Where("id", tid).Update()
- if err != nil {
- return &TalentHttpResult{Code: -2, Msg: err.Error()}
- }
- return &TalentHttpResult{Code: 0, Msg: "小红书账号绑定成功"}
- } else {
- return &TalentHttpResult{Code: -6, Msg: "图片中小红书ID和主页链接不一致"}
- }
- }
- } else if account.PlatformId == 3 { //微博
- //获取用户唯一标识
- shareText := account.HomePageUrl
- //调用MoreAPI
- userDetail := getWBUserDetail(shareText)
- if userDetail == nil {
- return &TalentHttpResult{Code: -1, Msg: "MoreApi调用出错"}
- }
- //是否被其他人绑定
- BindCode := CheckAccountBind(account.PlatformId, userDetail.WBID)
- if BindCode == -1 {
- return &TalentHttpResult{Code: -1, Msg: "查询账号表出错"}
- } else if BindCode == 1 {
- return &TalentHttpResult{Code: -5, Msg: "此微博Id已经被绑定"}
- } else {
- //调用OCR
- var orcInfo = getWBOCRresult(account.HomePageCaptureUrl, userDetail.Nickname)
- if orcInfo == nil {
- return &TalentHttpResult{Code: -1, Msg: "ORC接口出错"}
- }
- //orc的昵称和moreapi返回的昵称一致,且主页截图是自己的截图
- if orcInfo.SelfHome == 1 {
- //获取用户基本信息
- WBdata := g.Map{
- "talent_id": tid,
- "open_id": userDetail.WBID,
- "platform_id": account.PlatformId,
- "fan": userDetail.Fans,
- "nick_name": userDetail.Nickname,
- "head_uri": userDetail.HeadImage,
- "gender": userDetail.Gender,
- "create_time": gtime.Now(),
- "update_time": gtime.Now(),
- "home_page_url": account.HomePageUrl,
- "home_page_capture_url": account.HomePageCaptureUrl,
- }
- //入库
- _, err := g.Model("platform_kuaishou_user_info").Insert(WBdata)
- if err != nil {
- return &TalentHttpResult{Code: -1, Msg: "数据入库失败"}
- }
- // 修改talent表字段is_bind_account值为1
- _, err = g.Model(dao.YoungeeTalentInfo.Table).Data("is_bind_account=1").Where("id", tid).Update()
- if err != nil {
- return &TalentHttpResult{Code: -2, Msg: err.Error()}
- }
- return &TalentHttpResult{Code: 0, Msg: "微博账号绑定成功"}
- } else {
- return &TalentHttpResult{Code: -6, Msg: "主页截图非本人主页,获取着与主页链接不匹配"}
- }
- }
- } else if account.PlatformId == 5 { //B站
- //获取用户主页URL以及uid
- shareText := account.HomePageUrl
- //调用MoreAPI
- userDetail := getBiLiUserDetail(shareText)
- if userDetail == nil {
- return &TalentHttpResult{Code: -1, Msg: "MoreApi调用出错"}
- }
- //是否被其他人绑定
- BindCode := CheckAccountBind(account.PlatformId, userDetail.UID)
- if BindCode == -1 {
- return &TalentHttpResult{Code: -1, Msg: "查询账号表出错"}
- } else if BindCode == 1 {
- return &TalentHttpResult{Code: -5, Msg: "此bilibili已经被绑定"}
- } else {
- //调用OCR
- var orcInfo = getBliOCRresult(account.HomePageCaptureUrl, userDetail.Nickname)
- if orcInfo == nil {
- return &TalentHttpResult{Code: -1, Msg: "ORC接口出错"}
- }
- //orc的昵称和moreapi返回的昵称一致,且主页截图是自己的截图
- if orcInfo.SelfHome == 1 {
- //获取用户基本信息
- Blidata := g.Map{
- "talent_id": tid,
- "open_id": userDetail.UID,
- "platform_id": account.PlatformId,
- "fan": userDetail.Fans,
- "nick_name": userDetail.Nickname,
- "head_uri": userDetail.HeadImage,
- "gender": userDetail.Gender,
- "create_time": gtime.Now(),
- "update_time": gtime.Now(),
- "home_page_url": account.HomePageUrl,
- "home_page_capture_url": account.HomePageCaptureUrl,
- }
- //入库
- _, err := g.Model("platform_kuaishou_user_info").Insert(Blidata)
- if err != nil {
- return &TalentHttpResult{Code: -1, Msg: "数据入库失败"}
- }
- // 修改talent表字段is_bind_account值为1
- _, err = g.Model(dao.YoungeeTalentInfo.Table).Data("is_bind_account=1").Where("id", tid).Update()
- if err != nil {
- return &TalentHttpResult{Code: -2, Msg: err.Error()}
- }
- return &TalentHttpResult{Code: 0, Msg: "bli账号绑定成功"}
- } else {
- return &TalentHttpResult{Code: -6, Msg: "主页截图非本人主页,或者与主页链接不匹配"}
- }
- }
- } else {
- return &TalentHttpResult{Code: -1, Msg: "平台ID有误"}
- }
- return &TalentHttpResult{Code: 0, Msg: "success"}
- }
- //func OnUpdateTalentAccount(r *ghttp.Request) *TalentHttpResult {
- // tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
- // if err != nil {
- // return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
- // }
- // account := youngee_talent_model.PlatformAccountInfo{}
- // err = r.ParseForm(&account)
- // if err != nil {
- // return &TalentHttpResult{Code: -2, Msg: err.Error()}
- // }
- // account.TalentId = tid
- // _, err = g.DB().Model(dao.YoungeePlatformAccountInfo.Table).Update(account, "account_id = ? and talent_id = ?", account.PlatformId, tid)
- // if err != nil {
- // return &TalentHttpResult{Code: -3, Msg: "update failed"}
- // }
- // return &TalentHttpResult{Code: 0, Msg: "success"}
- //}
- // 检查该账号是否被其他人绑定了
- func CheckAccountBind(pId int, openId string) int {
- count, err := g.Model("platform_kuaishou_user_info").Where("platform_id = ? and open_id = ?", pId, openId).Count()
- if err != nil {
- return -1
- }
- if count > 0 { //已经被绑定
- return 1
- } else {
- return 0
- }
- }
- // 用于获取 XHS 用户详细信息的函数
- func getXHSUserDetail(homeURL string) *youngee_talent_model.XHSBasicInfo {
- cookie := "acw_tc=0a00d7db17326882742197649ed1bf15848a5ff06232f5c37b7eaa2663f722; abRequestId=028b7a52-6c20-5315-aaf5-3bf365d26f20; webBuild=4.35.0; xsecappid=xhs-pc-web; a1=1936c44e2c4IjfanuIengZ3lEpOKmXeKdOan0rcbl50000544495; webId=021b26332dd3b74978a347eb885a1761; gid=yjJJWyiYfKMyyjJJWyiYYVIvqK2EfyMuuyMdE016fE2vSU286k88SE888yKJJq4800jJd8qq; websectiga=16f444b9ff5e3d7e258b5f7674489196303a0b160e16647c6c2b4dcb609f4134; sec_poison_id=58886b56-6f01-4f86-9ea1-0a1febfde070; unread={\\\"ub\\\":\\\"66e2f3690000000012011cb0\\\",\\\"ue\\\":\\\"66e823b9000000001e018443\\\",\\\"uc\\\":14}; web_session=040069b41c29aed16ac492f173354bfc129950"
- GetDetailUrl := "http://120.46.92.62:6888/api/xhs/user_detail"
- // 构建请求体
- requestBody := map[string]string{"share_text": homeURL}
- jsonBody, err := json.Marshal(requestBody)
- if err != nil {
- fmt.Println("Error marshalling request body:", err)
- return nil
- }
- // 创建 POST 请求
- req, err := http.NewRequest("POST", GetDetailUrl, bytes.NewBuffer(jsonBody))
- if err != nil {
- fmt.Println("Error creating POST request:", err)
- return nil
- }
- // 设置请求头,包括 cookie
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("cookie", cookie)
- // 发送请求
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- fmt.Println("Error sending POST request:", err)
- return nil
- }
- defer resp.Body.Close()
- // 读取响应体
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println("Error reading response body:", err)
- return nil
- }
- // 创建一个空的 map 用于存放解析后的 JSON 数据
- var responseMap map[string]interface{}
- err = json.Unmarshal(body, &responseMap)
- if err != nil {
- fmt.Println("Error parsing JSON:", err)
- return nil
- }
- // 提取 data.response_body.data.basic_info
- if responseBody, ok := responseMap["data"].(map[string]interface{}); ok {
- if responseBodyData, ok := responseBody["response_body"].(map[string]interface{}); ok {
- if data, ok := responseBodyData["data"].(map[string]interface{}); ok {
- if basicInfo, ok := data["basic_info"].(map[string]interface{}); ok {
- // 使用 json.Unmarshal 将 basic_info 直接解析到结构体中
- var userInfo youngee_talent_model.XHSBasicInfo
- userInfo.Imageb = fmt.Sprintf("%v", basicInfo["imageb"])
- userInfo.Nickname = fmt.Sprintf("%v", basicInfo["nickname"])
- userInfo.Images = fmt.Sprintf("%v", basicInfo["images"])
- userInfo.RedID = fmt.Sprintf("%v", basicInfo["red_id"])
- userInfo.Gender = fmt.Sprintf("%v", basicInfo["gender"])
- userInfo.IPLocation = fmt.Sprintf("%v", basicInfo["ip_location"])
- userInfo.Desc = fmt.Sprintf("%v", basicInfo["desc"])
- // 提取 interactions
- if interactions, ok := data["interactions"].([]interface{}); ok {
- for _, interaction := range interactions {
- interactionData := interaction.(map[string]interface{})
- // 提取粉丝数(name 为 "粉丝")
- if interactionData["name"] == "粉丝" {
- countStr := fmt.Sprintf("%v", interactionData["count"])
- countInt, _ := strconv.Atoi(countStr)
- userInfo.Fans = countInt
- }
- // 提取获赞与收藏数(name 为 "获赞与收藏")
- if interactionData["name"] == "获赞与收藏" {
- LikeNum := fmt.Sprintf("%v", interactionData["count"])
- LikeNumInt, _ := strconv.Atoi(LikeNum)
- userInfo.LikeNum = LikeNumInt
- }
- }
- }
- // 返回 BasicInfo 结构体
- return &userInfo
- }
- }
- }
- }
- // 如果没有找到 basic_info,返回错误
- return nil
- }
- // 获取WB用户详细信息
- func getWBUserDetail(homeURL string) *youngee_talent_model.WBBasicInfo {
- GetDetailUrl := "http://120.46.92.62:6888/api/weibo/user_detail"
- // 构建请求体
- requestBody := map[string]string{"share_text": homeURL}
- jsonBody, err := json.Marshal(requestBody)
- if err != nil {
- fmt.Println("Error marshalling request body:", err)
- return nil
- }
- // 创建 POST 请求
- req, err := http.NewRequest("POST", GetDetailUrl, bytes.NewBuffer(jsonBody))
- if err != nil {
- fmt.Println("Error creating POST request:", err)
- return nil
- }
- // 设置请求头,包括 cookie
- req.Header.Set("Content-Type", "application/json")
- // 发送请求
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- fmt.Println("Error sending POST request:", err)
- return nil
- }
- defer resp.Body.Close()
- // 读取响应体
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println("Error reading response body:", err)
- return nil
- }
- // 创建一个空的 map 用于存放解析后的 JSON 数据
- var responseMap map[string]interface{}
- err = json.Unmarshal(body, &responseMap)
- if err != nil {
- fmt.Println("Error parsing JSON:", err)
- return nil
- }
- //提取数据
- // 提取数据
- if responseBody, ok := responseMap["data"].(map[string]interface{}); ok {
- // 提取所需字段 responseBody["id"]
- openId := fmt.Sprintf("%.0f", responseBody["id"])
- fmt.Println("openId----->:", openId)
- followers, _ := utils.TypeTran.AnyToInt(responseBody["followers_count"]) // 粉丝
- avatarHd, _ := utils.TypeTran.AnyToString(responseBody["avatar_hd"]) //头像
- nickName, _ := utils.TypeTran.AnyToString(responseBody["screen_name"]) //昵称
- fmt.Println("nickName----->:", nickName)
- statusesCount, _ := utils.TypeTran.AnyToInt(responseBody["statuses_count"]) // 作品数目
- gender, _ := utils.TypeTran.AnyToString(responseBody["gender"])
- // 创建并返回 BasicInfo 结构体
- userInfo := youngee_talent_model.WBBasicInfo{
- WBID: openId,
- HeadImage: avatarHd,
- Fans: followers,
- PostNum: statusesCount,
- Gender: gender,
- Nickname: nickName,
- }
- return &userInfo
- }
- // 如果没有找到 basic_info,返回错误
- return nil
- }
- func getBiLiUserDetail(homeURL string) *youngee_talent_model.BliBasicInfo {
- redirectedURL, err := getRedirectedURL(homeURL)
- if err != nil {
- fmt.Println("Error:", err)
- return nil
- }
- userID, err := extractUserID(redirectedURL)
- if err != nil {
- fmt.Println("Error extracting user ID:", err)
- return nil
- }
- fmt.Println("Extracted user ID:", userID)
- GetDetailUrl := "http://120.46.92.62:6888/api/bilibili/user_info"
- // 构建请求体
- requestBody := map[string]string{"uid": userID}
- jsonBody, err := json.Marshal(requestBody)
- if err != nil {
- fmt.Println("Error marshalling request body:", err)
- return nil
- }
- // 创建 POST 请求
- req, err := http.NewRequest("POST", GetDetailUrl, bytes.NewBuffer(jsonBody))
- if err != nil {
- fmt.Println("Error creating POST request:", err)
- return nil
- }
- // 设置请求头,包括 cookie
- req.Header.Set("Content-Type", "application/json")
- // 发送请求
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- fmt.Println("Error sending POST request:", err)
- return nil
- }
- defer resp.Body.Close()
- // 读取响应体
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println("Error reading response body:", err)
- return nil
- }
- // 创建一个空的 map 用于存放解析后的 JSON 数据
- var responseMap map[string]interface{}
- err = json.Unmarshal(body, &responseMap)
- if err != nil {
- fmt.Println("Error parsing JSON:", err)
- return nil
- }
- // 提取数据
- if responseBody, ok := responseMap["data"].(map[string]interface{}); ok {
- // 提取 space 信息
- spaceInfo := responseBody["space"].(map[string]interface{})
- // 提取 space.info 中的字段
- mid := fmt.Sprintf("%.0f", spaceInfo["info"].(map[string]interface{})["mid"])
- name, _ := utils.TypeTran.AnyToString(spaceInfo["info"].(map[string]interface{})["name"]) // name
- sex, _ := utils.TypeTran.AnyToString(spaceInfo["info"].(map[string]interface{})["sex"]) // sex
- face, _ := utils.TypeTran.AnyToString(spaceInfo["info"].(map[string]interface{})["face"]) // face
- // 提取 stat 信息
- stat := responseBody["stat"].(map[string]interface{})
- follower, _ := utils.TypeTran.AnyToInt(stat["follower"]) // follower
- // 创建并返回 BasicInfo 结构体
- userInfo := youngee_talent_model.BliBasicInfo{
- UID: mid,
- HeadImage: face,
- Fans: follower, // 转换为整数
- Gender: sex,
- Nickname: name,
- }
- return &userInfo
- }
- // 如果没有找到 basic_info,返回错误
- return nil
- }
- // *youngee_talent_model.OCRResponse
- func getXHSOCRresult(homePicURL string, platform_id int) *youngee_talent_model.OCRResponse {
- // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
- auth := basic.NewCredentialsBuilder().
- WithAk("YJRGTOWEITSYOXN3YPJY").
- WithSk("Kd7WDeS2CxlLXjMhGpLW0AmdOZHafitQpBy13a0s").
- Build()
- client := ocr.NewOcrClient(
- ocr.OcrClientBuilder().
- WithRegion(region.ValueOf("cn-north-4")).
- WithCredential(auth).
- Build())
- request := &model.RecognizeWebImageRequest{}
- request.Body = &model.WebImageRequestBody{
- Url: &homePicURL,
- }
- // 发送请求
- response, err := client.RecognizeWebImage(request)
- if err != nil {
- return nil
- }
- // 输出返回的完整响应
- fmt.Printf("%+v\n", response)
- resultText := response.String()
- fmt.Println("resultText--->:", resultText)
- var orcInfo youngee_talent_model.OCRResponse
- if strings.Contains(resultText, "编辑资料") &&
- strings.Contains(resultText, "购物车") &&
- strings.Contains(resultText, "查看我的订单") {
- orcInfo.SelfHome = 1
- } else {
- orcInfo.SelfHome = 0
- }
- //获取小红书ID
- re := regexp.MustCompile(`小红书号:([\w-]+)`) // 匹配字母、数字或破折号的组合
- match := re.FindStringSubmatch(resultText)
- if len(match) > 1 {
- xhsID := match[1] // match[1] 是第一个括号中的内容,即小红书号
- fmt.Println("xhsID-->:", xhsID)
- orcInfo.RedId = xhsID
- }
- return &orcInfo
- }
- func getWBOCRresult(homePicURL string, nickName string) *youngee_talent_model.OCRResponse {
- // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
- auth := basic.NewCredentialsBuilder().
- WithAk("YJRGTOWEITSYOXN3YPJY").
- WithSk("Kd7WDeS2CxlLXjMhGpLW0AmdOZHafitQpBy13a0s").
- Build()
- client := ocr.NewOcrClient(
- ocr.OcrClientBuilder().
- WithRegion(region.ValueOf("cn-north-4")).
- WithCredential(auth).
- Build())
- request := &model.RecognizeWebImageRequest{}
- request.Body = &model.WebImageRequestBody{
- Url: &homePicURL,
- }
- // 发送请求
- response, err := client.RecognizeWebImage(request)
- if err != nil {
- return nil
- }
- // 输出返回的完整响应
- fmt.Printf("%+v\n", response)
- resultText := response.String()
- fmt.Println("resultText--->:", resultText)
- var orcInfo youngee_talent_model.OCRResponse
- //检测是否是自己的主页
- //检查是否是自己的首页
- if strings.Contains(resultText, "编辑个人资料") &&
- strings.Contains(resultText, "全部微博") &&
- strings.Contains(resultText, nickName) {
- orcInfo.SelfHome = 1
- } else {
- orcInfo.SelfHome = 0
- }
- return &orcInfo
- }
- func getBliOCRresult(homePicURL string, nickName string) *youngee_talent_model.OCRResponse {
- // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
- auth := basic.NewCredentialsBuilder().
- WithAk("YJRGTOWEITSYOXN3YPJY").
- WithSk("Kd7WDeS2CxlLXjMhGpLW0AmdOZHafitQpBy13a0s").
- Build()
- client := ocr.NewOcrClient(
- ocr.OcrClientBuilder().
- WithRegion(region.ValueOf("cn-north-4")).
- WithCredential(auth).
- Build())
- request := &model.RecognizeWebImageRequest{}
- request.Body = &model.WebImageRequestBody{
- Url: &homePicURL,
- }
- // 发送请求
- response, err := client.RecognizeWebImage(request)
- if err != nil {
- return nil
- }
- // 输出返回的完整响应
- fmt.Printf("%+v\n", response)
- resultText := response.String()
- fmt.Println("resultText--->:", resultText)
- var orcInfo youngee_talent_model.OCRResponse
- //检测是否是自己的主页
- if strings.Contains(resultText, "编辑资料") &&
- strings.Contains(resultText, nickName) {
- orcInfo.SelfHome = 1
- } else {
- orcInfo.SelfHome = 0
- }
- return &orcInfo
- }
- // 获取WB用户详细信息
- func TestImage(r *ghttp.Request) *TalentHttpResult {
- one, _ := g.Model("platform_kuaishou_user_info").Where("id", 188).One()
- return &TalentHttpResult{Code: 0, Msg: "success", Data: one}
- }
- func getRedirectedURL(urlStr string) (string, error) {
- client := &http.Client{
- CheckRedirect: func(req *http.Request, via []*http.Request) error {
- if len(via) > 0 { // 如果发生重定向,返回最终的重定向 URL
- return nil
- }
- return fmt.Errorf("final URL: %s", req.URL) // 获取最终的重定向 URL
- },
- }
- resp, err := client.Get(urlStr)
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
- return resp.Request.URL.String(), nil // 返回最终目标 URL
- }
- func extractUserID(urlStr string) (string, error) {
- // 解析 URL
- parsedURL, err := url.Parse(urlStr)
- if err != nil {
- return "", err
- }
- // 提取路径部分中的用户 ID
- // 这里我们假设用户 ID 是 URL 的路径中的一部分(如 space.bilibili.com/29408510)
- segments := parsedURL.Path
- var userID string
- // 假设 ID 是路径中的第二部分,如 /29408510
- fmt.Sscanf(segments, "/%s", &userID)
- return userID, nil
- }
|