From aad0fb2c8768a364ebb3441275ddd7a9e29883eb Mon Sep 17 00:00:00 2001 From: Tsvetoslav Dimov Date: Wed, 28 Aug 2024 20:01:50 +0100 Subject: [PATCH] fix: use single tab navigation to prevent cookie issues --- .github/workflows/sync.yaml | 4 +- go.mod | 21 ++++--- go.sum | 45 +++++++-------- pkg/client/imdb.go | 111 ++++++++++++++++++++---------------- 4 files changed, 94 insertions(+), 87 deletions(-) diff --git a/.github/workflows/sync.yaml b/.github/workflows/sync.yaml index c5f0d26..c456349 100644 --- a/.github/workflows/sync.yaml +++ b/.github/workflows/sync.yaml @@ -30,8 +30,8 @@ jobs: steps: - name: Install Google Chrome run: | - wget -qO google-chrome-stable.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb - sudo dpkg --install google-chrome-stable.deb + wget --progress=dot:giga https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/1321438/chrome-linux.zip + unzip chrome-linux.zip -d "$HOME/.browser" - name: Check out code uses: actions/checkout@v4 - name: Setup Go diff --git a/go.mod b/go.mod index 9a235ae..6153070 100644 --- a/go.mod +++ b/go.mod @@ -4,27 +4,25 @@ go 1.22.2 require ( github.com/PuerkitoBio/goquery v1.9.2 - github.com/charmbracelet/bubbles v0.18.0 - github.com/charmbracelet/bubbletea v0.26.6 - github.com/charmbracelet/lipgloss v0.11.0 - github.com/go-rod/rod v0.116.1 - github.com/go-rod/stealth v0.4.9 + github.com/charmbracelet/bubbles v0.19.0 + github.com/charmbracelet/bubbletea v1.0.0 + github.com/charmbracelet/lipgloss v0.13.0 + github.com/go-rod/rod v0.116.2 github.com/jarcoal/httpmock v1.3.1 github.com/knadh/koanf/parsers/yaml v0.1.0 github.com/knadh/koanf/providers/confmap v0.1.0 github.com/knadh/koanf/providers/env v0.1.0 - github.com/knadh/koanf/providers/file v1.0.0 + github.com/knadh/koanf/providers/file v1.1.0 github.com/knadh/koanf/v2 v2.1.1 github.com/spf13/cobra v1.8.1 github.com/stretchr/testify v1.9.0 - golang.org/x/sync v0.7.0 ) require ( github.com/andybalholm/cascadia v1.3.2 // indirect github.com/atotto/clipboard v0.1.4 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect - github.com/charmbracelet/x/ansi v0.1.2 // indirect + github.com/charmbracelet/x/ansi v0.1.4 // indirect github.com/charmbracelet/x/input v0.1.0 // indirect github.com/charmbracelet/x/term v0.1.1 // indirect github.com/charmbracelet/x/windows v0.1.0 // indirect @@ -38,7 +36,7 @@ require ( github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-localereader v0.0.1 // indirect - github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/mattn/go-runewidth v0.0.16 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect @@ -52,9 +50,10 @@ require ( github.com/ysmood/goob v0.4.0 // indirect github.com/ysmood/got v0.40.0 // indirect github.com/ysmood/gson v0.7.3 // indirect - github.com/ysmood/leakless v0.8.0 // indirect + github.com/ysmood/leakless v0.9.0 // indirect golang.org/x/net v0.24.0 // indirect - golang.org/x/sys v0.21.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/sys v0.24.0 // indirect golang.org/x/text v0.14.0 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 5a1c3dc..95edf87 100644 --- a/go.sum +++ b/go.sum @@ -6,14 +6,14 @@ github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= -github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0= -github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw= -github.com/charmbracelet/bubbletea v0.26.6 h1:zTCWSuST+3yZYZnVSvbXwKOPRSNZceVeqpzOLN2zq1s= -github.com/charmbracelet/bubbletea v0.26.6/go.mod h1:dz8CWPlfCCGLFbBlTY4N7bjLiyOGDJEnd2Muu7pOWhk= -github.com/charmbracelet/lipgloss v0.11.0 h1:UoAcbQ6Qml8hDwSWs0Y1cB5TEQuZkDPH/ZqwWWYTG4g= -github.com/charmbracelet/lipgloss v0.11.0/go.mod h1:1UdRTH9gYgpcdNN5oBtjbu/IzNKtzVtb7sqN1t9LNn8= -github.com/charmbracelet/x/ansi v0.1.2 h1:6+LR39uG8DE6zAmbu023YlqjJHkYXDF1z36ZwzO4xZY= -github.com/charmbracelet/x/ansi v0.1.2/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= +github.com/charmbracelet/bubbles v0.19.0 h1:gKZkKXPP6GlDk6EcfujDK19PCQqRjaJZQ7QRERx1UF0= +github.com/charmbracelet/bubbles v0.19.0/go.mod h1:WILteEqZ+krG5c3ntGEMeG99nCupcuIk7V0/zOP0tOA= +github.com/charmbracelet/bubbletea v1.0.0 h1:BlNvkVed3DADQlV+W79eioNUOrnMUY25EEVdFUoDoGA= +github.com/charmbracelet/bubbletea v1.0.0/go.mod h1:xc4gm5yv+7tbniEvQ0naiG9P3fzYhk16cTgDZQQW6YE= +github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw= +github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY= +github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM= +github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ= github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28= github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI= @@ -27,11 +27,8 @@ github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= -github.com/go-rod/rod v0.113.0/go.mod h1:aiedSEFg5DwG/fnNbUOTPMTTWX3MRj6vIs/a684Mthw= -github.com/go-rod/rod v0.116.1 h1:BDMZY3qm/14SmvHBV7DoFUhXeJ2MbUYgumQ88b+v2WE= -github.com/go-rod/rod v0.116.1/go.mod h1:3Ash9fYwznqz9S1uLQgQRStur4fCXjoxxGW+ym6TYjU= -github.com/go-rod/stealth v0.4.9 h1:X2PmQk4DUF2wzw6GOsWjW/glb8K5ebnftbEvLh7MlZ4= -github.com/go-rod/stealth v0.4.9/go.mod h1:eAzyvw8c0iAd5nJJsSWeh0fQ5z94vCIfdi1hUmYDimc= +github.com/go-rod/rod v0.116.2 h1:A5t2Ky2A+5eD/ZJQr1EfsQSe5rms5Xof/qj296e+ZqA= +github.com/go-rod/rod v0.116.2/go.mod h1:H+CMO9SCNc2TJ2WfrG+pKhITz57uGNYU43qYHh438Mg= github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 h1:TQcrn6Wq+sKGkpyPvppOz99zsMBaUOKXq6HSv655U1c= github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= @@ -46,8 +43,8 @@ github.com/knadh/koanf/providers/confmap v0.1.0 h1:gOkxhHkemwG4LezxxN8DMOFopOPgh github.com/knadh/koanf/providers/confmap v0.1.0/go.mod h1:2uLhxQzJnyHKfxG927awZC7+fyHFdQkd697K4MdLnIU= github.com/knadh/koanf/providers/env v0.1.0 h1:LqKteXqfOWyx5Ab9VfGHmjY9BvRXi+clwyZozgVRiKg= github.com/knadh/koanf/providers/env v0.1.0/go.mod h1:RE8K9GbACJkeEnkl8L/Qcj8p4ZyPXZIQ191HJi44ZaQ= -github.com/knadh/koanf/providers/file v1.0.0 h1:DtPvSQBeF+N0QLPMz0yf2bx0nFSxUcncpqQvzCxfCyk= -github.com/knadh/koanf/providers/file v1.0.0/go.mod h1:/faSBcv2mxPVjFrXck95qeoyoZ5myJ6uxN8OOVNJJCI= +github.com/knadh/koanf/providers/file v1.1.0 h1:MTjA+gRrVl1zqgetEAIaXHqYje0XSosxSiMD4/7kz0o= +github.com/knadh/koanf/providers/file v1.1.0/go.mod h1:/faSBcv2mxPVjFrXck95qeoyoZ5myJ6uxN8OOVNJJCI= github.com/knadh/koanf/v2 v2.1.1 h1:/R8eXqasSTsmDCsAyYj+81Wteg8AqrV9CP6gvsTsOmM= github.com/knadh/koanf/v2 v2.1.1/go.mod h1:4mnTRbZCK+ALuBXHZMjDfG9y714L7TykVnZkXbMU3Es= github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= @@ -61,8 +58,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= -github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= -github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= +github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g= github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM= github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= @@ -93,18 +90,16 @@ github.com/ysmood/fetchup v0.2.3 h1:ulX+SonA0Vma5zUFXtv52Kzip/xe7aj4vqT5AJwQ+ZQ= github.com/ysmood/fetchup v0.2.3/go.mod h1:xhibcRKziSvol0H1/pj33dnKrYyI2ebIvz5cOOkYGns= github.com/ysmood/goob v0.4.0 h1:HsxXhyLBeGzWXnqVKtmT9qM7EuVs/XOgkX7T6r1o1AQ= github.com/ysmood/goob v0.4.0/go.mod h1:u6yx7ZhS4Exf2MwciFr6nIM8knHQIE22lFpWHnfql18= -github.com/ysmood/gop v0.0.2/go.mod h1:rr5z2z27oGEbyB787hpEcx4ab8cCiPnKxn0SUHt6xzk= github.com/ysmood/gop v0.2.0 h1:+tFrG0TWPxT6p9ZaZs+VY+opCvHU8/3Fk6BaNv6kqKg= github.com/ysmood/gop v0.2.0/go.mod h1:rr5z2z27oGEbyB787hpEcx4ab8cCiPnKxn0SUHt6xzk= -github.com/ysmood/got v0.34.1/go.mod h1:yddyjq/PmAf08RMLSwDjPyCvHvYed+WjHnQxpH851LM= github.com/ysmood/got v0.40.0 h1:ZQk1B55zIvS7zflRrkGfPDrPG3d7+JOza1ZkNxcc74Q= github.com/ysmood/got v0.40.0/go.mod h1:W7DdpuX6skL3NszLmAsC5hT7JAhuLZhByVzHTq874Qg= github.com/ysmood/gotrace v0.6.0 h1:SyI1d4jclswLhg7SWTL6os3L1WOKeNn/ZtzVQF8QmdY= github.com/ysmood/gotrace v0.6.0/go.mod h1:TzhIG7nHDry5//eYZDYcTzuJLYQIkykJzCRIo4/dzQM= github.com/ysmood/gson v0.7.3 h1:QFkWbTH8MxyUTKPkVWAENJhxqdBa4lYTQWqZCiLG6kE= github.com/ysmood/gson v0.7.3/go.mod h1:3Kzs5zDl21g5F/BlLTNcuAGAYLKt2lV5G8D1zF3RNmg= -github.com/ysmood/leakless v0.8.0 h1:BzLrVoiwxikpgEQR0Lk8NyBN5Cit2b1z+u0mgL4ZJak= -github.com/ysmood/leakless v0.8.0/go.mod h1:R8iAXPRaG97QJwqxs74RdwzcRHT1SWCGTNqY8q0JvMQ= +github.com/ysmood/leakless v0.9.0 h1:qxCG5VirSBvmi3uynXFkcnLMzkphdh3xx5FtrORwDCU= +github.com/ysmood/leakless v0.9.0/go.mod h1:R8iAXPRaG97QJwqxs74RdwzcRHT1SWCGTNqY8q0JvMQ= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -122,8 +117,8 @@ golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -133,8 +128,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= -golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= +golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 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= diff --git a/pkg/client/imdb.go b/pkg/client/imdb.go index e5567a6..af7be14 100644 --- a/pkg/client/imdb.go +++ b/pkg/client/imdb.go @@ -6,7 +6,7 @@ import ( "encoding/csv" "fmt" "log/slog" - "net/http" + "os" "slices" "strconv" "strings" @@ -15,8 +15,6 @@ import ( "github.com/go-rod/rod" "github.com/go-rod/rod/lib/launcher" "github.com/go-rod/rod/lib/proto" - "github.com/go-rod/stealth" - "golang.org/x/sync/errgroup" appconfig "github.com/cecobask/imdb-trakt-sync/internal/config" "github.com/cecobask/imdb-trakt-sync/internal/entities" @@ -27,7 +25,7 @@ const ( imdbPathExports = "/exports" imdbPathList = "/list/%s" imdbPathLists = "/profile/lists" - imdbPathRatings = "/list/ratings" + imdbPathRatings = "/user/%s/ratings" imdbPathSignIn = "/registration/ap-signin-handler/imdb_us" imdbPathWatchlist = "/list/watchlist" imdbCookieNameAtMain = "at-main" @@ -49,11 +47,40 @@ type imdbConfig struct { } func NewIMDbClient(ctx context.Context, conf *appconfig.IMDb, logger *slog.Logger) (IMDbClientInterface, error) { - browserPath, ok := launcher.LookPath() - if !ok { - return nil, fmt.Errorf("failure looking up browser path") + var browserPath string + if os.Getenv("GITHUB_ACTIONS") == "true" { + browserPath = fmt.Sprintf("%s/.browser/chrome-linux/chrome", os.Getenv("HOME")) + } else { + var found bool + browserPath, found = launcher.LookPath() + if !found { + return nil, fmt.Errorf("failure looking up browser path") + } } - browserURL, err := launcher.New().Bin(browserPath).Headless(*conf.Headless).Launch() + l := launcher.New().Headless(*conf.Headless).Bin(browserPath). + Set("allow-running-insecure-content"). + Set("autoplay-policy", "user-gesture-required"). + Set("disable-component-update"). + Set("disable-domain-reliability"). + Set("disable-features", "AudioServiceOutOfProcess", "IsolateOrigins", "site-per-process"). + Set("disable-print-preview"). + Set("disable-search-engine-choice-screen"). + Set("disable-setuid-sandbox"). + Set("disable-site-isolation-trials"). + Set("disable-speech-api"). + Set("disable-web-security"). + Set("disk-cache-size", "33554432"). + Set("enable-features", "SharedArrayBuffer"). + Set("hide-scrollbars"). + Set("ignore-gpu-blocklist"). + Set("in-process-gpu"). + Set("mute-audio"). + Set("no-default-browser-check"). + Set("no-pings"). + Set("no-sandbox"). + Set("no-zygote"). + Set("single-process") + browserURL, err := l.Launch() if err != nil { return nil, fmt.Errorf("failure launching browser: %w", err) } @@ -86,8 +113,7 @@ func (c *IMDbClient) authenticateUser() error { if err := setBrowserCookies(c.browser, c.config.IMDb); err != nil { return err } - tab, cleanupFunc, err := c.navigateAndValidateResponse(imdbPathBase) - defer cleanupFunc() + tab, err := c.navigateAndValidateResponse(imdbPathBase) if err != nil { return fmt.Errorf("failure navigating and validating response: %w", err) } @@ -100,8 +126,7 @@ func (c *IMDbClient) authenticateUser() error { } return nil } - tab, cleanupFunc, err := c.navigateAndValidateResponse(imdbPathBase + imdbPathSignIn) - defer cleanupFunc() + tab, err := c.navigateAndValidateResponse(imdbPathBase + imdbPathSignIn) if err != nil { return fmt.Errorf("failure navigating and validating response: %w", err) } @@ -151,8 +176,7 @@ func (c *IMDbClient) hydrate() error { if *c.config.Auth == appconfig.IMDbAuthMethodNone { return nil } - tab, cleanupFunc, err := c.navigateAndValidateResponse(imdbPathBase + imdbPathWatchlist) - defer cleanupFunc() + tab, err := c.navigateAndValidateResponse(imdbPathBase + imdbPathWatchlist) if err != nil { return fmt.Errorf("failure navigating and validating response: %w", err) } @@ -230,21 +254,19 @@ func (c *IMDbClient) ListExport(id string) error { } func (c *IMDbClient) ListsExport(ids ...string) error { - var errGroup errgroup.Group for _, id := range ids { - errGroup.Go(func() error { - return c.ListExport(id) - }) + if err := c.ListExport(id); err != nil { + return err + } } - return errGroup.Wait() + return nil } func (c *IMDbClient) ListsGet(ids ...string) ([]entities.IMDbList, error) { if len(ids) == 0 { return make([]entities.IMDbList, 0), nil } - resources, cleanupFunc, err := c.getExportedResources(ids...) - defer cleanupFunc() + resources, err := c.getExportedResources(ids...) if err != nil { return nil, fmt.Errorf("failure fetching exported resources: %w", err) } @@ -267,7 +289,7 @@ func (c *IMDbClient) RatingsExport() error { if *c.config.Auth == appconfig.IMDbAuthMethodNone { return nil } - ratingsURL := imdbPathBase + imdbPathRatings + ratingsURL := imdbPathBase + fmt.Sprintf(imdbPathRatings, c.config.userID) if err := c.exportResource(ratingsURL); err != nil { return fmt.Errorf("failure exporting ratings resource: %w", err) } @@ -276,8 +298,7 @@ func (c *IMDbClient) RatingsExport() error { } func (c *IMDbClient) RatingsGet() ([]entities.IMDbItem, error) { - resources, cleanupFunc, err := c.getExportedResources(c.config.userID) - defer cleanupFunc() + resources, err := c.getExportedResources(c.config.userID) if err != nil { return nil, fmt.Errorf("failure fetching exported resources: %w", err) } @@ -343,24 +364,23 @@ func (c *IMDbClient) listDownload(resource *rod.Element) (*entities.IMDbList, er }, nil } -func (c *IMDbClient) getExportedResources(ids ...string) (rod.Elements, func(), error) { - tab, cleanupFunc, err := c.navigateAndValidateResponse(imdbPathBase + imdbPathExports) +func (c *IMDbClient) getExportedResources(ids ...string) (rod.Elements, error) { + tab, err := c.navigateAndValidateResponse(imdbPathBase + imdbPathExports) if err != nil { - return nil, cleanupFunc, fmt.Errorf("failure navigating and validating response: %w", err) + return nil, fmt.Errorf("failure navigating and validating response: %w", err) } if err = c.waitExportsReady(tab, ids...); err != nil { - return nil, cleanupFunc, fmt.Errorf("failure waiting for exports to become available: %w", err) + return nil, fmt.Errorf("failure waiting for exports to become available: %w", err) } resources, err := tab.Elements("li[data-testid='user-ll-item']") if err != nil { - return nil, cleanupFunc, fmt.Errorf("failure finding exported resources: %w", err) + return nil, fmt.Errorf("failure finding exported resources: %w", err) } - return resources, cleanupFunc, nil + return resources, nil } func (c *IMDbClient) exportResource(url string) error { - tab, cleanupFunc, err := c.navigateAndValidateResponse(url) - defer cleanupFunc() + tab, err := c.navigateAndValidateResponse(url) if err != nil { return fmt.Errorf("failure navigating and validating response: %w", err) } @@ -459,8 +479,7 @@ func (c *IMDbClient) filterResources(resources rod.Elements, ids ...string) (rod } func (c *IMDbClient) lidsScrape() ([]string, error) { - tab, cleanupFunc, err := c.navigateAndValidateResponse(imdbPathBase + imdbPathLists) - defer cleanupFunc() + tab, err := c.navigateAndValidateResponse(imdbPathBase + imdbPathLists) if err != nil { return nil, fmt.Errorf("failure navigating and validating response: %w", err) } @@ -526,28 +545,22 @@ func (c *IMDbClient) scrollUntilAllElementsVisible(tab *rod.Page, selector strin return nil } -func (c *IMDbClient) navigateAndValidateResponse(url string) (*rod.Page, func(), error) { - tab, err := stealth.Page(c.browser) +func (c *IMDbClient) navigateAndValidateResponse(url string) (*rod.Page, error) { + pages, err := c.browser.Pages() if err != nil { - return nil, func() {}, fmt.Errorf("failure opening browser tab: %w", err) + return nil, fmt.Errorf("failure retrieving browser pages: %w", err) } - cleanupFunc := func() { - tab.MustClose() + tab := pages.First() + if pages.Empty() { + tab = c.browser.MustPage() } - go tab.MustHandleDialog() - var event proto.NetworkResponseReceived - wait := tab.WaitEvent(&event) if err = tab.Navigate(url); err != nil { - return nil, cleanupFunc, fmt.Errorf("failure navigating to url %s: %w", url, err) - } - wait() - if status := event.Response.Status; status != http.StatusOK { - return nil, cleanupFunc, fmt.Errorf("navigating to %s produced %d status", url, status) + return nil, fmt.Errorf("failure navigating to url %s: %w", url, err) } if err = tab.WaitLoad(); err != nil { - return nil, cleanupFunc, fmt.Errorf("failure waiting for tab %s to load: %w", url, err) + return nil, fmt.Errorf("failure waiting for tab %s to load: %w", url, err) } - return tab, cleanupFunc, nil + return tab, nil } func isListHyperlink(href string) bool {