Initial commit
This commit is contained in:
commit
abff29278f
69
gitea-to-ics.py
Normal file
69
gitea-to-ics.py
Normal file
|
@ -0,0 +1,69 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
import urllib.request
|
||||
|
||||
from base64 import b64decode
|
||||
from subprocess import run
|
||||
|
||||
import cherrypy
|
||||
|
||||
from icalendar import Calendar, Todo
|
||||
|
||||
GITEA_REPO_URL="https://git.bitmessage.org/api/v1"
|
||||
|
||||
combined = {}
|
||||
|
||||
def get_combined(token):
|
||||
for q in ("assigned", "review_requested"):
|
||||
print(f"Querying {q}")
|
||||
req = urllib.request.Request(GITEA_REPO_URL
|
||||
+ f"/repos/issues/search?{q}=true")
|
||||
req.add_header("Accept", "application/json")
|
||||
req.add_header("Content-Type", "application/json")
|
||||
req.add_header("Authorization", "token " + token)
|
||||
|
||||
with urllib.request.urlopen(req) \
|
||||
as response:
|
||||
issues = json.load(response)
|
||||
for issue in issues:
|
||||
_id = issue['id']
|
||||
if _id in combined:
|
||||
combined[_id]['categories'].append(q)
|
||||
else:
|
||||
combined[_id] = issue
|
||||
combined[_id]['categories'] = [q]
|
||||
return combined
|
||||
|
||||
def process_combined(combined):
|
||||
cal = Calendar()
|
||||
for _id, issue in combined.items():
|
||||
todo = Todo()
|
||||
todo['uid'] = _id
|
||||
todo['dtstamp'] = issue['created_at']
|
||||
if issue['due_date']:
|
||||
todo['due'] = issue['due_date']
|
||||
todo['summary'] = issue['title']
|
||||
todo['description'] = issue['body']
|
||||
todo['categories'] = issue['categories']
|
||||
cal.add_component(todo)
|
||||
return cal.to_ical()
|
||||
|
||||
def get_token(input_token):
|
||||
token = input_token.lstrip("Basic ")
|
||||
token = b64decode(token).decode('utf8', 'ignore')
|
||||
_, token = token.split(":", 2)
|
||||
return token
|
||||
|
||||
class Root:
|
||||
@cherrypy.expose
|
||||
def todo(self):
|
||||
authorization = cherrypy.request.headers.get('Authorization', ':')
|
||||
token = get_token(authorization)
|
||||
combined = get_combined(token)
|
||||
if not combined:
|
||||
raise cherrypy.HTTPError(401, 'Unauthorized')
|
||||
return(process_combined(combined))
|
||||
|
||||
if __name__ == '__main__':
|
||||
cherrypy.quickstart(Root(), '/')
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
cherrypy
|
||||
icalendar
|
Loading…
Reference in New Issue
Block a user