2023-12-14 19:00:04 +01:00
|
|
|
/*
|
2024-01-16 13:18:15 +01:00
|
|
|
Copyright © 2023-2024 Thomas von Dein
|
2023-12-14 19:00:04 +01:00
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2024-04-26 11:36:27 +02:00
|
|
|
"bufio"
|
2023-12-14 19:00:04 +01:00
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2024-01-01 20:53:05 +01:00
|
|
|
"io"
|
2023-12-15 14:50:40 +01:00
|
|
|
"log/slog"
|
|
|
|
|
"os"
|
2024-04-26 11:36:27 +02:00
|
|
|
"runtime"
|
2023-12-15 14:50:40 +01:00
|
|
|
"runtime/debug"
|
2023-12-16 20:32:10 +01:00
|
|
|
|
2024-04-26 11:36:27 +02:00
|
|
|
"github.com/inconshreveable/mousetrap"
|
2023-12-16 20:32:10 +01:00
|
|
|
"github.com/lmittmann/tint"
|
2024-01-19 14:21:02 +01:00
|
|
|
"github.com/tlinden/yadu"
|
2023-12-14 19:00:04 +01:00
|
|
|
)
|
|
|
|
|
|
2023-12-15 14:50:40 +01:00
|
|
|
const LevelNotice = slog.Level(2)
|
|
|
|
|
|
2023-12-14 19:00:04 +01:00
|
|
|
func main() {
|
2024-01-01 20:53:05 +01:00
|
|
|
os.Exit(Main(os.Stdout))
|
2023-12-14 19:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-26 11:36:27 +02:00
|
|
|
func init() {
|
|
|
|
|
// if we're running on Windows AND if the user double clicked the
|
|
|
|
|
// exe file from explorer, we tell them and then wait until any
|
|
|
|
|
// key has been hit, which will make the cmd window disappear and
|
|
|
|
|
// thus give the user time to read it.
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
if mousetrap.StartedByExplorer() {
|
|
|
|
|
fmt.Println("Do no double click kleingebaeck.exe!")
|
|
|
|
|
fmt.Println("Please open a command shell and run it from there.")
|
|
|
|
|
fmt.Println()
|
|
|
|
|
fmt.Print("Press any key to quit: ")
|
2024-04-26 13:37:38 +02:00
|
|
|
_, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2024-04-26 11:36:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 19:03:34 +01:00
|
|
|
func Main(output io.Writer) int {
|
2023-12-15 14:50:40 +01:00
|
|
|
logLevel := &slog.LevelVar{}
|
|
|
|
|
opts := &tint.Options{
|
|
|
|
|
Level: logLevel,
|
|
|
|
|
AddSource: false,
|
2024-01-25 19:03:34 +01:00
|
|
|
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
|
2023-12-15 14:50:40 +01:00
|
|
|
// Remove time from the output
|
2024-01-25 19:03:34 +01:00
|
|
|
if attr.Key == slog.TimeKey {
|
2023-12-15 14:50:40 +01:00
|
|
|
return slog.Attr{}
|
|
|
|
|
}
|
2024-01-25 19:03:34 +01:00
|
|
|
|
|
|
|
|
return attr
|
2023-12-15 14:50:40 +01:00
|
|
|
},
|
2023-12-19 18:23:41 +01:00
|
|
|
NoColor: IsNoTty(),
|
2023-12-15 14:50:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logLevel.Set(LevelNotice)
|
2024-01-25 19:03:34 +01:00
|
|
|
|
|
|
|
|
handler := tint.NewHandler(output, opts)
|
2023-12-15 14:50:40 +01:00
|
|
|
logger := slog.New(handler)
|
2024-01-25 19:03:34 +01:00
|
|
|
|
2023-12-15 14:50:40 +01:00
|
|
|
slog.SetDefault(logger)
|
|
|
|
|
|
2024-01-25 19:03:34 +01:00
|
|
|
conf, err := InitConfig(output)
|
2023-12-19 18:23:41 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return Die(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if conf.Showversion {
|
2024-01-25 19:03:34 +01:00
|
|
|
fmt.Fprintf(output, "This is kleingebaeck version %s\n", VERSION)
|
|
|
|
|
|
2023-12-14 19:00:04 +01:00
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-19 18:23:41 +01:00
|
|
|
if conf.Showhelp {
|
2024-01-25 19:03:34 +01:00
|
|
|
fmt.Fprintln(output, Usage)
|
|
|
|
|
|
2023-12-15 14:50:40 +01:00
|
|
|
return 0
|
|
|
|
|
}
|
2023-12-14 19:00:04 +01:00
|
|
|
|
2023-12-19 18:23:41 +01:00
|
|
|
if conf.Showmanual {
|
2023-12-16 20:32:10 +01:00
|
|
|
err := man()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return Die(err)
|
|
|
|
|
}
|
2024-01-25 19:03:34 +01:00
|
|
|
|
2023-12-16 20:32:10 +01:00
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-19 18:23:41 +01:00
|
|
|
if conf.Verbose {
|
2023-12-15 14:50:40 +01:00
|
|
|
logLevel.Set(slog.LevelInfo)
|
|
|
|
|
}
|
2023-12-14 19:00:04 +01:00
|
|
|
|
2023-12-19 18:23:41 +01:00
|
|
|
if conf.Debug {
|
2023-12-15 14:50:40 +01:00
|
|
|
// we're using a more verbose logger in debug mode
|
|
|
|
|
buildInfo, _ := debug.ReadBuildInfo()
|
2024-01-19 14:21:02 +01:00
|
|
|
opts := &yadu.Options{
|
2023-12-15 14:50:40 +01:00
|
|
|
Level: logLevel,
|
|
|
|
|
AddSource: true,
|
2024-01-19 14:21:02 +01:00
|
|
|
//NoColor: IsNoTty(),
|
2023-12-14 19:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-15 14:50:40 +01:00
|
|
|
logLevel.Set(slog.LevelDebug)
|
2024-01-25 19:03:34 +01:00
|
|
|
|
|
|
|
|
handler := yadu.NewHandler(output, opts)
|
2023-12-15 14:50:40 +01:00
|
|
|
debuglogger := slog.New(handler).With(
|
|
|
|
|
slog.Group("program_info",
|
|
|
|
|
slog.Int("pid", os.Getpid()),
|
|
|
|
|
slog.String("go_version", buildInfo.GoVersion),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
slog.SetDefault(debuglogger)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slog.Debug("config", "conf", conf)
|
2023-12-14 19:00:04 +01:00
|
|
|
|
2023-12-15 14:50:40 +01:00
|
|
|
// prepare output dir
|
2024-02-10 14:36:53 +01:00
|
|
|
outdir, err := OutDirName(conf)
|
2023-12-15 14:50:40 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return Die(err)
|
|
|
|
|
}
|
2024-02-10 14:36:53 +01:00
|
|
|
conf.Outdir = outdir
|
|
|
|
|
|
2024-01-14 18:39:52 +01:00
|
|
|
// used for all HTTP requests
|
2024-01-24 19:19:31 +01:00
|
|
|
fetch, err := NewFetcher(conf)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return Die(err)
|
|
|
|
|
}
|
2024-01-14 18:39:52 +01:00
|
|
|
|
2024-02-10 14:06:06 +01:00
|
|
|
// setup ad dir registry, needed to check for duplicates
|
|
|
|
|
DirsVisited = make(map[string]int)
|
|
|
|
|
|
2024-01-25 19:03:34 +01:00
|
|
|
switch {
|
|
|
|
|
case len(conf.Adlinks) >= 1:
|
2023-12-19 18:23:41 +01:00
|
|
|
// directly backup ad listing[s]
|
|
|
|
|
for _, uri := range conf.Adlinks {
|
2024-01-16 19:27:46 +01:00
|
|
|
err := ScrapeAd(fetch, uri)
|
2023-12-15 14:50:40 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return Die(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-25 19:03:34 +01:00
|
|
|
case conf.User > 0:
|
2023-12-19 18:23:41 +01:00
|
|
|
// backup all ads of the given user (via config or cmdline)
|
2024-01-16 19:27:46 +01:00
|
|
|
err := ScrapeUser(fetch)
|
2023-12-15 14:50:40 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return Die(err)
|
|
|
|
|
}
|
2024-01-25 19:03:34 +01:00
|
|
|
default:
|
2023-12-19 18:23:41 +01:00
|
|
|
return Die(errors.New("invalid or no user id or no ad link specified"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if conf.StatsCountAds > 0 {
|
|
|
|
|
adstr := "ads"
|
2024-01-25 19:03:34 +01:00
|
|
|
|
2023-12-19 18:23:41 +01:00
|
|
|
if conf.StatsCountAds == 1 {
|
|
|
|
|
adstr = "ad"
|
|
|
|
|
}
|
2024-01-25 19:03:34 +01:00
|
|
|
|
|
|
|
|
fmt.Fprintf(output, "Successfully downloaded %d %s with %d images to %s.\n",
|
2023-12-19 18:23:41 +01:00
|
|
|
conf.StatsCountAds, adstr, conf.StatsCountImages, conf.Outdir)
|
|
|
|
|
} else {
|
2024-01-25 19:03:34 +01:00
|
|
|
fmt.Fprintf(output, "No ads found.")
|
2023-12-14 19:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Die(err error) int {
|
2023-12-15 14:50:40 +01:00
|
|
|
slog.Error("Failure", "error", err.Error())
|
2024-01-25 19:03:34 +01:00
|
|
|
|
2023-12-14 19:00:04 +01:00
|
|
|
return 1
|
|
|
|
|
}
|