from twisted.web import resource
import os
from hatena import Log, ServerLog, Silent, NotFound
from DB import Database
from Hatenatools import TMB
#The movie folder:
class PyResource(resource.Resource):
isLeaf = False
def __init__(self):
resource.Resource.__init__(self)
self.CreatorID = CreatorIDResource()
def getChild(self, name, request):
if Database.CreatorExists(name):
return self.CreatorID
elif name == "":
return self
else:
return NotFound
def render(self, request):
request.setResponseCode(403)
return "403 - Denied access"
#The creator ID folder:
class CreatorIDResource(resource.Resource):
isLeaf = False
def __init__(self):
resource.Resource.__init__(self)
self.CreatorIDFile = CreatorIDFileResource()
def getChild(self, name, request):
CreatorID = request.path.split("/")[-2]
filename = ".".join(name.split(".")[:-1])
if Database.FlipnoteExists(CreatorID, filename):#html, ppm and info
return self.CreatorIDFile
elif name == "":
return self
else:
return NotFound
def render(self, request):
request.setResponseCode(403)
return "403 - Denied access"
#Any public file inside creator ID folder:
class CreatorIDFileResource(resource.Resource):
isLeaf = True
def __init__(self):
resource.Resource.__init__(self)
def render(self, request):
creator, file = request.path.split("/")[-2:]
filetype = file.split(".")[-1].lower()
if filetype in "ppm":
#log it:
path = "/".join(request.path.split("/")[3:])
Log(request, path)
#add a view:
Database.AddView(creator, file[:-4])
#read ppm file:
data = Database.GetFlipnotePPM(creator, file[:-4])
#send file to the client:
request.responseHeaders.setRawHeaders('content-type', ['text/plain'])
return data
elif filetype == "info":
path = "/".join(request.path.split("/")[3:])
Log(request, path, True)
request.responseHeaders.setRawHeaders('content-type', ['text/plain'])
return "0\n0\n"#undocumented what it means
elif filetype == "htm":
#maybe cache the details page of Database.Newest?
if "mode" in request.args:
if request.args["mode"][0] == "commentshalfsize":
pass
return self.GenerateDetailsPage(creator, ".".join(file.split(".")[:-1])).encode("UTF-8")
elif filetype == "star":
path = "/".join(request.path.split("/")[3:])
try:
color = request.args.get('starcolor')[0] #colour of the star. can be green, red, blue, or purple.
except:
color = "yellow" #if starcolor arg doesn't exist, then its yellow. there's probably a better way to handle this, but I'm not fluent with python ;-;
headers = request.getAllHeaders()
#bad formatting
if "x-hatena-star-count" not in headers:
ServerLog.write("%s got 403 when requesting %s without a X-Hatena-Star-Count header" % (request.getClientIP(), path), Silent)
request.setResponseCode(403)
return "403 - Denied access\nRequest lacks a X-Hatena-Star-Count http header"
#add the stars:
amount = int(headers["x-hatena-star-count"])
if (amount < 1) or (amount > 65535):
ServerLog.write("%s got 403 when requesting %s with an invalid X-Hatena-Star-Count header" % (request.getClientIP(), path), Silent)
request.setResponseCode(403)
return "403 - Denied access\nRequest has an invalid X-Hatena-Star-Count http header"
if not Database.AddStar(creator, file[:-5], amount, color):
#error
ServerLog.write("%s got 500 when requesting %s" % (request.getClientIP(), path), Silent)
request.setResponseCode(500)
return "500 - Internal server error\nAdding the stars seem to have failed."
#report success
ServerLog.write("%s added %i %s stars to %s/%s.ppm" % (request.getClientIP(), amount, color, creator, file[:-5]), Silent)
return "Success"
elif filetype == "dl":
path = "/".join(request.path.split("/")[3:])
Log(request, path, True)
#this is POSTed to when it've been stored to memory.
Database.AddDownload(creator, file[:-3])
return "Noted ;)"
else:
path = "/".join(request.path.split("/")[3:])
ServerLog.write("%s got 403 when requesting %s" % (request.getClientIP(), path), Silent)
request.setResponseCode(403)
return "403 - Denied access"
#details page
def GenerateDetailsPage(self, CreatorID, filename):#filename without ext
flipnote = Database.GetFlipnote(CreatorID, filename)#flipnote = [filename, views, stars, green stars, red stars, blue stars, purple stars, Channel], all probably strings
if not flipnote:
return "This flipnote doesn't exist!"
tmb = TMB().Read(Database.GetFlipnoteTMB(CreatorID, filename))
if not tmb:
return "This flipnote is corrupt!"
#Is it a spinoff?
Spinnoff = ""
if tmb.OriginalAuthorID <> tmb.EditorAuthorID or tmb.OriginalFilename <> tmb.CurrentFilename:
if Database.FlipnoteExists(tmb.OriginalAuthorID, tmb.OriginalFilename[:-4]):
Spinnoff = SpinoffTemplate1.replace("%%CreatorID%%", tmb.OriginalAuthorID).replace("%%Filename%%", tmb.OriginalFilename[:-4])
elif tmb.OriginalAuthorID <> tmb.EditorAuthorID:
Spinnoff = SpinoffTemplate2
#make each entry:
Entries = []
#Creator username:
name = "Creator"
#content = "%s" % (CreatorID, tmb.EditorAuthorName)
content = '%s' % (CreatorID, tmb.Username)
Entries.append(PageEntryTemplate.replace("%%Name%%", name).replace("%%Content%%", content))
#Stars:
name = "Stars"
content = u'\u2605 %s' % (CreatorID, filename, flipnote[2])#yellow stars
content += u'
\u2605 %s' % (CreatorID, filename, flipnote[3])#green stars
content += u'
\u2605 %s' % (CreatorID, filename, flipnote[4])#red stars
content += u'
\u2605 %s' % (CreatorID, filename, flipnote[5])#blue stars
content += u'
\u2605 %s' % (CreatorID, filename, flipnote[6])#purple stars
Entries.append(PageEntryTemplate.replace("%%Name%%", name).replace("%%Content%%", content))
#Views:
name = "Views"
content = str(flipnote[1])
Entries.append(PageEntryTemplate.replace("%%Name%%", name).replace("%%Content%%", content))
# Downloads:
name = "Downloads"
content = str(flipnote[8])
Entries.append(PageEntryTemplate.replace("%%Name%%", name).replace("%%Content%%", content))
#Channel:
if flipnote[7]:#todo: make channels work at all
name = "Channel"
content = 'a href="http://flipnote.hatena.com/ds/v2-xx/ch/%s.uls">%s' % (flipnote[7], flipnote[7])
Entries.append(PageEntryTemplate.replace("%%Name%%", name).replace("%%Content%%", content))
#Comments:
Comments = "0"
#doto: add original author info too
#add the entries to page:
return DetailsPageTemplate.replace("%%CreatorID%%", CreatorID).replace("%%Filename%%", filename).replace("%%CommentCount%%", Comments).replace("%%Spinoff%%", Spinnoff).replace("%%PageEntries%%", PageEntrySeparator.join(Entries))
#templates:
DetailsPageTemplate = """
|
Description
|
Comments(%%CommentCount%%) |