-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
202 lines (174 loc) · 5.93 KB
/
client.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"errors"
"flag"
"fmt"
"github.com/hashworks/xRELTerminalClient/src/xrel"
"github.com/hashworks/xRELTerminalClient/src/xrel/types"
"os"
"strings"
)
const (
// 2006-01-02 15:04:05.999999999 -0700 MST
xRELCommentTimeFormat = "02. Jan 2006, 03:04 pm"
xRELReleaseTimeFormat = "02.01.2006 03:04 pm"
)
var (
// Set the following uppercase three with -ldflags "-X main.VERSION=v1.2.3 [...]"
VERSION string = "unknown"
OAUTH_CONSUMER_KEY string
OAUTH_CONSUMER_SECRET string
versionFlag bool
configFilePathFlag string
authenticateFlag bool
checkRateLimitFlag bool
isP2PFlag bool
perPageFlag int
pageFlag int
getFiltersFlag bool
filterFlag string
latestFlag bool
browseArchiveFlag string
getCategoriesFlag bool
extInfoTypeFlag string
browseCategoryFlag string
infoFlag bool
imagesFlag bool
videosFlag bool
addFavEntryFlag bool
rateFlag int
limitFlag int
releasesFlag bool
searchExtInfoFlag string
searchReleaseFlag string
rateVideoFlag int
rateAudioFlag int
addCommentFlag string
releaseFlag string
commentsFlag string
upcomingTitlesFlag bool
rmFavEntryFlag bool
listFavEntriesFlag bool
)
func main() {
flagSet := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flagSet.Usage = Usage
flagSet.BoolVar(&versionFlag, "version", false, "")
flagSet.StringVar(&configFilePathFlag, "configFile", "", "")
flagSet.BoolVar(&authenticateFlag, "authenticate", false, "")
flagSet.BoolVar(&checkRateLimitFlag, "rateLimit", false, "")
flagSet.BoolVar(&isP2PFlag, "p2p", false, "")
flagSet.IntVar(&perPageFlag, "perPage", 5, "")
flagSet.IntVar(&pageFlag, "page", 0, "")
flagSet.BoolVar(&getFiltersFlag, "filters", false, "")
flagSet.StringVar(&filterFlag, "filter", "", "")
flagSet.BoolVar(&latestFlag, "latest", false, "")
flagSet.StringVar(&browseArchiveFlag, "browseArchive", "", "")
flagSet.StringVar(&extInfoTypeFlag, "mediaType", "", "")
flagSet.BoolVar(&getCategoriesFlag, "categories", false, "")
flagSet.StringVar(&browseCategoryFlag, "browseCategory", "", "")
flagSet.BoolVar(&imagesFlag, "images", false, "")
flagSet.BoolVar(&videosFlag, "videos", false, "")
flagSet.BoolVar(&infoFlag, "info", false, "")
flagSet.IntVar(&rateFlag, "rate", 0, "")
flagSet.BoolVar(&addFavEntryFlag, "addToFavorites", false, "")
flagSet.IntVar(&limitFlag, "limit", 0, "")
flagSet.BoolVar(&releasesFlag, "releases", false, "")
flagSet.StringVar(&searchExtInfoFlag, "searchMedia", "", "")
flagSet.StringVar(&searchReleaseFlag, "searchRelease", "", "")
flagSet.IntVar(&rateVideoFlag, "rateVideo", 0, "")
flagSet.IntVar(&rateAudioFlag, "rateAudio", 0, "")
flagSet.StringVar(&addCommentFlag, "addComment", "", "")
flagSet.StringVar(&releaseFlag, "release", "", "")
flagSet.StringVar(&commentsFlag, "comments", "", "")
flagSet.BoolVar(&upcomingTitlesFlag, "upcomingTitles", false, "")
flagSet.BoolVar(&listFavEntriesFlag, "showUnreadFavorites", false, "")
flagSet.BoolVar(&rmFavEntryFlag, "removeFavoriteEntry", false, "")
flagSet.Parse(os.Args[1:])
if limitFlag == 0 && perPageFlag != 0 {
limitFlag = perPageFlag
}
_ = readConfig(configFilePathFlag)
xrel.SetOAuthConsumerKeyAndSecret(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET)
switch {
case versionFlag:
fmt.Println("xREL Terminal Client")
fmt.Println("https://github.com/hashworks/xRELTerminalClient")
fmt.Println("Version: " + VERSION)
fmt.Println()
fmt.Println("Published under the GNU General Public License v3.0.")
case rmFavEntryFlag:
removeFavEntry()
case listFavEntriesFlag:
showFavEntries()
case upcomingTitlesFlag:
showUpcomingTitles(releasesFlag, isP2PFlag)
case releaseFlag != "":
if addCommentFlag != "" || rateVideoFlag != 0 || rateAudioFlag != 0 {
if (rateVideoFlag != 0 && rateAudioFlag == 0) || (rateVideoFlag == 0 && rateAudioFlag != 0) {
fmt.Println("You need to set either both or none of --rateVideo and --rateAudio.")
os.Exit(2)
} else {
addComment(releaseFlag, isP2PFlag, addCommentFlag, rateVideoFlag, rateAudioFlag)
}
} else {
showRelease(releaseFlag, isP2PFlag)
}
case searchReleaseFlag != "":
searchReleases(searchReleaseFlag, isP2PFlag, limitFlag)
case searchExtInfoFlag != "":
searchMedia(searchExtInfoFlag, extInfoTypeFlag, perPageFlag, pageFlag, limitFlag, isP2PFlag, infoFlag, releasesFlag, imagesFlag, videosFlag, addFavEntryFlag, rateFlag, browseArchiveFlag)
case getCategoriesFlag:
showCategories(isP2PFlag)
case getFiltersFlag:
showFilters(isP2PFlag)
case latestFlag:
showLatest(filterFlag, isP2PFlag, perPageFlag, perPageFlag)
case browseArchiveFlag != "":
browseArchive(filterFlag, browseArchiveFlag, isP2PFlag, perPageFlag, pageFlag)
case browseCategoryFlag != "":
browseCategory(browseCategoryFlag, extInfoTypeFlag, isP2PFlag, perPageFlag, pageFlag)
case commentsFlag != "":
showComments(commentsFlag, isP2PFlag, perPageFlag, pageFlag)
case checkRateLimitFlag:
checkRateLimit()
case authenticateFlag:
authenticate()
default:
flagSet.Usage()
}
ok(writeConfig(), "\nFailed to write the configuration to file system:\n")
}
func ok(err error, prefix string) {
if err != nil {
fmt.Println(prefix + err.Error())
os.Exit(1)
}
}
func findP2PCategoryID(categoryName string) (string, error) {
var (
categoryID string
err error
categories []types.P2PCategory
)
categories, err = xrel.GetP2PCategories()
if err == nil {
for i := 0; i < len(categories); i++ {
category := categories[i]
if category.SubCat != "" {
if strings.ToLower(category.SubCat) == strings.ToLower(categoryName) {
categoryID = category.Id
}
} else if strings.ToLower(category.MetaCat) == strings.ToLower(categoryName) {
categoryID = category.Id
}
if categoryID != "" {
break
}
}
if categoryID == "" {
err = errors.New("Category not found. Please choose one of --categories --p2p.")
}
}
return categoryID, err
}