Skip to content

Commit

Permalink
feat: implement link preview module and replaced Junzki/link-preview (#…
Browse files Browse the repository at this point in the history
…68)

* feat: implement link preview module and replaced Junzki/link-preview
* chore: organize code
  • Loading branch information
nekomeowww authored May 8, 2023
1 parent 363eb81 commit 5eadd47
Show file tree
Hide file tree
Showing 13 changed files with 358 additions and 31 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ issues:
- "if statements should only be cuddled with assignments" # from wsl
- "if statements should only be cuddled with assignments used in the if statement itself" # from wsl
- "assignments should only be cuddled with other assignments" # from wsl. false positive case: var a bool\nb := true
- "declarations should never be cuddled" # from wsl

3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
entgo.io/ent v0.12.2
github.com/Junzki/link-preview v0.0.0-20190616153136-4d94cb3f9ebf
github.com/PuerkitoBio/goquery v1.8.1
github.com/davecgh/go-spew v1.1.1
github.com/gin-gonic/gin v1.9.0
github.com/go-shiori/go-readability v0.0.0-20220215145315-dd6828d2f09b
Expand All @@ -28,7 +28,6 @@ require (

require (
ariga.io/atlas v0.10.0 // indirect
github.com/PuerkitoBio/goquery v1.5.0 // indirect
github.com/agext/levenshtein v1.2.1 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
Expand Down
24 changes: 18 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,13 @@ entgo.io/ent v0.12.2/go.mod h1:OA1Y5bNE8EtlxKv4IyzWwt4jgvGbkoKMcwp668iEKQE=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/Junzki/link-preview v0.0.0-20190616153136-4d94cb3f9ebf h1:5Ju8l+vxbEBwZw11lJPlcXxlb4c4/XwgXONwtScu4d4=
github.com/Junzki/link-preview v0.0.0-20190616153136-4d94cb3f9ebf/go.mod h1:e+kYgytfBnZ7WFS+SxyUkNeRu4X1NeZKMqxUDITLiD0=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/goquery v1.5.0 h1:uGvmFXOA73IKluu/F84Xd1tt/z07GYm8X49XKHP7EJk=
github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg=
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/andybalholm/cascadia v1.2.0/go.mod h1:YCyR8vOZT9aZ1CHEd8ap0gMVm2aFgxBp0T0eFw1RUQY=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
Expand Down Expand Up @@ -232,7 +229,6 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/imroc/req/v3 v3.33.2 h1:mqphLIo++p+IPYdjgP/Wd5rqXUjKvuEIst2U+EsLIwQ=
github.com/imroc/req/v3 v3.33.2/go.mod h1:cZ+7C3L/AYOr4tLGG16hZF90F1WzAdAdzt1xFSlizXY=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand Down Expand Up @@ -375,6 +371,7 @@ github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zclconf/go-cty v1.8.0 h1:s4AvqaeQzJIu3ndv4gVIhplVD0krU+bgrcLSVUnaWuA=
github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
Expand Down Expand Up @@ -407,6 +404,7 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -442,6 +440,7 @@ golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -474,10 +473,13 @@ golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210505214959-0714010a04ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand All @@ -495,6 +497,7 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -529,18 +532,26 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down Expand Up @@ -588,6 +599,7 @@ golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y=
golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
7 changes: 6 additions & 1 deletion internal/bots/slack/processors.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package slack

import (
"context"
"errors"
"time"

"github.com/nekomeowww/insights-bot/internal/models/smr"
"github.com/nekomeowww/insights-bot/pkg/bots/slackbot"
"github.com/slack-go/slack"
)

func (b *SlackBot) smr(info smrRequestInfo) {
summarization, err := b.smrModel.SummarizeInputURL(info.inputUrl)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2)
defer cancel()

summarization, err := b.smrModel.SummarizeInputURL(ctx, info.inputUrl)
slackCfg := b.config.Slack
slackCli := slackbot.NewSlackCli(nil, slackCfg.ClientID, slackCfg.ClientSecret, info.refreshToken, info.accessToken)
tokenStoreFunc := b.newStoreFuncForRefresh(info.teamID)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package summarize

import (
"context"
"strings"
"time"

"github.com/nekomeowww/insights-bot/pkg/bots/tgbot"
)
Expand All @@ -22,7 +24,10 @@ func (h *Handlers) HandleChannelPost(c *tgbot.Context) (tgbot.Response, error) {

urlString := strings.TrimSpace(strings.TrimPrefix(c.Update.ChannelPost.Text, "/smr "))

summarization, err := h.smr.SummarizeInputURL(urlString)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2)
defer cancel()

summarization, err := h.smr.SummarizeInputURL(ctx, urlString)
if err != nil {
return nil, tgbot.NewExceptionError(err)
}
Expand Down
7 changes: 6 additions & 1 deletion internal/bots/telegram/handlers/summarize/smr_command.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package summarize

import (
"context"
"errors"
"net/url"
"time"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/samber/lo"
Expand Down Expand Up @@ -41,7 +43,10 @@ func (h *Handlers) Handle(c *tgbot.Context) (tgbot.Response, error) {
return nil, tgbot.NewExceptionError(err)
}

summarization, err := h.smr.SummarizeInputURL(urlString)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2)
defer cancel()

summarization, err := h.smr.SummarizeInputURL(ctx, urlString)
if err != nil {
if errors.Is(err, smr.ErrContentNotSupported) {
return nil, tgbot.NewMessageError("暂时不支持量子速读这样的内容呢,可以换个别的链接试试。").WithEdit(&processingMessage)
Expand Down
27 changes: 17 additions & 10 deletions internal/models/chathistories/chat_histories.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"unicode/utf8"

"entgo.io/ent/dialect/sql"
"github.com/Junzki/link-preview"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/google/uuid"
"github.com/samber/lo"
Expand All @@ -25,6 +24,7 @@ import (
"github.com/nekomeowww/insights-bot/ent/chathistories"
"github.com/nekomeowww/insights-bot/internal/datastore"
"github.com/nekomeowww/insights-bot/pkg/bots/tgbot"
"github.com/nekomeowww/insights-bot/pkg/linkprev"
"github.com/nekomeowww/insights-bot/pkg/logger"
"github.com/nekomeowww/insights-bot/pkg/openai"
"github.com/nekomeowww/insights-bot/pkg/utils"
Expand All @@ -39,17 +39,19 @@ type NewModelParams struct {
}

type Model struct {
logger *logger.Logger
ent *datastore.Ent
openAI *openai.Client
logger *logger.Logger
ent *datastore.Ent
openAI *openai.Client
linkprev *linkprev.Client
}

func NewModel() func(NewModelParams) (*Model, error) {
return func(param NewModelParams) (*Model, error) {
return &Model{
logger: param.Logger,
ent: param.Ent,
openAI: param.OpenAI,
logger: param.Logger,
ent: param.Ent,
openAI: param.OpenAI,
linkprev: linkprev.NewClient(),
}, nil
}
}
Expand All @@ -71,12 +73,17 @@ func (m *Model) ExtractTextFromMessage(message *tgbotapi.Message) string {
var href string
if entity.Type == "url" {
href = string(utf16.Decode(textUTF16[startIndex:endIndex]))
result, err := LinkPreview.PreviewLink(href, nil)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

meta, err := m.linkprev.Preview(ctx, href)
if err != nil {
m.logger.Infof("🔗Failed to generate link preview for %s, error %+v", href, err)
return MarkdownLink{[]uint16{}, -1, -1}
}
title = result.Title

title = lo.Ternary(meta.Title != "", meta.Title, meta.OpenGraph.Title)
} else if entity.Type == "text_link" {
title = string(utf16.Decode(textUTF16[startIndex:endIndex]))
href = entity.URL
Expand All @@ -89,7 +96,7 @@ func (m *Model) ExtractTextFromMessage(message *tgbotapi.Message) string {
href = strings.ReplaceAll(unescaped, " ", "+")
}

md := "[" + title + "](" + href + ")"
md := fmt.Sprintf("[%s](%s)", title, href)
mdUTF16 := utf16.Encode([]rune(md))

return MarkdownLink{mdUTF16, startIndex, endIndex}
Expand Down
17 changes: 9 additions & 8 deletions internal/models/smr/smr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"net/url"
"strings"
"time"

"github.com/go-shiori/go-readability"
"github.com/imroc/req/v3"
Expand Down Expand Up @@ -37,7 +36,9 @@ type Model struct {
func NewModel() func(NewModelParams) *Model {
return func(param NewModelParams) *Model {
return &Model{
req: req.C(),
req: req.
C().
SetUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.54"),
logger: param.Logger,
openai: param.OpenAIClient,
}
Expand Down Expand Up @@ -65,8 +66,8 @@ func (u *URLSummarizationOutput) FormatSummarizationAsSlackMarkdown() string {
return fmt.Sprintf("*<%s|%s>*\n%s\n\n_🤖️ Generated by chatGPT_", u.URL, u.Title, u.Msg)
}

func (m *Model) SummarizeInputURL(url string) (*URLSummarizationOutput, error) {
article, err := m.extractContentFromURL(url)
func (m *Model) SummarizeInputURL(ctx context.Context, url string) (*URLSummarizationOutput, error) {
article, err := m.extractContentFromURL(ctx, url)
if err != nil {
return nil, fmt.Errorf("failed to parse %s, %w", url, err)
}
Expand All @@ -79,7 +80,7 @@ func (m *Model) SummarizeInputURL(url string) (*URLSummarizationOutput, error) {
}).Infof("✍️ summarizing article...")

resp, err := m.openai.SummarizeWithQuestionsAsSimplifiedChinese(
context.Background(),
ctx,
article.Title,
article.Byline,
textContent,
Expand Down Expand Up @@ -110,7 +111,7 @@ func (m *Model) SummarizeInputURL(url string) (*URLSummarizationOutput, error) {
}, nil
}

func (m *Model) extractContentFromURL(urlString string) (*readability.Article, error) {
func (m *Model) extractContentFromURL(ctx context.Context, urlString string) (*readability.Article, error) {
parsedURL, err := url.Parse(urlString)
if err != nil {
return nil, err
Expand All @@ -120,10 +121,9 @@ func (m *Model) extractContentFromURL(urlString string) (*readability.Article, e
}

resp, err := m.req.
SetUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.54").
SetTimeout(time.Minute).
R().
EnableDump().
SetContext(ctx).
Get(parsedURL.String())
if err != nil {
return nil, fmt.Errorf("failed to get url %s, %w: %v", parsedURL.String(), ErrNetworkError, err)
Expand All @@ -135,6 +135,7 @@ func (m *Model) extractContentFromURL(urlString string) (*readability.Article, e
return nil, fmt.Errorf("url fetched, but content-type not supported yet, %w, content-type: %s", ErrContentNotSupported, resp.Header.Get("Content-Type"))
}

defer resp.Body.Close()
buffer := new(bytes.Buffer)

_, err = io.Copy(buffer, resp.Body)
Expand Down
5 changes: 3 additions & 2 deletions internal/models/smr/smr_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package smr

import (
"context"
"fmt"
"os"
"strings"
Expand All @@ -26,7 +27,7 @@ func TestExtractContentFromURL(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

article, err := model.extractContentFromURL("https://a.b.c")
article, err := model.extractContentFromURL(context.Background(), "https://a.b.c")
require.Error(err)
require.Nil(article)

Expand All @@ -40,7 +41,7 @@ func TestExtractContentFromURL(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

article, err := model.extractContentFromURL(fmt.Sprintf("https://mp.weixin.qq.com/s/%s", ""))
article, err := model.extractContentFromURL(context.Background(), fmt.Sprintf("https://mp.weixin.qq.com/s/%s", ""))
require.NoError(err)

assert.NotEmpty(article.Title)
Expand Down
Loading

0 comments on commit 5eadd47

Please sign in to comment.