Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Copyright Notice here
- # a
- # a
- import sys, re, os, urllib, urllib2
- import string, cgi, time
- from xml.dom import getDOMImplementation
- from xml.sax import saxutils
- from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
- from xml.dom import getDOMImplementation
- scriptdir = os.path.abspath(os.path.dirname(sys.argv[0]))
- sys.path.append(scriptdir + "/lib/")
- sys.path.append(scriptdir + "/lib/chardet/")
- sys.path.append(scriptdir + "/sites/")
- os.chdir(scriptdir)
- from Googlyrics import Googlyrics
- if (len(sys.argv) > 1):
- lport = int(sys.argv[1])
- else:
- lport = 8801
- if (len(sys.argv) > 2):
- strictclient = sys.argv[2]
- else:
- strictclient = "0"
- class MyHandler(BaseHTTPRequestHandler):
- def do_GET(self):
- if (self.client_address[0] == strictclient) or ( strictclient == "0"):
- try:
- print(self.path)
- command, artist, title = self.path.split("+_+")
- print(self.client_address)
- self.send_response(200)
- self.send_header('Content-type', 'text/html')
- self.end_headers()
- g = Googlyrics()
- #command, artist, title = self.path.split("+_+")
- if os.path.exists(os.path.join(os.path.expanduser("~"), ".gLyrics", artist.replace("%20", "_"), title.replace("%20", "_"))):
- fi=open(os.path.join(os.path.expanduser("~"), ".gLyrics", artist.replace("%20", "_"), title.replace("%20", "_")), 'r')
- doctxt = fi.read()
- fi.close()
- rb = 0
- else:
- rb = 1
- try:
- outlyric = None
- hits = g.find_lyrics(urllib.unquote(title), urllib.unquote(artist))
- for ly in hits:
- lyric = ly.getLyric()
- if lyric is not None:
- outlyric = lyric
- break
- dom = getDOMImplementation()
- if outlyric is None:
- doc = dom.createDocument('', 'suggestions', '')
- doc.documentElement.setAttribute('title', urllib.unquote(title))
- doc.documentElement.setAttribute('artist', urllib.unquote(artist))
- else:
- doc = dom.createDocument('', 'lyrics', '')
- doc.documentElement.setAttribute('title', saxutils.escape(outlyric.title))
- doc.documentElement.setAttribute('site', saxutils.escape(outlyric.sitename))
- doc.documentElement.setAttribute('artist', saxutils.escape(outlyric.artist))
- doc.documentElement.setAttribute('site_url', saxutils.escape(outlyric.siteurl))
- doc.documentElement.setAttribute('page_url', saxutils.escape(outlyric.url))
- doc.documentElement.appendChild(doc.createTextNode(saxutils.escape(outlyric.lyrics).encode('UTF-8')))
- doctxt = doc.toxml()
- except OSError:
- doctxt = ""
- #self.wfile.write("hey, today is the " + str(time.localtime()[7]))
- #self.wfile.write(" day in the year " + str(time.localtime()[0]))
- if command.endswith("html"):
- #self.wfile.write("<HTML>" + doctxt.replace( "\n", "<Br>").replace("&#039;", "'") + "</HTML>")
- self.wfile.write("<HTML>" + doctxt.replace( "\n", "<Br>").replace("&", "&").decode("iso-8859-1") + "</HTML>")
- else:
- self.wfile.write(doctxt.replace("&", "&"))
- # if (doctxt != "") and (rb == 1):
- # if not os.path.exists(os.path.join(os.path.expanduser("~"), ".gLyrics")):
- # os.mkdir(os.path.join(os.path.expanduser("~"), ".gLyrics"))
- # if not os.path.exists(os.path.join(os.path.expanduser("~"), ".gLyrics", artist.replace("%20", "_"))):
- # os.mkdir(os.path.join(os.path.expanduser("~"), ".gLyrics", artist.replace("%20", "_")))
- # #print(os.path.join(os.path.expanduser("~"), ".gLyrics", artist, title))
- # fi=open(os.path.join(os.path.expanduser("~"), ".gLyrics", artist.replace("%20", "_"), title.replace("%20", "_")), 'w')
- # fi.write(doctxt)
- # fi.close()
- # return
- return
- except IOError:
- self.send_error(404,'File Not Found: %s' % self.path)
- else:
- print ("unable to read client")
- def do_POST(self):
- global rootnode
- try:
- ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
- if ctype == 'multipart/form-data':
- query=cgi.parse_multipart(self.rfile, pdict)
- self.send_response(301)
- self.end_headers()
- upfilecontent = query.get('upfile')
- print ("filecontent", upfilecontent[0])
- self.wfile.write("<HTML>POST OK.<BR><BR>");
- self.wfile.write(upfilecontent[0]);
- except :
- pass
- def main():
- try:
- server = HTTPServer(('', lport), MyHandler)
- print ('started httpserver...')
- server.serve_forever()
- except KeyboardInterrupt:
- print ('^C received, shutting down server')
- server.socket.close()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement