Tip: Click lines to highlight, hold ctrl/cmd to multi-select
http://codedumper.com/iyeri (21-Aug @ 05:13)
Syntax Highlighted 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>
- """
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>
"""