Beispiel #1
0
    def _poll(self):
        try:
            start = time.time()
            while not self._abort and time.time() - start < 300:
                try:
                    response = http.GET(self.POLL.format(self.id))
                except Exception, e:
                    util.ERROR('PinLogin connection error: {0}'.format(
                        e.__class__),
                               err=e)
                    time.sleep(self.POLL_INTERVAL)
                    continue

                if response.status_code != http.codes.ok:
                    self._expired = True
                    break
                try:
                    data = ElementTree.fromstring(
                        response.text.encode('utf-8'))
                except Exception, e:
                    util.ERROR('PinLogin data error: {0}'.format(e.__class__),
                               err=e)
                    time.sleep(self.POLL_INTERVAL)
                    continue

                token = data.find('auth_token').text
                if token:
                    self.authenticationToken = token
                    break
                time.sleep(self.POLL_INTERVAL)
Beispiel #2
0
def fetchResources(token):
    headers = util.BASE_HEADERS.copy()
    headers['X-Plex-Token'] = token
    util.LOG('GET {0}?X-Plex-Token={1}'.format(RESOURCES, util.hideToken(token)))
    response = http.GET(RESOURCES)
    data = ElementTree.fromstring(response.text.encode('utf8'))
    import plexserver
    return [plexserver.PlexServer(elem) for elem in data]
Beispiel #3
0
 def sign(self, channel, expires=60):
     """
     创建并获取频道口令
     """
     resp = http.GET('%ssign?cname=%s&expires=%s' % (
         self.admin_url,
         channel,
         expires,
     ))
     return resp.json()
Beispiel #4
0
 def push(self, channel, data):
     "推送消息"
     try:
         resp = http.GET('%spush?cname=%s&content=%s' % (
             self.admin_url,
             channel,
             utils.urlencode(data),
         ))
         return resp.json()
     except:
         return False
Beispiel #5
0
    def asyncAction(self, actions, verb, **params):
        expectedActionLink(actions, verb)
        ret = http.POST(self.opts, self.abs(actions.link[verb].href),
                        self.makeAction('true', '5000', **params).dump(),
                        self.fmt.MEDIA_TYPE)
        expectedStatusCode(ret, 202)
        resp_action = self.fmt.parse(ret['body'])
        unexpectedActionStatus(resp_action.status, "COMPLETE")
        for i in range(1, 3):
            time.sleep(1)
            resp = http.GET(self.opts, self.abs(resp_action.href),
                            self.fmt.MEDIA_TYPE)
            expectedStatusCode(resp, 200)
            resp_action = self.fmt.parse(resp['body'])
            unexpectedActionStatus(resp_action.status, "COMPLETE")

        time.sleep(4)
        resp = http.GET(self.opts, self.abs(resp_action.href),
                        self.fmt.MEDIA_TYPE)
        expectedStatusCode(resp, 200)
        resp_action = self.fmt.parse(resp['body'])
        expectedActionStatus(resp_action.status, "COMPLETE")
Beispiel #6
0
    def broadcast(self, data, delay=0):
        "广播一条消息"
        if not isinstance(data, (str, unicode)):
            data = utils.json_dumps(data)

        if delay > 0:
            utils.sleep(delay)

        try:
            resp = http.GET('%sbroadcast?content=%s' %
                            (self.admin_url, utils.urlencode(data)))
            return resp.text.strip().lower() == 'ok'
        except:
            return False
Beispiel #7
0
def _ip2location(ip):
    """
    将 ip 转换为位置信息
    :param ip: ip 地址
    :return:
    """
    assert (ip)
    try:
        resp = http.GET(settings.JUHE.IP.URL % (
            settings.JUHE.IP.KEY,
            ip,
        ))
        data = resp.json()
        if data and data['resultcode'] == '200':
            return data['result']
    except:
        pass

    return None
Beispiel #8
0
    def _poll(self):
        try:
            start = time.time()
            while not self._abort and time.time() - start < 300:
                response = http.GET(self.POLL.format(self.id))
                if response.status_code != http.codes.ok:
                    self._expired = True
                    break
                data = ElementTree.fromstring(response.text.encode('utf-8'))
                token = data.find('auth_token').text
                if token:
                    self.authenticationToken = token
                    break
                time.sleep(self.POLL_INTERVAL)

            if self._callback:
                self._callback(self.authenticationToken)
        finally:
            self._finished = True
Beispiel #9
0
 def query(self, href, constraint):
     t = template_parser.URITemplate(self.abs(href))
     qhref = t.sub({"query": constraint})
     ret = http.GET(self.opts, qhref, self.fmt.MEDIA_TYPE)
     expectedStatusCode(ret, 200)
     return self.fmt.parse(ret['body'])
Beispiel #10
0
 def unauth(self, href):
     unauth_opts = copy.deepcopy(self.opts)
     unauth_opts['user'] = None
     unauth_opts['secret'] = None
     ret = http.GET(unauth_opts, self.abs(href), self.fmt.MEDIA_TYPE)
     expectedStatusCode(ret, 401)
Beispiel #11
0
 def get(self, href):
     ret = http.GET(self.opts, self.abs(href), self.fmt.MEDIA_TYPE)
     expectedStatusCode(ret, 200)
     return self.fmt.parse(ret['body'])