Skip to content

Commit

Permalink
Merge pull request #6 from lampo/python3-conversion
Browse files Browse the repository at this point in the history
Convert alfred workflow to using python3
  • Loading branch information
Jake Crews authored Apr 13, 2022
2 parents 3e9c0fc + 0b69eb8 commit 43637ab
Show file tree
Hide file tree
Showing 8 changed files with 963 additions and 1,516 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
**/*.pyc
.vscode/
lib/*
!lib/.gitkeep
!lib/.gitkeep

.config
.irb_history
35 changes: 21 additions & 14 deletions github.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# encoding: utf-8
import sys
import re
from workflow import Workflow3, ICON_WEB, web, ICON_ERROR
from workflow import Workflow3, ICON_ERROR
from urllib.request import Request
from urllib import request
from json.decoder import JSONDecoder

log = None

Expand All @@ -16,14 +19,18 @@ def get_token(wf):
)
wf.send_feedback()
sys.exit(1)
return token
return token.decode('utf-8')


def request(token, url='https://api.github.com/user/repos'):
r = web.get(url, headers={'Authorization': 'token %s' % token})
r.raise_for_status()
return r
def repos_request(token, url='https://api.github.com/user/repos'):
request_with_header = Request(url, headers={'Authorization': 'token %s' % token})
return request.urlopen(request_with_header)

def decoded_repos_request(token, url='https://api.github.com/user/repos'):
request_with_header = Request(url, headers={'Authorization': 'token %s' % token})
f = request.urlopen(request_with_header)

return f.read().decode('utf-8')

def get_last(r):
link = r.headers.get('link')
Expand All @@ -42,22 +49,22 @@ def get_all_urls(r):
inclusive_last = last + 1
return ['https://api.github.com/user/repos?page=%d' % page for page in range(2, inclusive_last)]


def get_all(token):
from concurrent.futures import ThreadPoolExecutor, as_completed
r = request(token)
repos = r.json()
r = repos_request(token)
repos = JSONDecoder().decode(r.read().decode('utf-8'))
urls = get_all_urls(r)
pool = ThreadPoolExecutor(20)
futures = [pool.submit(request,token,url) for url in urls]
futures = [pool.submit(decoded_repos_request,token,url) for url in urls]
results = [r.result() for r in as_completed(futures)]
for result in results:
repos += result.json()
repos += JSONDecoder().decode(result)
return repos


def get_cached_repos(wf):
return wf.stored_data('repos')
repos = wf.stored_data('repos')
return repos


def load_repos(wf, token):
Expand All @@ -79,7 +86,7 @@ def main(wf):
# TODO provide helper to take them to documentation to get api token
# configured correctly
token = args[1]
wf.store_data('token', token)
wf.store_data('token', token.encode())
load_repos(wf, token)
return

Expand All @@ -91,7 +98,7 @@ def main(wf):

repos = get_repos(wf, token)
if args:
repos = wf.filter(args[0], repos, lambda repo: repo['full_name'])
repos = wf.filter(args[0], repos, lambda repo: repo.get('full_name'))

if not repos:
wf.warn_empty('No repos found. Refresh repos or try a different query.')
Expand Down
16 changes: 9 additions & 7 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>bundleid</key>
<string>com.matthewmcg.github</string>
<string>com.ramsey.github</string>
<key>connections</key>
<dict>
<key>089FEA1F-952B-4635-91C6-24861402B336</key>
Expand Down Expand Up @@ -132,7 +132,7 @@
</array>
</dict>
<key>createdby</key>
<string>Matthew McGarvey</string>
<string>Ramsey Solutions</string>
<key>description</key>
<string></string>
<key>disabled</key>
Expand All @@ -148,6 +148,8 @@
<false/>
<key>alfredfiltersresultsmatchmode</key>
<integer>0</integer>
<key>argumenttreatemptyqueryasnil</key>
<false/>
<key>argumenttrimmode</key>
<integer>0</integer>
<key>argumenttype</key>
Expand All @@ -167,7 +169,7 @@
<key>runningsubtext</key>
<string>Github repos are being loaded...</string>
<key>script</key>
<string>python github.py {query}</string>
<string>/usr/bin/python3 github.py {query}</string>
<key>scriptargtype</key>
<integer>0</integer>
<key>scriptfile</key>
Expand All @@ -186,7 +188,7 @@
<key>uid</key>
<string>F32B16BE-138F-4523-960F-775878AA1D76</string>
<key>version</key>
<integer>2</integer>
<integer>3</integer>
</dict>
<dict>
<key>config</key>
Expand Down Expand Up @@ -274,7 +276,7 @@
<key>escaping</key>
<integer>102</integer>
<key>script</key>
<string>python github.py --auth {query}</string>
<string>/usr/bin/python3 github.py --auth {query}</string>
<key>scriptargtype</key>
<integer>0</integer>
<key>scriptfile</key>
Expand Down Expand Up @@ -379,7 +381,7 @@
<key>escaping</key>
<integer>102</integer>
<key>script</key>
<string>python github.py --refresh</string>
<string>/usr/bin/python3 github.py --refresh</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
Expand Down Expand Up @@ -532,6 +534,6 @@
</dict>
</dict>
<key>webaddress</key>
<string>https://github.com/matthewmcgarvey/alfred-github-workflow</string>
<string>https://github.com/lampo/alfred-github-workflow</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion install-libraries.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

pip install -r requirements.txt --target=./lib
pip3 install -r requirements.txt --target=./lib
Loading

0 comments on commit 43637ab

Please sign in to comment.