epicyon/webinterface.py

117 lines
3.9 KiB
Python
Raw Normal View History

2019-07-20 21:13:36 +00:00
__filename__ = "webinterface.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "0.0.1"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
import json
def htmlHeader(lang='en') -> str:
htmlStr= \
'<!DOCTYPE html>\n' \
'<html lang="'+lang+'">\n' \
' <meta charset="utf-8">\n' \
' <style>\n' \
' @import url("epicyon.css");\n' \
' </style>\n' \
' <body>\n'
return htmlStr
def htmlFooter() -> str:
htmlStr= \
' </body>\n' \
'</html>\n'
return htmlStr
def htmlProfile(profileJson: {}) -> str:
"""Show the profile page as html
"""
return htmlHeader()+"<h1>Profile page</h1>"+htmlFooter()
def htmlFollowing(followingJson: {}) -> str:
"""Show the following collection as html
"""
return htmlHeader()+"<h1>Following collection</h1>"+htmlFooter()
def htmlFollowers(followersJson: {}) -> str:
"""Show the followers collection as html
"""
return htmlHeader()+"<h1>Followers collection</h1>"+htmlFooter()
2019-07-21 09:09:28 +00:00
def individualPostAsHtml(postJsonObject: {}) -> str:
2019-07-21 11:20:49 +00:00
avatarPosition=''
containerClass='container'
timeClass='time-right'
if postJsonObject['object']['inReplyTo']:
containerClass='container darker'
avatarPosition=' class="right"'
timeClass='time-left'
attachmentStr=''
if postJsonObject['object']['attachment']:
if isinstance(postJsonObject['object']['attachment'], list):
attachmentCtr=0
for attach in postJsonObject['object']['attachment']:
if attach.get('mediaType') and attach.get('url'):
mediaType=attach['mediaType']
imageDescription=''
if attach.get('name'):
imageDescription=attach['name']
if mediaType=='image/png' or \
mediaType=='image/jpeg' or \
mediaType=='image/gif':
if attach['url'].endswith('.png') or \
attach['url'].endswith('.jpg') or \
attach['url'].endswith('.jpeg') or \
attach['url'].endswith('.gif'):
if attachmentCtr>0:
attachmentStr+='<br>'
attachmentStr+= \
2019-07-21 12:24:38 +00:00
'<a href="'+attach['url']+'">' \
'<img src="'+attach['url']+'" alt="'+imageDescription+'" title="'+imageDescription+'" class="attachment"></a>\n'
2019-07-21 11:20:49 +00:00
attachmentCtr+=1
2019-07-21 09:09:28 +00:00
return \
2019-07-21 11:20:49 +00:00
'<div class="'+containerClass+'">\n' \
2019-07-21 12:11:28 +00:00
'<a href="'+postJsonObject['actor']+'">' \
'<img src="'+postJsonObject['actor']+'/avatar.png" alt="Avatar"'+avatarPosition+'></a>\n'+ \
2019-07-21 09:09:28 +00:00
postJsonObject['object']['content']+'\n'+ \
2019-07-21 11:20:49 +00:00
attachmentStr+ \
'<span class="'+timeClass+'">'+postJsonObject['object']['published']+'</span>\n'+ \
2019-07-21 11:52:13 +00:00
'</div>\n'
2019-07-21 09:09:28 +00:00
def htmlTimeline(timelineJson: {}) -> str:
"""Show the timeline as html
"""
if not timelineJson.get('orderedItems'):
return ""
tlStr=htmlHeader()
for item in timelineJson['orderedItems']:
if item['type']=='Create':
tlStr+=individualPostAsHtml(item)
tlStr+=htmlFooter()
return tlStr
2019-07-20 21:13:36 +00:00
def htmlInbox(inboxJson: {}) -> str:
"""Show the inbox as html
"""
2019-07-21 09:09:28 +00:00
return htmlTimeline(inboxJson)
2019-07-20 21:13:36 +00:00
def htmlOutbox(outboxJson: {}) -> str:
"""Show the Outbox as html
"""
2019-07-21 09:09:28 +00:00
return htmlTimeline(outboxJson)
2019-07-20 21:13:36 +00:00
def htmlIndividualPost(postJsonObject: {}) -> str:
"""Show an individual post as html
"""
2019-07-21 09:09:28 +00:00
return htmlHeader()+ \
individualPostAsHtml(postJsonObject)+ \
htmlFooter()
2019-07-20 21:13:36 +00:00
def htmlPostReplies(postJsonObject: {}) -> str:
"""Show the replies to an individual post as html
"""
return htmlHeader()+"<h1>Replies</h1>"+htmlFooter()