diff --git a/Github Star.alfredworkflow b/Github Star.alfredworkflow new file mode 100644 index 0000000..eb51f76 Binary files /dev/null and b/Github Star.alfredworkflow differ diff --git a/src/github-stars.py b/src/github-stars.py new file mode 100644 index 0000000..91a5b77 --- /dev/null +++ b/src/github-stars.py @@ -0,0 +1,73 @@ +import os +import json +import requests +import time + +# Check for username +username = os.getenv('username') +if not username: + print(json.dumps({ + 'items': [{ + "arg": "https://www.alfredapp.com/help/workflows/advanced/variables/", + "title": "Please set your GitHub username first", + "subtitle": "Hit enter to open an introduction to variables." + }] + })) + exit(1) + +cache_path = os.getenv('alfred_workflow_cache') +cache_response = os.path.join(cache_path, 'cache.json') +cache_ttl = int(os.getenv('cache_ttl')) # in seconds + +# Check first if caching directory exists. +if not os.path.isdir(cache_path): + os.makedirs(cache_path) + +starred_url = f'https://api.github.com/users/{username}/starred' +http_status = 200 # default status code, so when using cache it doesn't run into error handling + +# Check if there is cache +# If not load stars from github API +if os.path.exists(cache_response) and os.path.getmtime(cache_response) > (time.time() - cache_ttl): + with open(cache_response, 'r') as f: + resp_json = json.load(f) +else: + response = requests.get(starred_url, headers={'User-Agent': 'GitHub Stars Alfred workflow for: ' + username}) + http_status = response.status_code + resp_json = response.json() + + # Cache response + if http_status == 200: + with open(cache_response, 'w') as f: + json.dump(resp_json, f, indent=4) + +items = [] +# Github API returned some sort of error. +# Also check for presence of `message` key, if HTTP Status +# code was not set to an error. +if http_status != 200 or 'message' in resp_json: + print(json.dumps({ + 'items': [{ + "arg": resp_json.get('documentation_url'), + "title": f"GitHub Response Error ({http_status})", + "subtitle": resp_json.get('message'), + }], + })) + exit(1) +# Search through the results. +for star in resp_json: + match = star['full_name'].replace('/', ' ').replace('-', ' ').replace('_', ' ') + items.append({ + 'type': 'default', + 'title': star['full_name'], + 'subtitle': f" ⭐ {star['stargazers_count']}, {star['description']}", + 'arg': f"https://www.github.com/{star['full_name']}", + 'autocomplete': star['full_name'], + 'icon': { + 'path': "./icon.png" + }, + 'match': match, + 'quicklookurl': f"https://www.github.com/{star['full_name']}" + }) + +print(json.dumps({"items": items}, indent=4)) diff --git a/src/icon.png b/src/icon.png new file mode 100644 index 0000000..dc68bdd Binary files /dev/null and b/src/icon.png differ diff --git a/src/info.plist b/src/info.plist new file mode 100644 index 0000000..c28a1fa --- /dev/null +++ b/src/info.plist @@ -0,0 +1,225 @@ + + + + + bundleid + com.yinanchen.alfred-github-star + connections + + 3D0D2645-4CAE-4B86-9676-C3953FA844F2 + + + destinationuid + 29D42A92-C63E-48DD-88EF-51F971691178 + modifiers + 1048576 + modifiersubtext + Copy URL to clipboard. + vitoclose + + + + destinationuid + 41CCB96E-8804-4939-ACEF-BE00D2DC015F + modifiers + 0 + modifiersubtext + + vitoclose + + + + destinationuid + FE20F637-87D5-43EE-880A-C6C7A7AD1DA5 + modifiers + 262144 + modifiersubtext + Copy git clone command to clipboard. + vitoclose + + + + + createdby + Yinan Chen + description + Search your starred repositories in Github + disabled + + name + Github Star + objects + + + config + + autopaste + + clipboardtext + {query} + ignoredynamicplaceholders + + transient + + + type + alfred.workflow.output.clipboard + uid + 29D42A92-C63E-48DD-88EF-51F971691178 + version + 3 + + + config + + alfredfiltersresults + + alfredfiltersresultsmatchmode + 0 + argumenttreatemptyqueryasnil + + argumenttrimmode + 0 + argumenttype + 1 + escaping + 102 + keyword + ghs + queuedelaycustom + 3 + queuedelayimmediatelyinitially + + queuedelaymode + 0 + queuemode + 1 + runningsubtext + fetching… + script + export PATH=/opt/homebrew/bin:/usr/local/bin:$PATH +export PATH="$HOME/.pyenv/shims:$HOME/.pyenv/bin:$PATH" + +python3 github-stars.py + scriptargtype + 1 + scriptfile + + skipuniversalaction + + subtext + Browse starred repositories + title + Github Stared Repo + type + 5 + withspace + + + type + alfred.workflow.input.scriptfilter + uid + 3D0D2645-4CAE-4B86-9676-C3953FA844F2 + version + 3 + + + config + + browser + + skipqueryencode + + skipvarencode + + spaces + + url + {query} + + type + alfred.workflow.action.openurl + uid + 41CCB96E-8804-4939-ACEF-BE00D2DC015F + version + 1 + + + config + + concurrently + + escaping + 127 + script + clone_url="git clone {query}.git" +osascript -e "set the clipboard to \"$clone_url\"" + scriptargtype + 0 + scriptfile + + type + 5 + + type + alfred.workflow.action.script + uid + FE20F637-87D5-43EE-880A-C6C7A7AD1DA5 + version + 2 + + + readme + Tested on Alfred 5. + +Variables +- username should be set to your GitHub username +- cache_ttl is the amount of times in seconds until the Workflow refetches your stars from GitHub. + +Set these to your prefered values in Variable panel {x} + uidata + + 29D42A92-C63E-48DD-88EF-51F971691178 + + xpos + 405 + ypos + 60 + + 3D0D2645-4CAE-4B86-9676-C3953FA844F2 + + xpos + 175 + ypos + 195 + + 41CCB96E-8804-4939-ACEF-BE00D2DC015F + + xpos + 405 + ypos + 195 + + FE20F637-87D5-43EE-880A-C6C7A7AD1DA5 + + xpos + 405 + ypos + 325 + + + userconfigurationconfig + + variables + + cache_ttl + 600 + username + ychen-97 + + version + 1.0 + webaddress + https://github.com/ychen-97/alfred-github-star + + diff --git a/src/prefs.plist b/src/prefs.plist new file mode 100644 index 0000000..0c67376 --- /dev/null +++ b/src/prefs.plist @@ -0,0 +1,5 @@ + + + + +