-
Notifications
You must be signed in to change notification settings - Fork 2
/
twitter-hashflags.go
114 lines (101 loc) · 3.24 KB
/
twitter-hashflags.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"flag"
"fmt"
"github.com/gookit/color"
"github.com/trevorsmiley/fileutils"
"log"
"os"
"path/filepath"
"twitter-hashflags/hashflag"
"twitter-hashflags/twitter"
)
const (
hashflagDIR = "downloaded_hashflags"
deactivatedDIR = "deactivated"
detailsFile = "hashflag-list.txt"
htmlFile = "hashflags.html"
)
var cyan = color.FgCyan.Render
var green = color.FgGreen.Render
var red = color.FgRed.Render
var yellow = color.FgYellow.Render
func main() {
//list
listCommand := flag.NewFlagSet("info", flag.ExitOnError)
fullDetails := listCommand.Bool("out", false, "write full details to file")
fileDiff := listCommand.Bool("diff", false, "diff")
deactivated := listCommand.Bool("deactivated", false, "deactivated")
htmlPage := listCommand.Bool("html", false, "create local html page")
//sync
syncCommand := flag.NewFlagSet("sync", flag.ExitOnError)
moveInactive := syncCommand.Bool("m", false, "move deactivated")
force := syncCommand.Bool("force", false, "force download")
switch os.Args[1] {
case "info":
err := listCommand.Parse(os.Args[2:])
if err != nil {
log.Fatal(err)
}
case "sync":
err := syncCommand.Parse(os.Args[2:])
if err != nil {
log.Fatal(err)
}
default:
printHelp()
}
err := fileutils.CreateDirIfMissing(hashflagDIR)
if err != nil {
log.Fatalf("Error creating folder %s\n%v", hashflagDIR, err)
}
if listCommand.Parsed() {
hashflag.Info(getHashflags(), hashflagDIR, detailsFile, htmlFile, *fullDetails, *deactivated, *fileDiff, *htmlPage)
}
if syncCommand.Parsed() {
hashflag.Sync(getHashflags(), hashflagDIR, deactivatedDIR, *moveInactive, *force)
}
}
func printHelp() {
opList := []struct {
op string
desc string
flag bool
}{
{"info", "Print a list of all active hashflags", false},
{"-out", color.Sprintf("Write a list of all active hashflags with hashtags to %s", green(detailsFile)), true},
{"-html", color.Sprintf("Generate an html file (%s) to view all active hashflags directly from Twitter", green(htmlFile)), true},
{"-diff", color.Sprintf("List only hashflags missing in %s", green(hashflagDirPath())), true},
{"-deactivated", color.Sprintf("List only deactivated files that are in %s", green(hashflagDirPath())), true},
{"sync", color.Sprintf("Download all missing hashflags to %s", green(hashflagDirPath())), false},
{"-m", color.Sprintf(
"Move all deactivated files in %s to %s and download all missing hashflags to %s",
red(hashflagDirPath()),
red(dirPath(filepath.Join(hashflagDIR, deactivatedDIR))),
green(hashflagDirPath()),
), true},
{"-force", color.Sprintf("<red>Clear directory</> %s and download all active hashflags", red(hashflagDirPath())), true},
}
fmt.Println("Available commands:")
for _, op := range opList {
opText := cyan(op.op)
if op.flag {
opText = color.Sprintf("\t%s", yellow(op.op))
}
color.Printf("\t%s: %s\n", opText, op.desc)
}
}
func getHashflags() []hashflag.Hashflag {
hashflags, err := twitter.GetHashflagsFromTwitter()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found %s active hashflags\n\n", cyan(len(hashflags)))
return hashflags
}
func hashflagDirPath() string {
return dirPath(hashflagDIR)
}
func dirPath(dir string) string {
return fmt.Sprintf("%s%s", string(os.PathSeparator), dir)
}