Advertisement
Guest User

m

a guest
Feb 1st, 2009
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.11 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright Notice here
  4. # a
  5. # a
  6.  
  7.  
  8. import sys, re, os, urllib, urllib2
  9. import string, cgi, time
  10.  
  11. from xml.dom import getDOMImplementation
  12. from xml.sax import saxutils
  13. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  14.  
  15. from xml.dom import getDOMImplementation
  16.  
  17. scriptdir = os.path.abspath(os.path.dirname(sys.argv[0]))
  18. sys.path.append(scriptdir + "/lib/")
  19. sys.path.append(scriptdir + "/lib/chardet/")
  20. sys.path.append(scriptdir + "/sites/")
  21. os.chdir(scriptdir)
  22.  
  23. from Googlyrics import Googlyrics
  24.  
  25. if (len(sys.argv) > 1):
  26.   lport = int(sys.argv[1])
  27. else:
  28.   lport = 8801
  29.  
  30. if (len(sys.argv) > 2):
  31.   strictclient = sys.argv[2]
  32. else:
  33.   strictclient = "0"
  34.  
  35. class MyHandler(BaseHTTPRequestHandler):
  36.  
  37.     def do_GET(self):
  38.       if (self.client_address[0] == strictclient) or ( strictclient == "0"):
  39.         try:
  40.             print(self.path)
  41.             command, artist, title = self.path.split("+_+")
  42.             print(self.client_address)
  43.         self.send_response(200)
  44.         self.send_header('Content-type',    'text/html')
  45.         self.end_headers()
  46.         g = Googlyrics()
  47.         #command, artist, title = self.path.split("+_+")
  48.  
  49.         if os.path.exists(os.path.join(os.path.expanduser("~"), ".gLyrics", artist.replace("%20", "_"), title.replace("%20", "_"))):
  50.         fi=open(os.path.join(os.path.expanduser("~"), ".gLyrics", artist.replace("%20", "_"), title.replace("%20", "_")), 'r')
  51.         doctxt = fi.read()
  52.         fi.close()
  53.         rb = 0
  54.         else:
  55.         rb = 1
  56.                 try:
  57.                     outlyric = None
  58.                     hits = g.find_lyrics(urllib.unquote(title), urllib.unquote(artist))
  59.                     for ly in hits:
  60.                         lyric = ly.getLyric()
  61.                         if lyric is not None:
  62.                             outlyric = lyric
  63.                             break
  64.             dom = getDOMImplementation()
  65.                     if outlyric is None:
  66.                         doc = dom.createDocument('', 'suggestions', '')
  67.                         doc.documentElement.setAttribute('title', urllib.unquote(title))
  68.                         doc.documentElement.setAttribute('artist', urllib.unquote(artist))
  69.                     else:
  70.                         doc = dom.createDocument('', 'lyrics', '')
  71.                         doc.documentElement.setAttribute('title', saxutils.escape(outlyric.title))
  72.                         doc.documentElement.setAttribute('site', saxutils.escape(outlyric.sitename))
  73.                         doc.documentElement.setAttribute('artist', saxutils.escape(outlyric.artist))
  74.                         doc.documentElement.setAttribute('site_url', saxutils.escape(outlyric.siteurl))
  75.                         doc.documentElement.setAttribute('page_url', saxutils.escape(outlyric.url))
  76.                         doc.documentElement.appendChild(doc.createTextNode(saxutils.escape(outlyric.lyrics).encode('UTF-8')))
  77.                     doctxt = doc.toxml()
  78.         except OSError:
  79.             doctxt = ""
  80.  
  81.         #self.wfile.write("hey, today is the " + str(time.localtime()[7]))
  82.         #self.wfile.write(" day in the year " + str(time.localtime()[0]))
  83.         if command.endswith("html"):
  84.           #self.wfile.write("<HTML>" + doctxt.replace( "\n", "<Br>").replace("&amp;#039;", "'") + "</HTML>")
  85.           self.wfile.write("<HTML>" + doctxt.replace( "\n", "<Br>").replace("&amp;", "&").decode("iso-8859-1") + "</HTML>")
  86.         else:
  87.           self.wfile.write(doctxt.replace("&amp;", "&"))
  88.  
  89. #       if (doctxt != "") and (rb == 1):
  90. #       if not os.path.exists(os.path.join(os.path.expanduser("~"), ".gLyrics")):
  91. #           os.mkdir(os.path.join(os.path.expanduser("~"), ".gLyrics"))
  92. #       if not os.path.exists(os.path.join(os.path.expanduser("~"), ".gLyrics", artist.replace("%20", "_"))):
  93. #           os.mkdir(os.path.join(os.path.expanduser("~"), ".gLyrics", artist.replace("%20", "_")))
  94. #       #print(os.path.join(os.path.expanduser("~"), ".gLyrics", artist, title))
  95. #       fi=open(os.path.join(os.path.expanduser("~"), ".gLyrics", artist.replace("%20", "_"), title.replace("%20", "_")), 'w')
  96. #       fi.write(doctxt)
  97. #       fi.close()
  98. #       return
  99.  
  100.         return
  101.      
  102.         except IOError:
  103.             self.send_error(404,'File Not Found: %s' % self.path)
  104.       else:
  105.         print ("unable to read client")
  106.  
  107.     def do_POST(self):
  108.         global rootnode
  109.         try:
  110.             ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
  111.             if ctype == 'multipart/form-data':
  112.                 query=cgi.parse_multipart(self.rfile, pdict)
  113.             self.send_response(301)
  114.            
  115.             self.end_headers()
  116.             upfilecontent = query.get('upfile')
  117.             print ("filecontent", upfilecontent[0])
  118.             self.wfile.write("<HTML>POST OK.<BR><BR>");
  119.             self.wfile.write(upfilecontent[0]);
  120.            
  121.         except :
  122.             pass
  123.  
  124. def main():
  125.     try:
  126.         server = HTTPServer(('', lport), MyHandler)
  127.         print ('started httpserver...')
  128.         server.serve_forever()
  129.     except KeyboardInterrupt:
  130.         print ('^C received, shutting down server')
  131.         server.socket.close()
  132.  
  133. if __name__ == '__main__':
  134.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement