Skip to content

Commit

Permalink
Use go-xREL-API v2.1.0, add --listName, --showUnreadFavorites now onl…
Browse files Browse the repository at this point in the history
…y shows entries with unread releases
  • Loading branch information
hashworks committed May 8, 2016
1 parent 4aa3aa7 commit a56b55d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 30 deletions.
10 changes: 8 additions & 2 deletions README.md
Expand Up @@ -76,7 +76,9 @@ Function flags:
See --searchRelease.
--addToFavorites
Add selected media to a favorites list you select.
Requires authentication.
Requires authentication. Optional parameters:
--listName Games
Select a specific favorites list.
--info
Show information about the selected media.
Only usefull if used with the following parameters.
Expand All @@ -92,12 +94,16 @@ Function flags:
--showUnreadFavorites
Select a user's favorite list and show unread releases.
Requires authentication. Optional parameters:
--listName Games
Display only a specific list.
--markAsRead
Marks entries as read.
--removeFavoriteEntry
Select a user's favorite list and remove an entry.
Requires authentication.
Requires authentication. Optional parameters:
--listName Games
Select a specific list.
--latest
Lists latest releases. Optional parameters:
Expand Down
14 changes: 8 additions & 6 deletions src/client.go
Expand Up @@ -48,8 +48,9 @@ var (
addProofFlag string
upcomingTitlesFlag bool
countryFlag string
favListNameFlag string
rmFavEntryFlag bool
listFavEntriesFlag bool
listUnreadFavEntriesFlag bool
markFavEntriesAsReadFlag bool
)

Expand Down Expand Up @@ -104,7 +105,8 @@ func main() {
flagSet.BoolVar(&upcomingTitlesFlag, "upcomingTitles", false, "")
flagSet.StringVar(&countryFlag, "country", "", "")

flagSet.BoolVar(&listFavEntriesFlag, "showUnreadFavorites", false, "")
flagSet.BoolVar(&listUnreadFavEntriesFlag, "showUnreadFavorites", false, "")
flagSet.StringVar(&favListNameFlag, "listName", "", "")
flagSet.BoolVar(&markFavEntriesAsReadFlag, "markAsRead", false, "")
flagSet.BoolVar(&rmFavEntryFlag, "removeFavoriteEntry", false, "")

Expand All @@ -126,9 +128,9 @@ func main() {
fmt.Println()
fmt.Println("Published under the GNU General Public License v3.0.")
case rmFavEntryFlag:
removeFavEntry()
case listFavEntriesFlag:
showFavEntries(markFavEntriesAsReadFlag)
removeFavEntry(favListNameFlag)
case listUnreadFavEntriesFlag:
showUnreadFavEntries(favListNameFlag, markFavEntriesAsReadFlag)
case upcomingTitlesFlag:
showUpcomingTitles(countryFlag, releasesFlag, isP2PFlag)
case releaseFlag != "":
Expand All @@ -151,7 +153,7 @@ func main() {
case searchReleaseFlag != "":
searchReleases(searchReleaseFlag, isP2PFlag, limitFlag)
case searchExtInfoFlag != "":
searchMedia(searchExtInfoFlag, extInfoTypeFlag, perPageFlag, pageFlag, limitFlag, isP2PFlag, infoFlag, releasesFlag, imagesFlag, videosFlag, addFavEntryFlag, rateFlag, browseArchiveFlag)
searchMedia(searchExtInfoFlag, extInfoTypeFlag, perPageFlag, pageFlag, limitFlag, isP2PFlag, infoFlag, releasesFlag, imagesFlag, videosFlag, addFavEntryFlag, rateFlag, browseArchiveFlag, favListNameFlag)
case getCategoriesFlag:
showCategories(isP2PFlag)
case getFiltersFlag:
Expand Down
39 changes: 27 additions & 12 deletions src/favs.go
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
)

func selectFavList(selectPrefix string) (int, error) {
func selectFavList(selectPrefix, name string) (int, error) {
var (
id int
err error
Expand All @@ -24,6 +24,18 @@ func selectFavList(selectPrefix string) (int, error) {
favListCount := len(favLists)
if favListCount == 0 {
err = errors.New("You have no favorites lists.")
} else if name != "" {
favListFound := false
for i := 0; i < favListCount; i++ {
favList := favLists[i]
if favList.Name == name {
id = favList.ID
favListFound = true
}
}
if !favListFound {
err = errors.New("Favlist '" + name + "' not found.")
}
} else if favListCount > 1 {
for i := 0; i < favListCount; i++ {
favList := favLists[i]
Expand All @@ -47,16 +59,16 @@ func selectFavList(selectPrefix string) (int, error) {
return id, err
}

func addEntryToFavList(extInfoId string) {
id, err := selectFavList("Please choose the list you want to add an entry to: ")
func addEntryToFavList(extInfoId string, favListName string) {
id, err := selectFavList("Please choose the list you want to add an entry to: ", favListName)
ok(err, "Failed to get your favorites lists: ")
result, err := xrel.AddFavsListEntry(strconv.Itoa(id), extInfoId)
ok(err, "Failed to add entry: ")
fmt.Println("Sucessfully added \"" + result.ExtInfo.Title + "\".")
}

func removeFavEntry() {
id, err := selectFavList("Please choose the list you want to remove an entry from: ")
func removeFavEntry(favListName string) {
id, err := selectFavList("Please choose the list you want to remove an entry from: ", "")
ok(err, "Failed to get your favorites lists: ")
favListEntries, err := xrel.GetFavsListEntries(strconv.Itoa(id), false)
ok(err, "Failed to get favorites list entries: ")
Expand Down Expand Up @@ -94,8 +106,8 @@ func removeFavEntry() {
}
}

func showFavEntries(markAsRead bool) {
id, err := selectFavList("")
func showUnreadFavEntries(favListName string, markAsRead bool) {
id, err := selectFavList("", favListName)
ok(err, "Failed to get your favorites lists: ")
idStr := strconv.Itoa(id)
favListEntries, err := xrel.GetFavsListEntries(idStr, true)
Expand All @@ -105,14 +117,14 @@ func showFavEntries(markAsRead bool) {
fmt.Println("You have no favorites list entries on this list.")
os.Exit(1)
} else {
receivedUnreadReleases := false
for i := 0; i < favListEntriesCount; i++ {
entry := favListEntries[i]
fmt.Println(entry.Title + " [" + strings.ToUpper(entry.Type) + "]")
releaseCount := len(entry.Releases)
p2pReleaseCount := len(entry.P2PReleases)
if releaseCount == 0 && p2pReleaseCount == 0 {
fmt.Println("\tNo new releases.")
} else {
if releaseCount != 0 || p2pReleaseCount != 0 {
receivedUnreadReleases = true
fmt.Println(entry.Title + " [" + strings.ToUpper(entry.Type) + "]")
for i := 0; i < releaseCount; i++ {
release := entry.Releases[i]
if markAsRead {
Expand All @@ -127,8 +139,11 @@ func showFavEntries(markAsRead bool) {
}
fmt.Println("\t[P2P] " + release.Dirname + " (" + release.LinkURL + ")")
}
fmt.Println()
}
fmt.Println()
}
if !receivedUnreadReleases {
fmt.Println("No unread releases.")
}
}
}
4 changes: 2 additions & 2 deletions src/search.go
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
)

func searchMedia(query, extInfoType string, perPage, page, limit int, isP2P, showInfo, showReleases, showImages, showVideos, addFavEntry bool, rateMedia int, category string) {
func searchMedia(query, extInfoType string, perPage, page, limit int, isP2P, showInfo, showReleases, showImages, showVideos, addFavEntry bool, rateMedia int, category, favListName string) {
extInfoType = strings.ToLower(extInfoType)
results, err := xrel.SearchExtInfos(query, extInfoType, limit)
ok(err, "Failed to search for media: ")
Expand Down Expand Up @@ -44,7 +44,7 @@ func searchMedia(query, extInfoType string, perPage, page, limit int, isP2P, sho
id = results.Results[0].ID
}
if addFavEntry {
addEntryToFavList(id)
addEntryToFavList(id, favListName)
} else {
outputExtInfoData(id, perPage, page, isP2P, showInfo, showReleases, showImages, showVideos, rateMedia, category)
}
Expand Down
10 changes: 8 additions & 2 deletions src/usage.go
Expand Up @@ -74,7 +74,9 @@ func Usage() {
fmt.Println(" See --searchRelease.")
fmt.Println(" --addToFavorites")
fmt.Println(" Add selected media to a favorites list you select.")
fmt.Println(" Requires authentication.")
fmt.Println(" Requires authentication. Optional parameters:")
fmt.Println(" --listName Games")
fmt.Println(" Select a specific favorites list.")
fmt.Println(" --info")
fmt.Println(" Show information about the selected media.")
fmt.Println(" Only usefull if used with the following parameters.")
Expand All @@ -90,12 +92,16 @@ func Usage() {
fmt.Println("--showUnreadFavorites")
fmt.Println(" Select a user's favorite list and show unread releases.")
fmt.Println(" Requires authentication.")
fmt.Println(" --listName Games")
fmt.Println(" Display only a specific list.")
fmt.Println(" --markAsRead")
fmt.Println(" Marks entries as read.")
fmt.Println("")
fmt.Println("--removeFavoriteEntry")
fmt.Println(" Select a user's favorite list and remove an entry.")
fmt.Println(" Requires authentication.")
fmt.Println(" Requires authentication. Optional parameters:")
fmt.Println(" --listName Games")
fmt.Println(" Select a specific list.")
fmt.Println("")
fmt.Println("--latest")
fmt.Println(" Lists latest releases. Optional parameters:")
Expand Down
18 changes: 12 additions & 6 deletions src/user.go
Expand Up @@ -10,25 +10,31 @@ import (
func authenticate() {
authenticated := false

if types.Config.OAuth2Token != nil {
data, err := xrel.GetUserInfo()
if err == nil {
data, err := xrel.GetUserInfo()
if err == nil {
if data.Name == "" {
fmt.Println("Received no error, but failed to test authentication.")
} else {
fmt.Println("You're already authenticated, " + data.Name + ".")
authenticated = true
}
}
if !authenticated {
url := xrel.GetOAuth2RequestURL()
url := xrel.GetOAuth2AuthURL("")
fmt.Println("(1) Go to: " + url)
fmt.Println("(2) Grant access, you should get back a verification code.")
fmt.Print("(3) Enter that verification code here: ")
verificationCode := ""
fmt.Scanln(&verificationCode)
err := xrel.InitiateOAuth2CodeExchange(verificationCode)
err := xrel.PerformOAuth2UserAuthentication(verificationCode)
ok(err, "Failed to authenticate using oAuth2: ")
data, err := xrel.GetUserInfo()
ok(err, "Failed to test authentication: ")
fmt.Println("Authentication sucessfull, " + data.Name + ".")
if data.Name == "" {
fmt.Println("Received no error, but failed test authentication.")
} else {
fmt.Println("Authentication sucessfull, " + data.Name + ".")
}
}
}

Expand Down

0 comments on commit a56b55d

Please sign in to comment.