Results 1 to 2 of 2

Thread: [Python 3.3] Neo Connection Class

  1. #1
    Zachafer's Avatar
    Joined
    Dec 2011
    Posts
    1,235
    Userbars
    11
    Thanks
    769
    Thanked
    1,466/678
    DL/UL
    98/0
    Mentioned
    512 times
    Time Online
    24d 13h 9m
    Avg. Time Online
    8m

    [Python 3.3] Neo Connection Class

    Code:
    import http.cookiejar, urllib.request, urllib.parse
    
    class NeoAccount:
        """A simple Neopets connection class"""
        def __init__(self, un, pw):
            self.username = un
            self.password = pw
            cookie_handler = urllib.request.HTTPCookieProcessor()
            self._opener = urllib.request.build_opener(cookie_handler)
            self._opener.addheaders = [('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0')]
            self.referer = None
    
        def login(self):
            self.clearCookies()
            data = {"destination" : "/index.phtml",
                    "username" : self.username,
                    "password" : self.password}
            html = self.post("http://www.neopets.com/login.phtml", data)
            return "Welcome, " in html
    
        def get(self, url, referer = ""):
            if referer is not None:
                self._opener.addheaders = [('Referer', referer)]
            return str(self._opener.open(url).read())
    
        def post(self, url, parameters, referer = ""):
            data = urllib.parse.urlencode(parameters)
            data = data.encode('UTF-8')
            if referer is not None:
                self._opener.addheaders = [('Referer', referer)]
            return str(self._opener.open(url, data).read())
    
        def clearCookies(self):
            self.cj = http.cookiejar.CookieJar()
    Example usage:
    Code:
    username = input("Neopets username: ")
    password = input("Neopets password: ")
    
    acc = NeoAccount(username, password)
    if acc.login():
        print("Logged into " + username)
        html = acc.get("http://www.neopets.com/inventory.phtml")
        match = re.search("Total Items: <b>([0-9]+)</b>", html)
        if match:
            itemCount = match.group(1)
        else:
            itemCount = "0"
        print(username + " has " + itemCount + " items!")
    else:
        print("Failed to login to " + username)

  2. The Following 5 Users Say Thank You to Zachafer For This Useful Post:

    DarkByte (02-05-2015),Demetri (09-28-2013),fingerprints (09-30-2013),Ghosts (02-06-2015),j03 (09-28-2013)

  3. #2

    Joined
    Jun 2012
    Posts
    1,699
    Thanks
    876
    Thanked
    2,881/1,142
    DL/UL
    44/1
    Mentioned
    562 times
    Time Online
    118d 6h 45m
    Avg. Time Online
    40m
    I need to get learning python 3 , dont think its to different and apparently fixes alot of the encoding issues ive been having lately simulating certain packets that are outside of the standard charset range. Thanks for the class .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •