Ejemplo n.º 1
0
class Test1AccessTokenSecret(unittest.TestCase):
    def setUp(self):
        try:
            file = open('API.keys', 'r+')
        except IOError:
            print("You need to put key/secret in API.keys")
            raise
        except:
            print("Unexpected error:", sys.exc_info()[0])
        else:
            data = json.load(file)
            file.close()
            self.plurk = PlurkAPI(data["CONSUMER_KEY"], data["CONSUMER_SECRET"])

    def teardown(self):
        pass

    def test_invalid_access_key(self):
        self.plurk.authorize("foor", "bar")
        r = self.plurk.callAPI('/APP/Profile/getOwnProfile')
        self.assertIsNone(r)
        err = self.plurk.error()
        self.assertEqual(err['code'], "400")
        self.assertEqual(err['reason'], "BAD REQUEST")
        self.assertEqual(err['content']['error_text'],
                         "40106:invalid access token")
Ejemplo n.º 2
0
class Plurker(Publisher):
    def __init__(
        self,
        app_key: Optional[str] = None,
        app_secret: Optional[str] = None,
        access_token: Optional[str] = None,
        access_secret: Optional[str] = None,
        stats: Optional[PlurkerStats] = None,
    ) -> None:
        """
        Will try to fetch `PLURK_APP_KEY`, `PLURK_APP_SECRET`,
        `PLURK_USER_TOKEN`, `PLURK_USER_SECRET` from environment variables
        if `app_key`, `app_secret`, `access_token`, `access_secret` weren't provided.

        """
        APP_KEY = os.getenv('PLURK_APP_KEY', app_key)
        APP_SECRET = os.getenv('PLURK_APP_SECRET', app_secret)
        ACCESS_TOKEN = os.getenv('PLURK_USER_TOKEN', access_token)
        ACCESS_TOKEN_SECRET = os.getenv('PLURK_USER_SECRET', access_secret)

        self.plurk = PlurkAPI(key=APP_KEY,
                              secret=APP_SECRET,
                              access_token=ACCESS_TOKEN,
                              access_secret=ACCESS_TOKEN_SECRET)
        self._stats = stats or PlurkerStats()
        super().__init__()

    @property
    def stats(self):
        return self._stats

    def publish(self, *, content):
        """
        :raise PublishError: raise error when publish failed.
        """
        self.stats.start()
        response = self._publish(content)
        self.stats.finish()
        return response

    def _publish(self, content):
        response = self.plurk.callAPI("/APP/Timeline/plurkAdd",
                                      options=content)
        if response:
            self.stats.sending_success()
            return response
        else:
            self.stats.sending_failed()
            error = self.plurk.error()
            msg = error['reason']

            if 'error_text' in error['content']:
                msg = error['content']['error_text']

            raise PublishError(publisher=self, reason=msg, caused_by=content)
Ejemplo n.º 3
0
class Test1AccessTokenSecret(unittest.TestCase):
    def setUp(self):
        pass

    def teardown(self):
        pass

    def test_invalid_access_key(self):
        self.plurk = PlurkAPI("key", "secret")
        self.plurk.authorize("foor", "bar")
        r = self.plurk.callAPI('/APP/Profile/getOwnProfile')
        self.assertIsNone(r)
        err = self.plurk.error()
        self.assertEqual(err['code'], 400)
        self.assertEqual(err['reason'], "BAD REQUEST")
        self.assertEqual(err['content']['error_text'],
                         "40106:invalid access token")
Ejemplo n.º 4
0
class Test1AccessTokenSecret(unittest.TestCase):
    def setUp(self):
        pass

    def teardown(self):
        pass

    def test_invalid_access_key(self):
        self.plurk = PlurkAPI("key", "secret")
        self.plurk.authorize("foor", "bar")
        r = self.plurk.callAPI('/APP/Profile/getOwnProfile')
        self.assertIsNone(r)
        err = self.plurk.error()
        self.assertEqual(err['code'], 400)
        self.assertEqual(err['reason'], "BAD REQUEST")
        self.assertEqual(err['content']['error_text'],
                         "40106:invalid access token")
Ejemplo n.º 5
0
class Test0ConsumerTokenSecret(unittest.TestCase):
    def setUp(self):
        pass

    def teardown(self):
        pass

    def test_no_consumer_key(self):
        with self.assertRaises(ValueError):
            self.plurk = PlurkAPI()
            self.plurk.callAPI('/APP/Profile/getPublicProfile',
                               {'user_id': 'clsung'})

    def test_invalid_consumer_key(self):
        self.plurk = PlurkAPI("token", "secret")
        r = self.plurk.callAPI('/APP/Profile/getPublicProfile',
                               {'user_id': 'clsung'})
        self.assertIsNone(r)
        err = self.plurk.error()
        self.assertEqual(err['code'], 400)
        self.assertEqual(err['reason'], "BAD REQUEST")
        self.assertEqual(err['content']['error_text'],
                         "40101:unknown application key")
Ejemplo n.º 6
0
class Test0ConsumerTokenSecret(unittest.TestCase):
    def setUp(self):
        pass

    def teardown(self):
        pass

    def test_no_consumer_key(self):
        with self.assertRaises(ValueError):
            self.plurk = PlurkAPI()
            self.plurk.callAPI('/APP/Profile/getPublicProfile',
                               {'user_id': 'clsung'})

    def test_invalid_consumer_key(self):
        self.plurk = PlurkAPI("token", "secret")
        r = self.plurk.callAPI('/APP/Profile/getPublicProfile',
                               {'user_id': 'clsung'})
        self.assertIsNone(r)
        err = self.plurk.error()
        self.assertEqual(err['code'], 400)
        self.assertEqual(err['reason'], "BAD REQUEST")
        self.assertEqual(err['content']['error_text'],
                         "40101:unknown application key")