The "CookieInfo" and "Cookie" classes provide developers with an read-only interface to the "cookies.txt" that most browsers store. The CookieInfo class makes Cookie objects from the cookie file. It also provides methods to operate on the whole set of Cookies. The Cookie object provides methods to convert a cookie into both XML and SQL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | import string, sys
true, false = 1, 0
class Cookie:
Url = ""
Data1 = ""
Data2 = ""
Data3 = ""
Data4 = ""
Data5 = ""
# construct the cookie object
def __init__(self, cookieInfo):
self.Url = cookieInfo[0]
self.Data1 = cookieInfo[1]
self.Data2 = cookieInfo[2]
self.Data3 = cookieInfo[3]
self.Data4 = cookieInfo[4]
self.Data5 = cookieInfo[5]
# Get statements (pretend the data is private)
def getUrl(self): return self.Url
def getData1(self): return self.Data1
def getData2(self): return self.Data2
def getData3(self): return self.Data3
def getData4(self): return self.Data4
def getData5(self): return self.Data5
# generate SQL for the cookie
def generateSQL(self):
sql = "INSERT INTO Cookie (Url,Data1,Data2,Data3,Data4,Data5)"
sql += " VALUES ('"+self.Url+"','"+self.Data1+"','"+self.Data2+"','"+self.Data3+"','"
sql += self.Data4+"','"+self.Data5+"');"
return sql
# generate XML for the cookie
def generateXML(self):
xml = "<cookie url='"+self.Url+"' data1='"+self.Data1+"' data2='"+self.Data2
xml+= "' data3='"+self.Data3+"' data4='"+self.Data4+"' data5='"+self.Data5+"'/>"
return xml
class CookieInfo:
rawCookieContent = ""
cookies = []
cookieSeperator = " "
# Construct the CookieInfo object.
def __init__(self, cookiePathName):
cookieFile = open(cookiePathName, "r")
self.rawCookieContent = cookieFile.readlines()
cookieFile.close()
for line in self.rawCookieContent:
if line[0:1] == '#':
pass
elif line[0:1] == '\n':
pass
else:
self.cookies.append(Cookie(line.split(self.cookieSeperator)))
# Returns the amount of cookies present in the file
def count(self):
return len(self.cookies)
# Find a cookie by its URL and return a Cookie object, returns "" on fail
def findCookieByURL(self, url):
for cookie in self.cookies:
if cookie.getUrl() == url:
return cookie
else: return false
# Find an array of Cookie objects that contain the given string
def findCookiesByString(self, str):
string_find = string.find
string_join = string.join
results = []
for c in self.cookies:
atrs = [c.getUrl(),c.getData1(),c.getData2(), c.getData3(),c.getData4(),c.getData5()]
if string_find(string_join(atrs, " "), str) != -1:
results.append(c)
return results
# Return SQL for all the cookies
def returnAllCookiesInSQL(self):
sql = ""
for cookie in self.cookies:
sql += cookie.generateSQL + "\n"
# Return XML for all the cookies
def returnAllCookiesInXML(self):
xml = "<?xml version='1.0' ?>\n\n<cookies>\n"
for cookie in self.cookies:
xml += cookie.generateXML + "\n"
xml += "\n</cookies>"
# test/sample code (modify for your particular cookies file)
c = CookieInfo("cookies.txt")
print "You have: " + str(c.count()) + " cookies!"
# prints out 3 data element from www.chapters.ca's cookie
cookie = c.findCookieByURL("www.chapters.ca")
if cookie != false:
print "Here is the 3th piece of data from the cookie made by www.chapters.ca: " + cookie.getData3()
else:
print "The URL you have provided doesn't exist in your cookies file!"
# prints out the url's of all cookies with "mark" in them
print "Here are the url's of the cookies that have 'mail' somewhere within their content: "
for cookie in c.findCookiesByString("mail"):
print cookie.getUrl()
# prints out the sql and xml for the www.chapters.ca cookie
cookie = c.findCookieByURL("www.chapters.ca")
print "Here is the SQL for the www.chapters.ca cookie: " + cookie.generateSQL()
print "Here is the XML for the www.chapters.ca cookie: " + cookie.generateXML()
|
These classes let people forget about parsing cookies, and just start using them as objects. The "Cookie" generateSQL and generateXML methods may have to be modified depending on your preferences and data schema.