Tip: Click lines to highlight, hold ctrl/cmd to multi-select

http://codedumper.com/iyeri (21-Aug @ 05:13)

Syntax Highlighted Code

  1. #!/home/yansky/mypython/bin/python
  2. import cgi, HTMLParser, urllib2, httplib, BeautifulSoup, MySQLdb, sys
  3. #import cgitb; cgitb.enable()
  4. print 'Content-Type: text/html\n\n'
  5.  
  6. # Get the cgi module and the values of all fields in the form
  7.  
  8. formStorage = cgi.FieldStorage()
  9.  
  10. # Get a parameter string from the form
  11. theValue =  formStorage['postLink'].value
  12. getanimValue = formStorage.getvalue("animated")
  13. animValue = 0
  14. if getanimValue == 'animated':
  15.   animValue = 1  
  16.  
  17. def putInDB(picLink, uid, nm, aV):
  18.  
  19.   # connect
  20.   try:
  21.  
  22.     db = MySQLdb.connect(host="localhost", user="foo", passwd="bar", db="yansky_avatars")
  23.    
  24.   except MySQLdb.Error, e:
  25.  
  26.     print "Error %d: %s" % (e.args[0], e.args[1])
  27.    
  28.     sys.exit (1)  
  29.  
  30.  
  31.   # create a cursor
  32.   cursor = db.cursor()
  33.  
  34.   # execute SQL statement
  35.   cursor.execute("SELECT userID FROM avStorage")
  36.   # get the resultset as a tuple
  37.   checkIfThere = cursor.fetchall()
  38.  
  39.   dbExec = "INSERT INTO avStorage (userName, userID, avatarLink, animated) VALUES (%s, %s, %s, %s)"
  40.  
  41.   tuP = (nm, uid, picLink, aV)
  42.  
  43.   whatDone = 'Saved.'
  44.  
  45.   for checks in checkIfThere:
  46.  
  47.     if checks[0] == uid:
  48.      
  49.       # execute SQL statement
  50.       dbExec = "UPDATE avStorage SET userName=%s, userID=%s, avatarLink=%s, animated=%s WHERE userID=%s"
  51.      
  52.       tuP = (nm, uid, picLink, aV, uid)
  53.      
  54.       whatDone = 'Updated.'
  55.  
  56.   # execute SQL statement
  57.   cursor.execute(
  58.       dbExec,
  59.       tuP
  60.   )  
  61.  
  62.  
  63.   # execute SQL statement
  64.   cursor.execute("SELECT * FROM avStorage")
  65.   # get the resultset as a tuple
  66.   result = cursor.fetchall()
  67.  
  68.   new_text_file1 = open("../wpavatars/avatar.css", "w")
  69.   new_text_file2 = open("../wpavatars/jbavatar.css", "w")
  70.   new_text_file3 = open("../wpavatars/animatedavatar.css", "w")
  71.  
  72.   tex1 = ""
  73.   tex2 = ""
  74.   tex3 = ""
  75.   # iterate through resultset
  76.   for record in result:
  77.  
  78.     uName = record[0]
  79.     uIDs = str(record[1])
  80.     uAvLink = record[2]
  81.  
  82.     if record[3]:
  83.    
  84.       tex3 += "/* "+uName+" */ \n tr.wlr_"+uIDs+" td.bodyuser div:first-child >a:first-child {background:url("+uAvLink+"); display:block; height:80px; width:80px; background-repeat: no-repeat;  } \n \n "
  85.    
  86.     else:
  87.    
  88.       tex1 += "/* "+uName+" */ \n tr.wlr_"+uIDs+" td.bodyuser div:first-child >a:first-child {background:url("+uAvLink+"); display:block; height:80px; width:80px; background-repeat: no-repeat;  } \n \n "
  89.       tex2 += "/* "+uName+" */ \n tr.wlr_"+uIDs+" a .bu_name:after {content: url("+uAvLink+") ; } \n \n "
  90.      
  91.   new_text_file1.write(tex1)
  92.   new_text_file2.write(tex2)
  93.   new_text_file3.write(tex3)
  94.    
  95.   new_text_file1.close()
  96.   new_text_file2.close()
  97.   new_text_file3.close()
  98.  
  99.  
  100.   print """\
  101.      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  102.      <html>
  103.      <head>
  104.      <title>Whirlpool.Net.Au Avatar Thingo</title>
  105.      <style type="text/css">
  106.        body{
  107.          
  108.          background-color:#F0F0F8;
  109.        
  110.        }
  111.      </style>
  112.      </head>
  113.      <body>
  114.        <br />
  115.        <p>Avatar Link """+whatDone+""" </p>
  116.      </body>
  117.      </html>
  118.  """
  119.  
  120.  
  121.  
  122. def getAvLink(repFullNum, threadNum, pageNum, anchorNum):
  123.  
  124.     getURL = "http://forums.whirlpool.net.au/forum-replies.cfm?t="+threadNum+"&p="+pageNum+"#r"+anchorNum
  125.     shortenLink = "/forum-replies.cfm?t="+threadNum+"&p="+pageNum+"#r"+anchorNum
  126.    
  127.     if (pageNum == '1'):
  128.         shortenLink = "/forum-replies.cfm?t="+threadNum+"#r"+anchorNum
  129.  
  130.     request = urllib2.Request(getURL)
  131.     request.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')
  132.     opener = urllib2.build_opener()
  133.     feeddata = opener.open(request).read()
  134.     b = BeautifulSoup.BeautifulSoup(feeddata)
  135.  
  136.     getPostAs = b.find("a", { "href" : shortenLink }).parent.parent.parent.findAll('a')
  137.  
  138.     for item in getPostAs:
  139.  
  140.         if item.has_key('href'):
  141.  
  142.             hr = item['href']
  143.  
  144.             if hr.startswith('/forum-user.cfm?id='):
  145.  
  146.             uid = int(hr.split('id=')[1])
  147.  
  148.             nm = ''.join(str(item.contents[1].contents[0]))
  149.  
  150.             elif hr.endswith('.jpg'):
  151.  
  152.             picLink = hr
  153.  
  154.             elif hr.endswith('.jpeg'):
  155.  
  156.             picLink = hr
  157.  
  158.             elif hr.endswith('.png'):
  159.  
  160.             picLink = hr
  161.  
  162.             elif hr.endswith('.gif'):
  163.  
  164.             picLink = hr            
  165.  
  166.     putInDB(picLink, uid ,nm, animValue)
  167.                
  168.  
  169. if theValue.startswith("http://forums.whirlpool.net.au/forum-replies-herring") and theValue.find("r=") > -1:
  170.  
  171.     vS = theValue.split('&tpr=')
  172.  
  173.     repFullNum = vS[0].split('r=')[1].split('&tpr')[0]
  174.  
  175.     vS2 = vS[1].split(',')
  176.  
  177.     threadNum = vS2[0]
  178.  
  179.     pageNum = vS2[1]
  180.     anchorNum = vS2[2]
  181.  
  182.     getAvLink(repFullNum, threadNum, pageNum, anchorNum)
  183.  
  184. else:
  185.   print """\
  186.      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  187.      <html>
  188.      <head>
  189.      <title>Whirlpool.Net.Au Avatar Thingo</title>
  190.      <style type="text/css">
  191.        body{
  192.          
  193.          background-color:#F0F0F8;
  194.        
  195.        }
  196.      </style>
  197.      </head>
  198.      <br />
  199.      <p>Dude, that's not a proper URL!</p>
  200.      </body></html>
  201.      """

Plain Code

#!/home/yansky/mypython/bin/python
import cgi, HTMLParser, urllib2, httplib, BeautifulSoup, MySQLdb, sys
#import cgitb; cgitb.enable()
print 'Content-Type: text/html\n\n'

# Get the cgi module and the values of all fields in the form

formStorage = cgi.FieldStorage()

# Get a parameter string from the form 
theValue =  formStorage['postLink'].value
getanimValue = formStorage.getvalue("animated")
animValue = 0
if getanimValue == 'animated':
  animValue = 1  

def putInDB(picLink, uid, nm, aV):
  
  # connect
  try:
  
    db = MySQLdb.connect(host="localhost", user="foo", passwd="bar", db="yansky_avatars")
    
  except MySQLdb.Error, e:
  
    print "Error %d: %s" % (e.args[0], e.args[1])
    
    sys.exit (1)  
  

  # create a cursor
  cursor = db.cursor()
  
  # execute SQL statement
  cursor.execute("SELECT userID FROM avStorage")
  # get the resultset as a tuple
  checkIfThere = cursor.fetchall()
  
  dbExec = "INSERT INTO avStorage (userName, userID, avatarLink, animated) VALUES (%s, %s, %s, %s)"
  
  tuP = (nm, uid, picLink, aV)
  
  whatDone = 'Saved.'
  
  for checks in checkIfThere:

    if checks[0] == uid:
      
      # execute SQL statement
      dbExec = "UPDATE avStorage SET userName=%s, userID=%s, avatarLink=%s, animated=%s WHERE userID=%s"
      
      tuP = (nm, uid, picLink, aV, uid)
      
      whatDone = 'Updated.'

  # execute SQL statement
  cursor.execute(
      dbExec,
      tuP
  )   
  
  
  # execute SQL statement
  cursor.execute("SELECT * FROM avStorage")
  # get the resultset as a tuple
  result = cursor.fetchall()
  
  new_text_file1 = open("../wpavatars/avatar.css", "w")
  new_text_file2 = open("../wpavatars/jbavatar.css", "w")
  new_text_file3 = open("../wpavatars/animatedavatar.css", "w")
  
  tex1 = ""
  tex2 = ""
  tex3 = ""
  # iterate through resultset
  for record in result:
  
    uName = record[0]
    uIDs = str(record[1])
    uAvLink = record[2]
  
    if record[3]:
    
      tex3 += "/* "+uName+" */ \n tr.wlr_"+uIDs+" td.bodyuser div:first-child >a:first-child {background:url("+uAvLink+"); display:block; height:80px; width:80px; background-repeat: no-repeat;  } \n \n "
    
    else:
    
      tex1 += "/* "+uName+" */ \n tr.wlr_"+uIDs+" td.bodyuser div:first-child >a:first-child {background:url("+uAvLink+"); display:block; height:80px; width:80px; background-repeat: no-repeat;  } \n \n "
      tex2 += "/* "+uName+" */ \n tr.wlr_"+uIDs+" a .bu_name:after {content: url("+uAvLink+") ; } \n \n "
      
  new_text_file1.write(tex1)
  new_text_file2.write(tex2)
  new_text_file3.write(tex3)
    
  new_text_file1.close()
  new_text_file2.close()
  new_text_file3.close()
  
  
  print """\
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      <title>Whirlpool.Net.Au Avatar Thingo</title>
      <style type="text/css">
        body{
          
          background-color:#F0F0F8;
        
        }
      </style>
      </head>
      <body>
        <br />
        <p>Avatar Link """+whatDone+""" </p>
      </body>
      </html>
  """



def getAvLink(repFullNum, threadNum, pageNum, anchorNum):

    getURL = "http://forums.whirlpool.net.au/forum-replies.cfm?t="+threadNum+"&p="+pageNum+"#r"+anchorNum
    shortenLink = "/forum-replies.cfm?t="+threadNum+"&p="+pageNum+"#r"+anchorNum
    
    if (pageNum == '1'):
        shortenLink = "/forum-replies.cfm?t="+threadNum+"#r"+anchorNum

    request = urllib2.Request(getURL)
    request.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')
    opener = urllib2.build_opener()
    feeddata = opener.open(request).read()
    b = BeautifulSoup.BeautifulSoup(feeddata)

    getPostAs = b.find("a", { "href" : shortenLink }).parent.parent.parent.findAll('a')

    for item in getPostAs:

        if item.has_key('href'):

            hr = item['href']

            if hr.startswith('/forum-user.cfm?id='):

            uid = int(hr.split('id=')[1])

            nm = ''.join(str(item.contents[1].contents[0]))

            elif hr.endswith('.jpg'):

            picLink = hr

            elif hr.endswith('.jpeg'):

            picLink = hr

            elif hr.endswith('.png'):

            picLink = hr

            elif hr.endswith('.gif'):

            picLink = hr             

    putInDB(picLink, uid ,nm, animValue)
                

if theValue.startswith("http://forums.whirlpool.net.au/forum-replies-herring") and theValue.find("r=") > -1:

    vS = theValue.split('&tpr=')

    repFullNum = vS[0].split('r=')[1].split('&tpr')[0]

    vS2 = vS[1].split(',')

    threadNum = vS2[0]

    pageNum = vS2[1]
    anchorNum = vS2[2]

    getAvLink(repFullNum, threadNum, pageNum, anchorNum)

else:
  print """\
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      <title>Whirlpool.Net.Au Avatar Thingo</title>
      <style type="text/css">
        body{
          
          background-color:#F0F0F8;
        
        }
      </style>
      </head>
      <br />
      <p>Dude, that's not a proper URL!</p>
      </body></html>
      """ 

Permalink: http://codedumper.com/iyeri