Skip to content

Commit

Permalink
release v0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Wayneoween committed Apr 22, 2019
1 parent ec9b12a commit 3fcfd9b
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.upx
.DS_Store
alfred_ddb
alfred-dndbeyond-monster-workflow
workflow-install.py
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

Version 0.3 – 2019-04-22
------------------------

### Bugfixes

* [Implement Auto-Update](https://github.com/Wayneoween/alfred-dndbeyond-monster-workflow/issues/4)

Version 0.2 – 2019-04-22
------------------------

Expand Down
140 changes: 140 additions & 0 deletions alfred-dndbeyond-monster-workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright (c) 2019 Marius Schuller <code@marius-schuller.de>
// MIT Licence - http://opensource.org/licenses/MIT

/*
This Alfred Workflow queries the dndbeyond general search with the monster filter
and displaoys the first 10 matches.
Pressing Enter on each entry uses the default browser to open the monster page on dndbeyond.com
*/
package main

import (
"flag"
"log"
"os"
"os/exec"

aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/update"
"github.com/gocolly/colly"
"github.com/gocolly/colly/extensions"
)

// Name of the background job that checks for updates
const updateJobName = "checkForUpdate"

var (
doCheck bool
baseurl = "https://www.dndbeyond.com"
helpURL = "https://marius-schuller.de"
maxResults = 10
url = baseurl + "/search?f=monsters&c=monsters&q=" // ddb search url
iconAvailable = &aw.Icon{Value: "update-available.png"}
repo = "Wayneoween/alfred-dndbeyond-monster-workflow" // GitHub repo
wf *aw.Workflow // Our Workflow object
)

type monster struct {
MonsterName string
MonsterURL string
}

func init() {
flag.BoolVar(&doCheck, "check", false, "check for a new version")
// Create a new *Workflow using default configuration
// (workflow settings are read from the environment variables
// set by Alfred)
wf = aw.New(aw.HelpURL(helpURL), aw.MaxResults(maxResults), update.GitHub(repo))
}

func run() {
var query string
monsters := []monster{}
wf.Args() // call to handle magic actions
flag.Parse()

// Use wf.Args() to enable Magic Actions
if args := wf.Args(); len(args) > 0 {
query = args[0]
}

if doCheck {
wf.Configure(aw.TextErrors(true))
log.Println("Checking for updates...")
if err := wf.CheckForUpdate(); err != nil {
wf.FatalError(err)
}
return
}

if wf.UpdateCheckDue() && !wf.IsRunning(updateJobName) {
log.Println("Running update check in background...")

cmd := exec.Command(os.Args[0], "-check")
if err := wf.RunInBackground(updateJobName, cmd); err != nil {
log.Printf("Error starting update check: %s", err)
}
}

// Only show update status if query is empty.
if query == "" && wf.UpdateAvailable() {
// Turn off UIDs to force this item to the top.
// If UIDs are enabled, Alfred will apply its "knowledge"
// to order the results based on your past usage.
wf.Configure(aw.SuppressUIDs(true))

// Notify user of update. As this item is invalid (Valid(false)),
// actioning it expands the query to the Autocomplete value.
// "workflow:update" triggers the updater Magic Action that
// is automatically registered when you configure Workflow with
// an Updater.
//
// If executed, the Magic Action downloads the latest version
// of the workflow and asks Alfred to install it.
wf.NewItem("Update available!").
Subtitle("↩ to install").
Autocomplete("workflow:update").
Valid(false).
Icon(iconAvailable)
}

log.Printf("[main] query=%s", query)

// Instantiate default collector
c := colly.NewCollector(
// Visit only domains: old.reddit.com
colly.AllowURLRevisit(),
colly.Async(true),
)

extensions.RandomUserAgent(c)

c.OnHTML(".ddb-search-results-listing-item-header-primary-text", func(e *colly.HTMLElement) {
temp := monster{}
temp.MonsterName = e.ChildText("a")
temp.MonsterURL = e.ChildAttr("a", "href")
monsters = append(monsters, temp)
wf.NewItem(temp.MonsterName).Subtitle(baseurl + temp.MonsterURL).Arg(baseurl + temp.MonsterURL).UID(temp.MonsterName + temp.MonsterURL).Valid(true)
log.Println("MonsterName ", temp.MonsterName)
log.Println("MonsterURL ", baseurl+temp.MonsterURL)
})

c.Visit(url + query)

c.Wait()

log.Println(monsters)

if len(monsters) == 0 {
wf.WarnEmpty("Nothing found.", "Try another name.")
}

// And send the results to Alfred
wf.SendFeedback()
}

func main() {
// Wrap your entry point with Run() to catch and log panics and
// show an error in Alfred instead of silently dying
wf.Run(run)
}
88 changes: 0 additions & 88 deletions alfred_ddb.go

This file was deleted.

11 changes: 9 additions & 2 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<key>runningsubtext</key>
<string>Loading...</string>
<key>script</key>
<string>./alfred_ddb "$1"</string>
<string>./alfred-dndbeyond-monster-workflow "$1"</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
Expand Down Expand Up @@ -103,6 +103,13 @@
<string>Changelog
=========
Version 0.3 – 2019-04-22
------------------------
### Bugfixes
* [Implement Auto-Update](https://github.com/Wayneoween/alfred-dndbeyond-monster-workflow/issues/4)
Version 0.2 – 2019-04-22
------------------------
Expand Down Expand Up @@ -140,7 +147,7 @@ Initial release \o/
</dict>
</dict>
<key>version</key>
<string>0.2</string>
<string>0.3</string>
<key>webaddress</key>
<string>https://marius-schuller.de</string>
</dict>
Expand Down
Binary file added update-available.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3fcfd9b

Please sign in to comment.