コード例 #1
0
ファイル: soapClient.py プロジェクト: Aspediens/codesync
 def __init__(self, table, instance):
     self.__table = table
     self.__instance = instance
     username = Config.getUsername(instance)
     password = Config.getPassword(instance)
     SLogger.debug("Connecting to %s to download from table %s" % (instance, table))
     try:
         Client.__init__(self,
                         instance + table + "_list.do?WSDL",
                         username=username,
                         password=password,
                         timeout=Config.getTimeout())
     except URLError, e:
         SnowClient.__processURLError(e)
コード例 #2
0
ファイル: test_config.py プロジェクト: foomango/isay
class ConfigTestCase(TestCase):
    """Unit test for Config
    """

    def setUp(self):
        Config.CONFIG_FILE = '/tmp/isay/isay.conf'
        self.conf = Config()

    def test_read(self):
        pass

    def test_getUsername(self):
        username = self.conf.getUsername()
        self.assertEquals('test', username)

    def test_getPasswd(self):
        password = self.conf.getPasswd()
        self.assertEquals('test', password)

    def test_getAutosave(self):
        autosave = self.conf.getAutosave()
        self.assertEquals('true', autosave)
コード例 #3
0
ファイル: shanbay.py プロジェクト: foomango/isay
class ShanBay(object):
    """Translate word by shanbay.com
    """

    cookiePath = os.path.join(os.path.expanduser('~'),
                              '.isay', '.cookie.txt')

    def __init__(self):
        self.url = 'http://www.shanbay.com/api/v1/bdc/search?%s'
        self.loginUrl = 'http://shanbay.com/accounts/login/'
        self.learnUrl = 'http://shanbay.com/api/v1/bdc/learning/'

        self.conf = Config()

        self.cj = cookielib.MozillaCookieJar(self.cookiePath)
        self.loadCookieJar()

        if 'true' == self.conf.getAutosave():
            self.opener = urllib2.build_opener(
                urllib2.HTTPCookieProcessor(self.cj))
        else:
            self.opener = urllib2.build_opener()

    def getUsername(self):
        """Get user name
        Args:
            return: str, user name
        """
        return self.conf.getUsername()

    def getPasswd(self):
        """Get pass word
        Args:
            return: str, pass word
        """
        return self.conf.getPasswd()

    def getAutosave(self):
        """Get auto save
        Args:
            return: bool, is auto save
        """
        return 'true' == self.conf.getAutosave()

    def loadCookieJar(self):
        """Load cookies from file, if file does not exist, create it
        """
        try:
            self.cj.load()
        # cookie file not found, create it
        except IOError:
            self.cj.save()

    def translate(self, word):
        """Translate word
        Args:
            word: string, word to be translated
            return: string, meaning
        """

        meaning = 'Unknown'

        params = urllib.urlencode({'word': word})
        url = self.url % params
        data = self.getJson(url, [])

        if data['status_code'] == 0:
            phonetics = '[' + data['data']['pronunciation'] + ']'
            meaning = data['data']['content'] + phonetics + '\n'
            meaning += data['data']['definition'].replace('\n', '\n ')

            if self.getAutosave() and 'learning_id' not in data['data']:
                id = data['data']['content_id']
                self.addToLearningList(id)
        else:
            meaning = data['msg']

        return meaning

    def addToLearningList(self, id):
        """Add word to learning list
        Args:
            id: int, id of word
            return: json, json received from the web
        """
        username = self.getUsername()
        password = self.getPasswd()

        url = self.learnUrl
        headers = {'X-Requested-With': 'XMLHttpRequest'}
        data = {'id': id, 'content_type': 'vocabulary'}
        try:
            jsonData = self.postJson(url, data, headers)
        except Exception:
            self.login(username, password)
            jsonData = self.postJson(url, data, headers)

        return jsonData

    def request(self, url, params):
        """Send request to shanbay.com
        Args:
            params: dict, parameters
                    url, url to request
            return: file-like object,
        """
        doc = urllib.urlopen(url)
        return doc
        pass

    def getHeaders(self, url):
        """Get http headers
        Args:
            url: string, url to get
            return:
        """
        doc = urllib.urlopen(url)
        return doc.info().headers

    def parseCookies(self, headers):
        """Parse cookies from http headers
        Args:
            headers: list, http headers
            return: dict, cookies
        """
        cookies = {}
        cookieReg = re.compile(r'^Set-Cookie:\s*(\w+)=(\w*);')

        for header in headers:
            m = cookieReg.match(header)
            if m:
                cookies[m.group(1)] = m.group(2)

        return cookies

    def getJson(self, url, headers=[]):
        """Get json from url
        Args:
            url: str, url
            headers: str, headers
            return: json, reponse
        """
        self.opener.addheaders = headers
        response = self.opener.open(url)
        data = response.read()
        jsonData = json.loads(data)

        if self.getAutosave():
            self.cj.save()
        return jsonData

    def postJson(self, url, data, headers=[]):
        """Post json to url
        Args:
            url: str, url
            data: list, data
            headers: list, headers
        """
        extraHeaders = {'content-type': 'application/json'}
        cookies = 'sessionid=' +\
            self.cj._cookies['.shanbay.com']['/']['sessionid'].value
        cookiesHeader = {'Cookie:': cookies}
        headers.update(extraHeaders)
        headers.update(cookiesHeader)
        data = json.dumps(data)

        req = urllib2.Request(url, data, headers)
        resp = urllib2.urlopen(req)
        rawData = resp.read()
        jsonData = json.loads(rawData)

        return jsonData

    def login(self, name, passwd):
        """Login shanbay.com
        Args:
            name: str, user name
            passwd: str, password
        """

        class NoRedirection(urllib2.HTTPErrorProcessor):
            def http_response(self, request, response):
                return response

        # empty cookie
        self.cj.clear()

        # get csrf
        self.opener.open(self.loginUrl)

        # login
        loginData = urllib.urlencode({
            'csrfmiddlewaretoken': self.getCsrftoken(),
            'username': name,
            'password': passwd
        })
        opener = urllib2.build_opener(NoRedirection,
                                      urllib2.HTTPCookieProcessor(self.cj))
        response = opener.open(self.loginUrl, loginData)
        if response.getcode() != 302:
            print '[Warning] Login failed'
        self.cj.save()

    def getCsrftoken(self):
        """Get csrftoken
        Args:
            return: string, csrftoken
        """

        csrftoken = ''

        try:
            csrftoken = self.cj._cookies['.shanbay.com']['/']['csrftoken'].value
        except Exception:
            csrftoken = ''

        return csrftoken
コード例 #4
0
ファイル: test_shanbay.py プロジェクト: foomango/isay
class ShanBayTestCase(TestCase):
    """Unit test for ShanBay
    """

    def setUp(self):
        self.cookiePath = '/tmp/isay/.testcookie.txt'
        cj = cookielib.MozillaCookieJar(self.cookiePath)
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        opener.open('http://shanbay.com/accounts/login/')
        cj.save()
        self.cj = cj

        ShanBay.cookiePath = self.cookiePath
        self.shanBay = ShanBay()

        Config.CONFIG_FILE = '/tmp/isay/isay_real.conf'
        self.conf = Config()
        self.shanBay.conf = self.conf

    def login(self):
        username = self.conf.getUsername()
        passwd = self.conf.getPasswd()
        self.shanBay.login(username, passwd)

    def test_getUsername(self):
        username = self.conf.getUsername()
        self.assertEquals(username, self.shanBay.getUsername())

    def test_getPasswd(self):
        passwd = self.conf.getPasswd()
        self.assertEquals(passwd, self.shanBay.getPasswd())

    def test_getAutosave(self):
        autosave = 'true' == self.conf.getAutosave()
        self.assertEquals(autosave, self.shanBay.getAutosave())

    def test_loadCookieJar(self):
        self.shanBay.loadCookieJar()

    def test_translate(self):
        self.login()
        self.assertTrue('int' in self.shanBay.translate('hi'))

    def test_addToLearningList(self):
        jsonData = self.shanBay.addToLearningList(3130)
        self.assertEquals(0, jsonData['status_code'])

    def test_getHeaders(self):
        url = 'http://shanbay.com/accounts/login/'
        headers = self.shanBay.getHeaders(url)
        self.assertGreater(len(headers), 0)

    def test_parseCookies(self):
        headers = []
        headers.append('Set-Cookie: csrftoken=foo; domain=foo.com')
        headers.append('Date: Mon, 04 May 2015 09:58:06 GMT\r\n')
        headers.append('Set-Cookie: user=; domain=foo.com')
        cookies = self.shanBay.parseCookies(headers)
        self.assertEquals('foo', cookies['csrftoken'])
        self.assertEquals('', cookies['user'])

    def test_getJson(self):
        self.login()

        url = 'http://www.shanbay.com/api/v1/bdc/search?%s'
        params = urllib.urlencode({'word': 'hello'})
        jsonData = self.shanBay.getJson(url % params)

        self.assertEquals(0, jsonData['status_code'])

    def test_postJson(self):
        self.login()

        url = 'http://shanbay.com/api/v1/bdc/learning/'
        headers = {'X-Requested-With': 'XMLHttpRequest'}
        data = {"id": 3130, "content_type": "vocabulary"}
        jsonData = self.shanBay.postJson(url, data, headers)
        self.assertEquals(0, jsonData['status_code'])

    def test_login(self):
        self.login()
        self.cj.load()
        sessionid = self.cj._cookies['.shanbay.com']['/']['sessionid'].value
        self.assertNotEquals('', sessionid)

    def test_getCsrftoken(self):
        self.assertNotEquals('', self.shanBay.getCsrftoken())