-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglyphsapp-documentation.py
53 lines (40 loc) · 1.44 KB
/
glyphsapp-documentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Author: Mark Frömberg (https://markfromberg.com) :: @Mark2Mark
Published by: Hypertype (https://hypertype.xyz) :: @hyper-type
Script Filter Settings:
(Triggered by typing 'gd' followed by a space and filter query)
Keyword: `gd`, check "with Space", "Argument Optional"
Language: `/bin/zsh` or `/bin/bash`, "with input as argv"
Script: `python3 glyphsapp-documentation.py "$1"`
"""
import urllib.request
import sys
import json
import re
def compare(a, b):
if a.lower() in b.lower():
return True
return False
documentation_url = "https://docu.glyphsapp.com/"
# Fetch documentation and parse links
with urllib.request.urlopen(documentation_url) as response:
html_content = response.read().decode("utf-8")
link_pattern = r'<dt id="([\w\.]+)">' # alternatively for other sites: e.g. `r'<a href="#([\w\.]+)">'`
links = re.findall(link_pattern, html_content)
# Prepare the results as Alfred JSON
results = []
for link in links:
result = {
"uid": link,
"title": link,
"arg": documentation_url + "#" + link,
"icon": {"path": "icon.png"}, # ripped from twitter
}
results.append(result)
# Filter results based on user input
query = sys.argv[1].strip()
if query:
results = [result for result in results if compare(query, result["title"])]
# Output results as Alfred JSON
alfred_output = {"items": results}
sys.stdout.write(json.dumps(alfred_output)) # or use `print`