<commit_before>#!/usr/bin/env python
 
import httplib, urllib

class BukkitForum(object):
  host      = 'forums.bukkit.org'
  username  = None
  password  = <PASSWORD>
  cookies   = {}
  headers   = {
   'Cache-Control': 'max-age=0',
 'Accept-Language': 'en-US,en;q=0.8',
  'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
          'Accept': 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
      'User-Agent': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-US) '+\
                    'AppleWebKit/534.16 (KHTML, like Gecko) '+\
                    'Chrome/10.0.648.205 Safari/534.16',
  }
  
  def __init__(self, username, password):
    self.username = username
    self.password = password
  
  def __update_cookies(self, cookies):
    print cookies
    if cookies is not None:
      for cookie in cookies.split(';'):
        dset        = cookie.split('=')
        if len(dset) > 1:
          if dset[0] in ['f_user', 'f_session', 'IDstack']:
            self.cookies[dset[0]] = dset[1]
  
  def __logged_in(self, page):
    if page[:100].find('class="Public LoggedOut"') > -1:
      return False
    elif page[:100].find('class="Public LoggedIn"') > -1:
      return True
    else:
      return 'UNKNOWN'
  
  def __get_cookies(self):
    cookies = []
    for cookie in self.cookies:
      cookies.append('%s=%s; ' % (cookie, self.cookies[cookie]))
    print '; '.join(cookies)
    return '; '.join(cookies)
  
  def __get(self, loc):
    con           = httplib.HTTPConnection(self.host)
    cookies       = self.__get_cookies()
    headers       = self.headers
    if cookies is not '':
      headers['Cookie'] = cookies
    con.request('GET', loc, headers=headers)
    resp          = con.getresponse()
    print resp.getheaders()
    self.__update_cookies(resp.getheader('set-cookie'))
    newloc        = resp.getheader('location')
    if newloc is not None:
      print 'Chasing Referral: %s' % newloc
      page        = self.__get(newloc)
    else:
      page        = resp.read()
    return page
  
  def __post(self, loc, formdata):
    con           = httplib.HTTPConnection(self.host)
    cookies       = self.__get_cookies()
    headers       = self.headers
    payload       = urllib.urlencode(formdata)
    if cookies is not '':
      headers['Cookie']       = cookies
    headers['Host']           = 'forums.bukkit.org'
    headers['Origin']         = 'http://forums.bukkit.org/'
    headers['Referer']        = 'http://forums.bukkit.org/'
    headers['Content-Type']   = 'application/x-www-form-urlencoded'
    headers['Content-Length'] = len(payload)
    print headers
    con.request('POST', loc, headers=headers, body=payload)
    resp          = con.getresponse()
    print resp.getheaders()
    self.__update_cookies(resp.getheader('set-cookie'))
    newloc        = resp.getheader('location')
    if newloc is not None:
      print 'Chasing Referral: %s' % newloc
      page        = self.__get(newloc)
    else:
      page        = resp.read()
    return page
  
  def login(self):
    #self.__get('/')
    formdata  = {
         'login': self.username,
      'register': 0,
      'password': <PASSWORD>,
      'remember': 0,
  'cookie_check': 1,
      'redirect': '/',
      '_xfToken': '',
    }
    page = self.__post('/login', formdata)
    return self.__logged_in(page)
  
  def private_message(self, user, message, locked=False):
    pass
    
    

if __name__ == '__main__':
  forum   = BukkitForum('XXX', 'XXX')
  print forum.login()
  print forum.cookies<commit_msg>Minor changes to testing class.<commit_after>#!/usr/bin/env python
 
import httplib, urllib

class XenForo(object):
  host      = None
  username  = None
  password  = <PASSWORD>
  cookies   = {}
  headers   = {
   'Cache-Control': 'max-age=0',
 'Accept-Language': 'en-US,en;q=0.8',
  'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
          'Accept': 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
      'User-Agent': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-US) '+\
                    'AppleWebKit/534.16 (KHTML, like Gecko) '+\
                    'Chrome/10.0.648.205 Safari/534.16',
  }
  
  def __init__(self, username, password, host):
    self.username = username
    self.password = password
    self.host     = host
  
  def _update_cookies(self, cookies):
    print cookies
    if cookies is not None:
      for cookie in cookies.split(';'):
        dset        = cookie.split('=')
        if len(dset) > 1:
          if dset[0] in ['f_user', 'f_session', 'IDstack']:
            self.cookies[dset[0]] = dset[1]
  
  def _logged_in(self, page):
    if page[:100].find('class="Public LoggedOut"') > -1:
      return False
    elif page[:100].find('class="Public LoggedIn"') > -1:
      return True
    else:
      return 'UNKNOWN'
  
  def _get_cookies(self):
    cookies = []
    for cookie in self.cookies:
      cookies.append('%s=%s; ' % (cookie, self.cookies[cookie]))
    print '; '.join(cookies)
    return '; '.join(cookies)
  
  def _get(self, loc):
    con           = httplib.HTTPConnection(self.host)
    cookies       = self._get_cookies()
    headers       = self.headers
    if cookies is not '':
      headers['Cookie'] = cookies
    con.request('GET', loc, headers=headers)
    resp          = con.getresponse()
    print resp.getheaders()
    self._update_cookies(resp.getheader('set-cookie'))
    newloc        = resp.getheader('location')
    if newloc is not None:
      print 'Chasing Referral: %s' % newloc
      page        = self._get(newloc)
    else:
      page        = resp.read()
    return page
  
  def _post(self, loc, formdata):
    con           = httplib.HTTPConnection(self.host)
    cookies       = self._get_cookies()
    headers       = self.headers
    payload       = urllib.urlencode(formdata)
    if cookies is not '':
      headers['Cookie']       = cookies
    headers['Host']           = 'forums.bukkit.org'
    headers['Origin']         = 'http://forums.bukkit.org/'
    headers['Referer']        = 'http://forums.bukkit.org/'
    headers['Content-Type']   = 'application/x-www-form-urlencoded'
    headers['Content-Length'] = len(payload)
    print headers
    con.request('POST', loc, headers=headers, body=payload)
    resp          = con.getresponse()
    print resp.getheaders()
    self._update_cookies(resp.getheader('set-cookie'))
    newloc        = resp.getheader('location')
    if newloc is not None:
      print 'Chasing Referral: %s' % newloc
      page        = self.__get(newloc)
    else:
      page        = resp.read()
    return page
  
  def login(self):
    self.__get('/')
    formdata  = {
         'login': self.username,
      'register': 0,
      'password': <PASSWORD>,
      'remember': 0,
  'cookie_check': 1,
      'redirect': '/',
      '_xfToken': '',
    }
    page = self._post('/login/login', formdata)
    return self._logged_in(page)
  
  def private_message(self, user, message, locked=False):
    pass
    
    

if __name__ == '__main__':
  forum   = XenForo('XXX', 'XXX', 'forums.bukkit.org')
  print forum.login()
  print forum.cookies
{'fraction_non_alphanumeric': 0.15033500837520938, 'fraction_numerical': 0.016471245114461196, 'mean_word_length': 2.3685942642219087, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '305720', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2435, 'n_words': 493}