Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tools
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
庄欣
tools
Commits
ee68230e
Commit
ee68230e
authored
May 17, 2021
by
庄欣
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
用户默认昵称用户xxx变更为新用户xxx
parent
7e0c0626
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
236 additions
and
2 deletions
+236
-2
tieba/config.yaml
+16
-0
tieba/main.go
+6
-0
tieba/service/service.go
+4
-2
tieba/service/update.go
+183
-0
tieba/test/s_test.go
+27
-0
No files found.
tieba/config.yaml
0 → 100644
View file @
ee68230e
debug
:
1
dev
:
db
:
root:mH9TaSz#FV43T*z%@(111.231.204.74:3306)/mepai_v2
qcloud
:
region
:
sh
bucket
:
devmepai
secret-id
:
AKIDNcdPozQhJSXWXNtUinnT1OmwqhhT6NQ0
secret-key
:
TgdNr89wc2OUK5aomX1IJ8lCVMN82n0y
prod
:
db
:
qcloud
:
region
:
bucket
:
secret-id
:
secret-key
:
\ No newline at end of file
tieba/main.go
View file @
ee68230e
...
...
@@ -12,6 +12,12 @@ func main() {
fmt
.
Println
(
"!!您需要输入要采集的贴吧页面地址"
)
return
}
cstr
,
err
:=
os
.
ReadFile
(
"config.yaml"
)
if
err
!=
nil
{
panic
(
err
)
}
service
.
SetConfig
(
cstr
)
service
.
Open
(
os
.
Args
[
1
])
service
.
Begin
();
}
tieba/service/service.go
View file @
ee68230e
...
...
@@ -30,7 +30,7 @@ func SaveUser(users []User) {
if
strings
.
Contains
(
u
.
Avatar
,
"tb2.bdstatic.com/tb/static-pb/img/head_80"
)
{
continue
}
file
,
_
:=
os
.
OpenFile
(
avatars
+
u
.
Nickname
,
os
.
O_WRONLY
|
os
.
O_CREATE
,
0
666
)
file
,
_
:=
os
.
OpenFile
(
avatars
+
u
.
Nickname
,
os
.
O_WRONLY
|
os
.
O_CREATE
,
0
777
)
res
,
err
:=
http
.
Get
(
u
.
Avatar
)
if
!
strings
.
Contains
(
u
.
Avatar
,
"http"
)
{
u
.
Avatar
=
"https:"
+
u
.
Avatar
...
...
@@ -39,7 +39,6 @@ func SaveUser(users []User) {
fmt
.
Printf
(
"无法访问:%s"
,
u
.
Avatar
)
continue
}
defer
res
.
Body
.
Close
()
data
,
_
:=
ioutil
.
ReadAll
(
res
.
Body
)
file
.
Write
(
data
)
...
...
@@ -81,6 +80,9 @@ Loop:
chromedp
.
ScrollIntoView
(
"#tb_rich_poster > div.poster_head.clearfix > div.poster_head_text > a"
)
.
Do
(
ctx
)
goLoop
:=
chromedp
.
WaitVisible
(
"#thread_theme_7 > div.l_thread_info > ul > li.l_pager.pager_theme_5.pb_list_pager > a:nth-child(7)"
,
chromedp
.
ByQuery
,
chromedp
.
AtLeast
(
0
),
chromedp
.
After
(
func
(
ctx2
context
.
Context
,
id
runtime
.
ExecutionContextID
,
node
...*
cdp
.
Node
)
error
{
if
len
(
node
)
<=
0
{
return
nil
}
chromedp
.
Click
(
"#thread_theme_7 > div.l_thread_info > ul > li.l_pager.pager_theme_5.pb_list_pager > a:nth-child(7)"
)
.
Do
(
ctx2
)
return
errors
.
New
(
"go"
);
}),
...
...
tieba/service/update.go
0 → 100644
View file @
ee68230e
package
service
import
(
"bytes"
"context"
"fmt"
_
"github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"github.com/tencentyun/cos-go-sdk-v5"
"gopkg.in/yaml.v3"
"io/ioutil"
"math/big"
"net/http"
"net/url"
"os"
"time"
"crypto/rand"
)
type
Tieba
struct
{
Debug
int
Dev
Detail
Prod
Detail
}
type
Detail
struct
{
Db
string
QCloud
QCloud
}
type
QCloud
struct
{
Region
string
Bucket
string
SecretId
string
SecretKey
string
}
type
FUser
struct
{
Id
int
`json:"id"`
Nickname
string
`json:"nickname"`
Uid
int
`json:"uid"`
}
type
UserModel
struct
{
Id
int
`json:"id"`
Nickname
string
`json:"nickname"`
Avatar
string
`json:"avatar"`
NicknameLower
string
`json:"nickname_lower"`
}
type
CosConfig
struct
{
SecretId
string
`yaml:"secret-id"`
SecretKey
string
`yaml:"secret-key"`
Bucket
string
Region
string
}
var
dbc
*
xorm
.
Engine
var
config
Detail
var
allInfo
[]
string
func
SetConfig
(
cstr
[]
byte
)
{
var
ac
Tieba
err
:=
yaml
.
Unmarshal
(
cstr
,
&
ac
)
if
err
!=
nil
{
fmt
.
Println
(
err
.
Error
())
return
}
if
ac
.
Debug
==
1
{
config
=
ac
.
Dev
}
else
{
config
=
ac
.
Prod
}
dbc
,
_
=
xorm
.
NewEngine
(
"mysql"
,
config
.
Db
)
//连接池都设为20
dbc
.
SetMaxOpenConns
(
20
);
dbc
.
ShowSQL
(
true
)
}
func
upload
(
stream
[]
byte
,
remotePath
string
)
{
u
,
_
:=
url
.
Parse
(
fmt
.
Sprintf
(
"https://%s.cos.%s.myqcloud.com"
,
config
.
QCloud
.
Bucket
,
config
.
QCloud
.
Region
))
b
:=
&
cos
.
BaseURL
{
BucketURL
:
u
}
c
:=
cos
.
NewClient
(
b
,
&
http
.
Client
{
Timeout
:
100
*
time
.
Second
,
Transport
:
&
cos
.
AuthorizationTransport
{
SecretID
:
config
.
QCloud
.
SecretId
,
SecretKey
:
config
.
QCloud
.
SecretKey
,
},
})
ctx
:=
context
.
Background
()
c
.
Object
.
Put
(
ctx
,
remotePath
,
bytes
.
NewReader
(
stream
),
nil
)
}
func
Begin
()
{
var
items
[]
FUser
;
var
lastId
int
=
-
1
;
ReadAll
()
for
;;
{
items
=
items
[
0
:
0
]
err
:=
dbc
.
Table
(
"mepai_fuser"
)
.
Join
(
"LEFT"
,
"mepai_user"
,
"mepai_fuser.uid=mepai_user.id"
)
.
Where
(
"mepai_user.nickname_lower like ?"
,
"用户%"
)
.
And
(
"mepai_fuser.id > ?"
,
lastId
)
.
Asc
(
"mepai_fuser.id"
)
.
Limit
(
30
)
.
Find
(
&
items
)
if
err
!=
nil
{
fmt
.
Println
(
err
.
Error
())
}
if
len
(
items
)
<=
0
{
break
}
lastId
=
items
[
len
(
items
)
-
1
]
.
Id
updateUser
(
items
)
}
}
func
updateUser
(
users
[]
FUser
)
{
for
_
,
u
:=
range
users
{
info
:=
getOne
(
u
.
Uid
);
fmt
.
Printf
(
"获取到%s给用户%d,头像:%s
\n
"
,
info
.
Nickname
,
u
.
Uid
,
info
.
Avatar
)
_
,
err
:=
dbc
.
Table
(
"mepai_user"
)
.
Cols
(
"nickname"
,
"avatar"
,
"nickname_lower"
)
.
Update
(
info
)
if
err
!=
nil
{
fmt
.
Println
(
err
.
Error
())
}
}
}
func
ReadAll
()
{
dir
:=
"./avatars"
files
,
err
:=
ioutil
.
ReadDir
(
dir
)
if
err
!=
nil
{
fmt
.
Println
(
"read dir fail:"
,
err
)
return
}
for
_
,
fi
:=
range
files
{
if
!
fi
.
IsDir
()
{
allInfo
=
append
(
allInfo
,
fi
.
Name
())
}
}
}
func
getOne
(
uid
int
)
UserModel
{
var
one
string
var
user
UserModel
dir
:=
"./avatars/"
Loop
:
one
,
allInfo
=
allInfo
[
0
],
allInfo
[
1
:
]
_
,
err
:=
dbc
.
Table
(
"mepai_user"
)
.
Where
(
"nickname_lower = ?"
,
one
)
.
Get
(
&
user
)
if
err
!=
nil
{
panic
(
err
)
}
if
user
.
Id
>
0
{
goto
Loop
}
path
:=
fmt
.
Sprintf
(
"/app/u/%d/avatar_%s.jpg"
,
uid
,
CreateRandomString
(
16
))
filePath
:=
dir
+
one
stream
,
_
:=
ioutil
.
ReadFile
(
filePath
)
upload
(
stream
,
path
)
user
.
Id
=
uid
user
.
Avatar
=
path
user
.
Nickname
=
one
user
.
NicknameLower
=
one
os
.
Remove
(
filePath
)
return
user
}
func
CreateRandomString
(
len
int
)
string
{
var
container
string
var
str
=
"abcdefghijklmnopqrstuvwxyz1234567890"
b
:=
bytes
.
NewBufferString
(
str
)
length
:=
b
.
Len
()
bigInt
:=
big
.
NewInt
(
int64
(
length
))
for
i
:=
0
;
i
<
len
;
i
++
{
randomInt
,
_
:=
rand
.
Int
(
rand
.
Reader
,
bigInt
)
container
+=
string
(
str
[
randomInt
.
Int64
()])
}
return
container
}
tieba/test/s_test.go
0 → 100644
View file @
ee68230e
package
test
import
(
"bytes"
"fmt"
"math/big"
"testing"
"crypto/rand"
)
func
TestR
(
t
*
testing
.
T
)
{
fmt
.
Println
(
CreateRandomString
(
16
))
}
func
CreateRandomString
(
len
int
)
string
{
var
container
string
var
str
=
"abcdefghijklmnopqrstuvwxyz1234567890"
b
:=
bytes
.
NewBufferString
(
str
)
length
:=
b
.
Len
()
bigInt
:=
big
.
NewInt
(
int64
(
length
))
for
i
:=
0
;
i
<
len
;
i
++
{
randomInt
,
_
:=
rand
.
Int
(
rand
.
Reader
,
bigInt
)
container
+=
string
(
str
[
randomInt
.
Int64
()])
}
return
container
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment