-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (73 loc) · 2.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
)
func main() {
folderInPtr := flag.String("inDir", ".", "The folder with .torrents. Default current dir")
folderOutPtr := flag.String("outDir", ".", "The folder for output. Default current dir")
feedNamePtr := flag.String("filename", "feed.xml", "Filename without directory, for torrent feed. Default feed.xml")
baseURLPtr := flag.String("baseUrl", "", "The baseURL which the torrents can be downloaded from. Default null.")
flag.Parse()
// read folder In
folderIn := filepath.Dir(*folderInPtr)
folderOut := filepath.Dir(*folderOutPtr)
feedName := *feedNamePtr
baseURL := *baseURLPtr
if *baseURLPtr == "" {
log.Fatal("Missing baseURL")
os.Exit(-1)
}
// output buffer
var outBuffer bytes.Buffer // A Buffer needs no initialization.
// head of xml feed
outBuffer.Write([]byte(`
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Zibo Mod Files</title>
<link>https://forums.x-plane.org/index.php?/forums/topic/185685-zibo-install-guide-training-checklist/</link>
<description>Zibo Mod Install Guide, Training Checklist and Updates</description>
<atom:link href="https://1drv.ms/u/s!AjcwFonqlaRWgYdsjr2JbyxDPdcCvA?e=OENfhN" rel="self" type="application/rss+xml" />
`))
fmt.Printf("Running with options\n inDir= %s\n outDir= %s\n filename= %s\n baseUrl= %s\n", folderIn, folderOut, feedName, baseURL)
fmt.Println("\nScanning directory " + folderIn + "\n")
err := filepath.Walk(folderIn, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && (filepath.Ext(path) == ".torrent" || filepath.Ext(path) == ".pdf") {
fmt.Printf("\t%s/%s\n", folderIn, path)
title := fmt.Sprint("\t<title>", info.Name(), "</title>\n")
pubDate := fmt.Sprint("\t<pubDate>", info.ModTime(), "</pubDate>\n")
url, err := url.Parse(baseURL + "/" + info.Name())
if err != nil {
log.Fatal("BaseURL in wrong format", err)
}
urlStr := fmt.Sprint("\t<link>", url.String(), "</link>\n")
description := "\t<description />\n"
outBuffer.Write([]byte("<item>\n"))
outBuffer.Write([]byte(title))
outBuffer.Write([]byte(description))
outBuffer.Write([]byte(urlStr))
outBuffer.Write([]byte(pubDate))
outBuffer.Write([]byte("</item>\n"))
}
return nil
})
if err != nil {
log.Fatal(err)
}
outBuffer.Write([]byte(`
</channel>
</rss>`))
file := folderOut + "/" + feedName
ioutil.WriteFile(file, outBuffer.Bytes(), 0644)
fmt.Printf("Writing %s ", file)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Done")
}