Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
yinan-c authored May 17, 2023
1 parent 304430b commit 2b5e33c
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 0 deletions.
Binary file added Github Star.alfredworkflow
Binary file not shown.
73 changes: 73 additions & 0 deletions src/github-stars.py
Original file line number Diff line number Diff line change
@@ -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))
Binary file added src/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
225 changes: 225 additions & 0 deletions src/info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>bundleid</key>
<string>com.yinanchen.alfred-github-star</string>
<key>connections</key>
<dict>
<key>3D0D2645-4CAE-4B86-9676-C3953FA844F2</key>
<array>
<dict>
<key>destinationuid</key>
<string>29D42A92-C63E-48DD-88EF-51F971691178</string>
<key>modifiers</key>
<integer>1048576</integer>
<key>modifiersubtext</key>
<string>Copy URL to clipboard.</string>
<key>vitoclose</key>
<false/>
</dict>
<dict>
<key>destinationuid</key>
<string>41CCB96E-8804-4939-ACEF-BE00D2DC015F</string>
<key>modifiers</key>
<integer>0</integer>
<key>modifiersubtext</key>
<string></string>
<key>vitoclose</key>
<false/>
</dict>
<dict>
<key>destinationuid</key>
<string>FE20F637-87D5-43EE-880A-C6C7A7AD1DA5</string>
<key>modifiers</key>
<integer>262144</integer>
<key>modifiersubtext</key>
<string>Copy git clone command to clipboard.</string>
<key>vitoclose</key>
<false/>
</dict>
</array>
</dict>
<key>createdby</key>
<string>Yinan Chen</string>
<key>description</key>
<string>Search your starred repositories in Github</string>
<key>disabled</key>
<false/>
<key>name</key>
<string>Github Star</string>
<key>objects</key>
<array>
<dict>
<key>config</key>
<dict>
<key>autopaste</key>
<false/>
<key>clipboardtext</key>
<string>{query}</string>
<key>ignoredynamicplaceholders</key>
<false/>
<key>transient</key>
<false/>
</dict>
<key>type</key>
<string>alfred.workflow.output.clipboard</string>
<key>uid</key>
<string>29D42A92-C63E-48DD-88EF-51F971691178</string>
<key>version</key>
<integer>3</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>alfredfiltersresults</key>
<true/>
<key>alfredfiltersresultsmatchmode</key>
<integer>0</integer>
<key>argumenttreatemptyqueryasnil</key>
<true/>
<key>argumenttrimmode</key>
<integer>0</integer>
<key>argumenttype</key>
<integer>1</integer>
<key>escaping</key>
<integer>102</integer>
<key>keyword</key>
<string>ghs</string>
<key>queuedelaycustom</key>
<integer>3</integer>
<key>queuedelayimmediatelyinitially</key>
<true/>
<key>queuedelaymode</key>
<integer>0</integer>
<key>queuemode</key>
<integer>1</integer>
<key>runningsubtext</key>
<string>fetching…</string>
<key>script</key>
<string>export PATH=/opt/homebrew/bin:/usr/local/bin:$PATH
export PATH="$HOME/.pyenv/shims:$HOME/.pyenv/bin:$PATH"
python3 github-stars.py</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
<string></string>
<key>skipuniversalaction</key>
<true/>
<key>subtext</key>
<string>Browse starred repositories</string>
<key>title</key>
<string>Github Stared Repo</string>
<key>type</key>
<integer>5</integer>
<key>withspace</key>
<false/>
</dict>
<key>type</key>
<string>alfred.workflow.input.scriptfilter</string>
<key>uid</key>
<string>3D0D2645-4CAE-4B86-9676-C3953FA844F2</string>
<key>version</key>
<integer>3</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>browser</key>
<string></string>
<key>skipqueryencode</key>
<false/>
<key>skipvarencode</key>
<false/>
<key>spaces</key>
<string></string>
<key>url</key>
<string>{query}</string>
</dict>
<key>type</key>
<string>alfred.workflow.action.openurl</string>
<key>uid</key>
<string>41CCB96E-8804-4939-ACEF-BE00D2DC015F</string>
<key>version</key>
<integer>1</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>concurrently</key>
<false/>
<key>escaping</key>
<integer>127</integer>
<key>script</key>
<string>clone_url="git clone {query}.git"
osascript -e "set the clipboard to \"$clone_url\""</string>
<key>scriptargtype</key>
<integer>0</integer>
<key>scriptfile</key>
<string></string>
<key>type</key>
<integer>5</integer>
</dict>
<key>type</key>
<string>alfred.workflow.action.script</string>
<key>uid</key>
<string>FE20F637-87D5-43EE-880A-C6C7A7AD1DA5</string>
<key>version</key>
<integer>2</integer>
</dict>
</array>
<key>readme</key>
<string>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}</string>
<key>uidata</key>
<dict>
<key>29D42A92-C63E-48DD-88EF-51F971691178</key>
<dict>
<key>xpos</key>
<real>405</real>
<key>ypos</key>
<real>60</real>
</dict>
<key>3D0D2645-4CAE-4B86-9676-C3953FA844F2</key>
<dict>
<key>xpos</key>
<real>175</real>
<key>ypos</key>
<real>195</real>
</dict>
<key>41CCB96E-8804-4939-ACEF-BE00D2DC015F</key>
<dict>
<key>xpos</key>
<real>405</real>
<key>ypos</key>
<real>195</real>
</dict>
<key>FE20F637-87D5-43EE-880A-C6C7A7AD1DA5</key>
<dict>
<key>xpos</key>
<real>405</real>
<key>ypos</key>
<real>325</real>
</dict>
</dict>
<key>userconfigurationconfig</key>
<array/>
<key>variables</key>
<dict>
<key>cache_ttl</key>
<string>600</string>
<key>username</key>
<string>ychen-97</string>
</dict>
<key>version</key>
<string>1.0</string>
<key>webaddress</key>
<string>https://github.com/ychen-97/alfred-github-star</string>
</dict>
</plist>
5 changes: 5 additions & 0 deletions src/prefs.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
</plist>

0 comments on commit 2b5e33c

Please sign in to comment.