Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. import httplib, urllib, json, re, getpass, operator
  2.  
  3. class DailyBooth:
  4.   domain = 'dailybooth.com'
  5.   id, username, password = 0, '', ''
  6.  
  7.   def loop(self):
  8.     print '' # new line? -.-
  9.     commands = {'1': 'Login', '2': 'Dashboard', '3': 'Upload photo', '4': 'Logout', '5': 'Login check', '9': 'Exit'}
  10.     map(operator.itemgetter(0),commands) # right order? werid stuff -.-
  11.     for num, item in sorted(commands.iteritems()):
  12.       print num + ': ' + item
  13.     try:
  14.       action = int(raw_input('What do you want to do? '))
  15.     except:
  16.       action = 0
  17.     print '' # new line? -.-
  18.    
  19.     if action == 0:
  20.       print 'Invalid command'
  21.    
  22.     if action == 1:
  23.       uname = raw_input('Username: ')
  24.       pword = getpass.getpass('Password: ')
  25.       if self.login(uname,pword):
  26.         print 'Logged in!'
  27.       else:
  28.         print 'Login failed..'
  29.    
  30.     if action == 2:
  31.       self.dashboard()
  32.    
  33.     if action == 3:
  34.       print 'Soon..'
  35.    
  36.     if action == 4:
  37.       print 'Soon..'
  38.    
  39.     if action == 5:
  40.       if self.logged_in():
  41.         print 'Yes, you are logged in.'
  42.       else:
  43.         print 'No, you are not logged in'
  44.    
  45.     if action == 9:
  46.       self.loop = False
  47.  
  48.   def login(self, username, password):
  49.     print 'Logging in...'
  50.     h = httplib.HTTPConnection(self.domain)
  51.     param = urllib.urlencode({'do': 'login', 'submit': 'Sign In', 'username': username, 'password': password})
  52.     header = {"Content-type": "application/x-www-form-urlencoded",
  53.                "Accept": "text/plain"}
  54.     h.request("POST", "/login?ajax", param, header)
  55.     response = h.getresponse()
  56.     if response.status == 200:
  57.       content = response.read()
  58.       jsc = json.loads(content)
  59.       print jsc['message']
  60.       if jsc['success'] == 1:
  61.         cookies = response.getheader('set-cookie')
  62.         self.cookie = cookies;
  63.         self.id = re.search("user_id=(\d+);", cookies).groups()[0]
  64.         self.username = re.search("username=([a-zA-Z0-9]+);", cookies).groups()[0]
  65.         self.password = re.search("password=([a-zA-Z0-9]+);", cookies).groups()[0]
  66.         return True
  67.     return False
  68.  
  69.   def logged_in(self):
  70.     try:
  71.       if self.id != 0 and self.username != '' and self.password != '':
  72.         return True
  73.       else:
  74.         return False
  75.     except:
  76.       return False
  77.  
  78.   def dashboard(self):
  79.     if self.logged_in():
  80.       print 'Dashboard'
  81.       h = httplib.HTTPConnection(self.domain)
  82.       h.putrequest("GET", "/dashboard")
  83.       h.putheader('Cookie','user_id=' + self.id + '; username=' + self.username + '; password=' + self.password + ';')
  84.       h.endheaders()
  85.       response = h.getresponse()
  86.       if response.status == 200:
  87.         content = response.read()
  88.         if re.search("<title>An Error has Occurred<\/title>", content): #doesn't work yet..
  89.           print 'Something went wrong..'
  90.         else:
  91.           print 'Dashboard here..'
  92.       else:
  93.         print 'Failed retreiving dashboard..'
  94.     else:
  95.       print 'You have to be logged in to view the dashboard!'
  96.  
  97. print 'DailyBooth v0.1 - by Michael Owens'
  98. db = DailyBooth()
  99.  
  100. while db.loop != False:
  101.   db.loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement