<commit_before>#!/usr/bin/env python
#
# Copyright 2010 Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""Python client library for the Facebook Platform.

This client library is designed to support the Graph API and the official
Facebook JavaScript SDK, which is the canonical way to implement
Facebook authentication. Read more about the Graph API at
http://developers.facebook.com/docs/api. You can download the Facebook
JavaScript SDK at http://github.com/facebook/connect-js/.

If your application is using Google AppEngine's webapp framework, your
usage of this module might look like this:

    user = facebook.get_user_from_cookie(self.request.cookies, key, secret)
    if user:
        graph = facebook.GraphAPI(user["access_token"])
        profile = graph.get_object("me")
        friends = graph.get_connections("me", "friends")

"""

import cgi
import time
import urllib
import urllib2
import hashlib
import hmac
import base64
import logging

# Find a JSON parser
try:
    import simplejson as json
except ImportError:
    try:
        from django.utils import simplejson as json
    except ImportError:
        import json
_parse_json = json.loads

class GraphAPI(object):
    """A client for the Facebook Graph API.

    See http://developers.facebook.com/docs/api for complete documentation
    for the API.

    The Graph API is made up of the objects in Facebook (e.g., people, pages,
    events, photos) and the connections between them (e.g., friends,
    photo tags, and event RSVPs). This client provides access to those
    primitive types in a generic way. For example, given an OAuth access
    token, this will fetch the profile of the active user and the list
    of the user's friends:

       graph = facebook.GraphAPI(access_token)
       user = graph.get_object("me")
       friends = graph.get_connections(user["id"], "friends")

    You can see a list of all of the objects and connections supported
    by the API at http://developers.facebook.com/docs/reference/api/.

    You can obtain an access token via OAuth or by using the Facebook
    JavaScript SDK. See http://developers.facebook.com/docs/authentication/
    for details.

    If you are using the JavaScript SDK, you can use the
    get_user_from_cookie() method below to get the OAuth access token
    for the active user from the cookie saved by the SDK.
    """
    def __init__(self, access_token=None):
        self.access_token = access_token

    def get_object(self, id, **args):
        """Fetchs the given object from the graph."""
        return self.request(id, args)

    def get_objects(self, ids, **args):
        """Fetchs all of the given object from the graph.

        We return a map from ID to object. If any of the IDs are invalid,
        we raise an exception.
        """
        args["ids"] = ",".join(ids)
        return self.request("", args)

    def get_connections(self, id, connection_name, **args):
        """Fetchs the connections for given object."""
        return self.request(id + "/" + connection_name, args)

    def put_object(self, parent_object, connection_name, **data):
        """Writes the given object to the graph, connected to the given parent.

        For example,

            graph.put_object("me", "feed", message="Hello, world")

        writes "Hello, world" to the active user's wall. Likewise, this
        will comment on a the first post of the active user's feed:

            feed = graph.get_connections("me", "feed")
            post = feed["data"][0]
            graph.put_object(post["id"], "comments", message="First!")

        See http://developers.facebook.com/docs/api#publishing for all of
        the supported writeable objects.

        Most write operations require extended permissions. For example,
        publishing wall posts requires the "publish_stream" permission. See
        http://developers.facebook.com/docs/authentication/ for details about
        extended permissions.
        """
        assert self.access_token, "Write operations require an access token"
        return self.request(parent_object + "/" + connection_name, post_args=data)

    def put_wall_post(self, message, attachment={}, profile_id="me"):
        """Writes a wall post to the given profile's wall.

        We default to writing to the authenticated user's wall if no
        profile_id is specified.

        attachment adds a structured attachment to the status message being
        posted to the Wall. It should be a dictionary of the form:

            {"name": "Link name"
             "link": "http://www.example.com/",
             "caption": "{*actor*} posted a new review",
             "description": "This is a longer description of the attachment",
             "picture": "http://www.example.com/thumbnail.jpg"}

        """
        return self.put_object(profile_id, "feed", message=message, **attachment)

    def put_comment(self, object_id, message):
        """Writes the given comment on the given post."""
        return self.put_object(object_id, "comments", message=message)

    def put_like(self, object_id):
        """Likes the given post."""
        return self.put_object(object_id, "likes")

    def delete_object(self, id):
        """Deletes the object with the given ID from the graph."""
        self.request(id, post_args={"method": "delete"})

    def put_photo(self, image, message=None, album_id=None, **kwargs):
        """Uploads an image using multipart/form-data
        image=File like object for the image
        message=Caption for your image
        album_id=None posts to /me/photos which uses or creates and uses
        an album for your application.
        """
        object_id = album_id or "me"
        #it would have been nice to reuse self.request; but multipart is messy in urllib
        post_args = {
				  'access_token': self.access_token,
				  'source': image,
				  'message': message
        }
        post_args.update(kwargs)
        content_type, body = self._encode_multipart_form(post_args)
        req = urllib2.Request("https://graph.facebook.com/%s/photos" % object_id, data=body)
        req.add_header('Content-Type', content_type)
        try:
            data = urllib2.urlopen(req).read()
        #For Python 3 use this:
        #except urllib2.HTTPError as e:
        except urllib2.HTTPError, e:
            data = e.read() # Facebook sends OAuth errors as 400, and urllib2 throws an exception, we want a GraphAPIError
        try:
            response = _parse_json(data)
            if response and response.get("error"):
                raise GraphAPIError(response["error"].get("code", 1),
                                    response["error"]["message"])
        except ValueError:
            response = data

        return response

    # based on: http://code.activestate.com/recipes/146306/
    def _encode_multipart_form(self, fields):
        """Fields are a dict of form name-> value
        For files, value should be a file object.
        Other file-like objects might work and a fake name will be chosen.
        Return (content_type, body) ready for httplib.HTTP instance
        """
        BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
        CRLF = '\r\n'
        L = []
        for (key, value) in fields.items():
            logging.debug("Encoding %s, (%s)%s" % (key, type(value), value))
            if not value:
                continue
            L.append('--' + BOUNDARY)
            if hasattr(value, 'read') and callable(value.read):
                filename = getattr(value,'name','%s.jpg' % key)
                L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
                L.append('Content-Type: image/jpeg')
                value = value.read()
                logging.debug(type(value))
            else:
                L.append('Content-Disposition: form-data; name="%s"' % key)
            L.append('')
            if isinstance(value, unicode):
                logging.debug("Convert to ascii")
                value = value.encode('ascii')
            L.append(value)
        L.append('--' + BOUNDARY + '--')
        L.append('')
        body = CRLF.join(L)
        content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
        return content_type, body

    def request(self, path, args=None, post_args=None):
        """Fetches the given path in the Graph API.

        We translate args to a valid query string. If post_args is given,
        we send a POST request to the given path with the given arguments.
        """
        if not args: args = {}
        if self.access_token:
            if post_args is not None:
                post_args["access_token"] = self.access_token
            else:
                args["access_token"] = self.access_token
        post_data = None if post_args is None else urllib.urlencode(post_args)
        file = urllib2.urlopen("https://graph.facebook.com/" + path + "?" +
                              urllib.urlencode(args), post_data)

        try:
            fileInfo = file.info()
            if fileInfo.maintype == 'text':
                response = _parse_json(file.read())
            elif fileInfo.maintype == 'image':
                mimetype = fileInfo['content-type']
                response = {
                    "data": file.read(),
                    "mime-type": mimetype,
                    "url": file.url,
                }
            else:
                raise GraphAPIError('Response Error', 'Maintype was not text or image')
        finally:
            file.close()
        if response and isinstance(response, dict) and response.get("error"):
            raise GraphAPIError(response["error"]["type"],
                                response["error"]["message"])
        return response

    def api_request(self, path, args=None, post_args=None):
        """Fetches the given path in the Graph API.

        We translate args to a valid query string. If post_args is given,
        we send a POST request to the given path with the given arguments.
        """
        if not args: args = {}
        if self.access_token:
            if post_args is not None:
                post_args["access_token"] = self.access_token
            else:
                args["access_token"] = self.access_token
        if self.api_key:
            if post_args is not None:
                post_args["api_key"] = self.api_key
            else:
                args["api_key"] = self.api_key
        if post_args is not None:
            post_args["format"] = "json-strings"
        else:
            args["format"] = "json-strings"
        post_data = None if post_args is None else urllib.urlencode(post_args)
        file = urllib.urlopen("https://api.facebook.com/method/" + path + "?" +
                              urllib.urlencode(args), post_data)
        try:
            response = _parse_json(file.read())
        finally:
            file.close()
        if response and response.get("error"):
            raise GraphAPIError(response["error"]["type"],
                                response["error"]["message"])
        return response

    def fql(self, query, args=None, post_args=None):
        """FQL query.
        Two reasons to have this method:
        1. Graph api does not expose some info fields of a user, e.g.
            a user's networks/affiliations, we have to fall back to old api.
        2. FQL is a strong tool.
        Example query: "SELECT affiliations FROM user WHERE uid = me()"
        """
        if not args: args = {}
        if self.access_token:
            if post_args is not None:
                post_args["access_token"] = self.access_token
            else:
                args["access_token"] = self.access_token
        post_data = None if post_args is None else urllib.urlencode(post_args)

        args["query"] = query
        args["format"]="json"
        file = urllib2.urlopen("https://api.facebook.com/method/fql.query?" +
                              urllib.urlencode(args), post_data)
        try:
            content  = file.read()
            response = _parse_json(content)
            #Return a list if success, return a dictionary if failed
            if type(response) is dict and "error_code" in response:
                raise GraphAPIError(response["error_code"],response["error_msg"])
        except Exception, e:
            raise e
        finally:
            file.close()

        return response

class GraphAPIError(Exception):
    def __init__(self, type, message):
        Exception.__init__(self, message)
        self.type = type

def get_user_from_cookie(cookies, app_id, app_secret):
    """Parses the cookie set by the official Facebook JavaScript SDK.

    cookies should be a dictionary-like object mapping cookie names to
    cookie values.

    If the user is logged in via Facebook, we return a dictionary with the
    keys "uid" and "access_token". The former is the user's Facebook ID,
    and the latter can be used to make authenticated requests to the Graph API.
    If the user is not logged in, we return None.

    Download the official Facebook JavaScript SDK at
    http://github.com/facebook/connect-js/. Read more about Facebook
    authentication at http://developers.facebook.com/docs/authentication/.
    """
    cookie = cookies.get("fbs_" + app_id, "")
    if not cookie: return None
    args = dict((k, v[-1]) for k, v in cgi.parse_qs(cookie.strip('"')).items())
    payload = "".join(k + "=" + args[k] for k in sorted(args.keys())
                      if k != "sig")
    sig = hashlib.md5(payload + app_secret).hexdigest()
    expires = int(args["expires"])
    if sig == args.get("sig") and (expires == 0 or time.time() < expires):
        return args
    else:
        return None

def parse_signed_request(signed_request, app_secret):
    """ Return dictionary with signed request data.

    We return a dictionary containing the information in the signed_request. This will
    include a user_id if the user has authorised your application, as well as any
    information requested in the scope.

    If the signed_request is malformed or corrupted, False is returned.
    """
    try:
        l = signed_request.split('.', 2)
        encoded_sig = str(l[0])
        payload = str(l[1])
        sig = base64.urlsafe_b64decode(encoded_sig + "=" * ((4 - len(encoded_sig) % 4) % 4))
        data = base64.urlsafe_b64decode(payload + "=" * ((4 - len(payload) % 4) % 4))
    except IndexError:
        return False # raise ValueError('signed_request malformed')
    except TypeError:
        return False # raise ValueError('signed_request had corrupted payload')

    data = _parse_json(data)
    if data.get('algorithm', '').upper() != 'HMAC-SHA256':
        return False # raise ValueError('signed_request used unknown algorithm')

    expected_sig = hmac.new(app_secret, msg=payload, digestmod=hashlib.sha256).digest()
    if sig != expected_sig:
        return False # raise ValueError('signed_request had signature mismatch')

    return data

def auth_url(app_id, canvas_url, perms = None):
    url = "https://www.facebook.com/dialog/oauth?"
    kvps = {'client_id': app_id, 'redirect_uri': canvas_url}
    if perms:
        kvps['scope'] = ",".join(perms)
    return url + urllib.urlencode(kvps)

def get_app_access_token(application_id, application_secret):
    """
    Get the access_token for the app that can be used for insights and creating test users
    application_id = retrieved from the developer page
    application_secret = retrieved from the developer page
    returns the application access_token
    """
    # Get an app access token
    args = {'grant_type':'client_credentials',
            'client_id':application_id,
            'client_secret':application_secret}

    file = urllib2.urlopen("https://graph.facebook.com/oauth/access_token?" +
                              urllib.urlencode(args))

    try:
        result = file.read().split("=")[1]
    finally:
        file.close()

    return result
<commit_msg>OAuth 2.0 support.<commit_after>#!/usr/bin/env python
#
# Copyright 2010 Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""Python client library for the Facebook Platform.

This client library is designed to support the Graph API and the official
Facebook JavaScript SDK, which is the canonical way to implement
Facebook authentication. Read more about the Graph API at
http://developers.facebook.com/docs/api. You can download the Facebook
JavaScript SDK at http://github.com/facebook/connect-js/.

If your application is using Google AppEngine's webapp framework, your
usage of this module might look like this:

    user = facebook.get_user_from_cookie(self.request.cookies, key, secret)
    if user:
        graph = facebook.GraphAPI(user["access_token"])
        profile = graph.get_object("me")
        friends = graph.get_connections("me", "friends")

"""

import cgi
import time
import urllib
import urllib2
import hashlib
import hmac
import base64
import logging

# Find a JSON parser
try:
    import simplejson as json
except ImportError:
    try:
        from django.utils import simplejson as json
    except ImportError:
        import json
_parse_json = json.loads

# Find a query string parser
try:
    from urlparse import parse_qs
except ImportError:
    from cgi import parse_qs

class GraphAPI(object):
    """A client for the Facebook Graph API.

    See http://developers.facebook.com/docs/api for complete documentation
    for the API.

    The Graph API is made up of the objects in Facebook (e.g., people, pages,
    events, photos) and the connections between them (e.g., friends,
    photo tags, and event RSVPs). This client provides access to those
    primitive types in a generic way. For example, given an OAuth access
    token, this will fetch the profile of the active user and the list
    of the user's friends:

       graph = facebook.GraphAPI(access_token)
       user = graph.get_object("me")
       friends = graph.get_connections(user["id"], "friends")

    You can see a list of all of the objects and connections supported
    by the API at http://developers.facebook.com/docs/reference/api/.

    You can obtain an access token via OAuth or by using the Facebook
    JavaScript SDK. See http://developers.facebook.com/docs/authentication/
    for details.

    If you are using the JavaScript SDK, you can use the
    get_user_from_cookie() method below to get the OAuth access token
    for the active user from the cookie saved by the SDK.
    """
    def __init__(self, access_token=None):
        self.access_token = access_token

    def get_object(self, id, **args):
        """Fetchs the given object from the graph."""
        return self.request(id, args)

    def get_objects(self, ids, **args):
        """Fetchs all of the given object from the graph.

        We return a map from ID to object. If any of the IDs are invalid,
        we raise an exception.
        """
        args["ids"] = ",".join(ids)
        return self.request("", args)

    def get_connections(self, id, connection_name, **args):
        """Fetchs the connections for given object."""
        return self.request(id + "/" + connection_name, args)

    def put_object(self, parent_object, connection_name, **data):
        """Writes the given object to the graph, connected to the given parent.

        For example,

            graph.put_object("me", "feed", message="Hello, world")

        writes "Hello, world" to the active user's wall. Likewise, this
        will comment on a the first post of the active user's feed:

            feed = graph.get_connections("me", "feed")
            post = feed["data"][0]
            graph.put_object(post["id"], "comments", message="First!")

        See http://developers.facebook.com/docs/api#publishing for all of
        the supported writeable objects.

        Most write operations require extended permissions. For example,
        publishing wall posts requires the "publish_stream" permission. See
        http://developers.facebook.com/docs/authentication/ for details about
        extended permissions.
        """
        assert self.access_token, "Write operations require an access token"
        return self.request(parent_object + "/" + connection_name, post_args=data)

    def put_wall_post(self, message, attachment={}, profile_id="me"):
        """Writes a wall post to the given profile's wall.

        We default to writing to the authenticated user's wall if no
        profile_id is specified.

        attachment adds a structured attachment to the status message being
        posted to the Wall. It should be a dictionary of the form:

            {"name": "Link name"
             "link": "http://www.example.com/",
             "caption": "{*actor*} posted a new review",
             "description": "This is a longer description of the attachment",
             "picture": "http://www.example.com/thumbnail.jpg"}

        """
        return self.put_object(profile_id, "feed", message=message, **attachment)

    def put_comment(self, object_id, message):
        """Writes the given comment on the given post."""
        return self.put_object(object_id, "comments", message=message)

    def put_like(self, object_id):
        """Likes the given post."""
        return self.put_object(object_id, "likes")

    def delete_object(self, id):
        """Deletes the object with the given ID from the graph."""
        self.request(id, post_args={"method": "delete"})

    def put_photo(self, image, message=None, album_id=None, **kwargs):
        """Uploads an image using multipart/form-data
        image=File like object for the image
        message=Caption for your image
        album_id=None posts to /me/photos which uses or creates and uses
        an album for your application.
        """
        object_id = album_id or "me"
        #it would have been nice to reuse self.request; but multipart is messy in urllib
        post_args = {
				  'access_token': self.access_token,
				  'source': image,
				  'message': message
        }
        post_args.update(kwargs)
        content_type, body = self._encode_multipart_form(post_args)
        req = urllib2.Request("https://graph.facebook.com/%s/photos" % object_id, data=body)
        req.add_header('Content-Type', content_type)
        try:
            data = urllib2.urlopen(req).read()
        #For Python 3 use this:
        #except urllib2.HTTPError as e:
        except urllib2.HTTPError, e:
            data = e.read() # Facebook sends OAuth errors as 400, and urllib2 throws an exception, we want a GraphAPIError
        try:
            response = _parse_json(data)
            if response and response.get("error"):
                raise GraphAPIError(response["error"].get("code", 1),
                                    response["error"]["message"])
        except ValueError:
            response = data

        return response

    # based on: http://code.activestate.com/recipes/146306/
    def _encode_multipart_form(self, fields):
        """Fields are a dict of form name-> value
        For files, value should be a file object.
        Other file-like objects might work and a fake name will be chosen.
        Return (content_type, body) ready for httplib.HTTP instance
        """
        BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
        CRLF = '\r\n'
        L = []
        for (key, value) in fields.items():
            logging.debug("Encoding %s, (%s)%s" % (key, type(value), value))
            if not value:
                continue
            L.append('--' + BOUNDARY)
            if hasattr(value, 'read') and callable(value.read):
                filename = getattr(value,'name','%s.jpg' % key)
                L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
                L.append('Content-Type: image/jpeg')
                value = value.read()
                logging.debug(type(value))
            else:
                L.append('Content-Disposition: form-data; name="%s"' % key)
            L.append('')
            if isinstance(value, unicode):
                logging.debug("Convert to ascii")
                value = value.encode('ascii')
            L.append(value)
        L.append('--' + BOUNDARY + '--')
        L.append('')
        body = CRLF.join(L)
        content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
        return content_type, body

    def request(self, path, args=None, post_args=None):
        """Fetches the given path in the Graph API.

        We translate args to a valid query string. If post_args is given,
        we send a POST request to the given path with the given arguments.
        """
        if not args: args = {}
        if self.access_token:
            if post_args is not None:
                post_args["access_token"] = self.access_token
            else:
                args["access_token"] = self.access_token
        post_data = None if post_args is None else urllib.urlencode(post_args)
        file = urllib2.urlopen("https://graph.facebook.com/" + path + "?" +
                              urllib.urlencode(args), post_data)

        try:
            fileInfo = file.info()
            if fileInfo.maintype == 'text':
                response = _parse_json(file.read())
            elif fileInfo.maintype == 'image':
                mimetype = fileInfo['content-type']
                response = {
                    "data": file.read(),
                    "mime-type": mimetype,
                    "url": file.url,
                }
            else:
                raise GraphAPIError('Response Error', 'Maintype was not text or image')
        finally:
            file.close()
        if response and isinstance(response, dict) and response.get("error"):
            raise GraphAPIError(response["error"]["type"],
                                response["error"]["message"])
        return response

    def api_request(self, path, args=None, post_args=None):
        """Fetches the given path in the Graph API.

        We translate args to a valid query string. If post_args is given,
        we send a POST request to the given path with the given arguments.
        """
        if not args: args = {}
        if self.access_token:
            if post_args is not None:
                post_args["access_token"] = self.access_token
            else:
                args["access_token"] = self.access_token
        if self.api_key:
            if post_args is not None:
                post_args["api_key"] = self.api_key
            else:
                args["api_key"] = self.api_key
        if post_args is not None:
            post_args["format"] = "json-strings"
        else:
            args["format"] = "json-strings"
        post_data = None if post_args is None else urllib.urlencode(post_args)
        file = urllib.urlopen("https://api.facebook.com/method/" + path + "?" +
                              urllib.urlencode(args), post_data)
        try:
            response = _parse_json(file.read())
        finally:
            file.close()
        if response and response.get("error"):
            raise GraphAPIError(response["error"]["type"],
                                response["error"]["message"])
        return response

    def fql(self, query, args=None, post_args=None):
        """FQL query.
        Two reasons to have this method:
        1. Graph api does not expose some info fields of a user, e.g.
            a user's networks/affiliations, we have to fall back to old api.
        2. FQL is a strong tool.
        Example query: "SELECT affiliations FROM user WHERE uid = me()"
        """
        if not args: args = {}
        if self.access_token:
            if post_args is not None:
                post_args["access_token"] = self.access_token
            else:
                args["access_token"] = self.access_token
        post_data = None if post_args is None else urllib.urlencode(post_args)

        args["query"] = query
        args["format"]="json"
        file = urllib2.urlopen("https://api.facebook.com/method/fql.query?" +
                              urllib.urlencode(args), post_data)
        try:
            content  = file.read()
            response = _parse_json(content)
            #Return a list if success, return a dictionary if failed
            if type(response) is dict and "error_code" in response:
                raise GraphAPIError(response["error_code"],response["error_msg"])
        except Exception, e:
            raise e
        finally:
            file.close()

        return response

class GraphAPIError(Exception):
    def __init__(self, type, message):
        Exception.__init__(self, message)
        self.type = type

def get_user_from_cookie(cookies, app_id, app_secret):
    """Parses the cookie set by the official Facebook JavaScript SDK.

    cookies should be a dictionary-like object mapping cookie names to
    cookie values.

    If the user is logged in via Facebook, we return a dictionary with the
    keys "uid" and "access_token". The former is the user's Facebook ID,
    and the latter can be used to make authenticated requests to the Graph API.
    If the user is not logged in, we return None.

    Download the official Facebook JavaScript SDK at
    http://github.com/facebook/connect-js/. Read more about Facebook
    authentication at http://developers.facebook.com/docs/authentication/.
    """
    cookie = cookies.get("fbsr_" + app_id, "")
    if not cookie: return None
    parsed_request = parse_signed_request(cookie, app_secret)
    args = {
        "client_id": app_id,
        "client_secret": app_secret,
        "code": parsed_request["code"],
        "redirect_uri":""
    }
    # We would use GraphAPI.request() here, except for that the fact that the
    # response is a key-value pair, and not JSON.
    response = urllib.urlopen("https://graph.facebook.com/oauth/access_token" +
                              "?" + urllib.urlencode(args))
    query_str = parse_qs(response.read())
    if "access_token" in query_str:
        result = {
            "uid":parsed_request["user_id"],
            "issued_at":parsed_request["issued_at"],
            "access_token":query_str["access_token"][0],
        }
        if "expires" in query_str:
            result["expires"] = query_str["expires"][0]
        return result
    else:
        return None

def parse_signed_request(signed_request, app_secret):
    """ Return dictionary with signed request data.

    We return a dictionary containing the information in the signed_request. This will
    include a user_id if the user has authorised your application, as well as any
    information requested in the scope.

    If the signed_request is malformed or corrupted, False is returned.
    """
    try:
        l = signed_request.split('.', 2)
        encoded_sig = str(l[0])
        payload = str(l[1])
        sig = base64.urlsafe_b64decode(encoded_sig + "=" * ((4 - len(encoded_sig) % 4) % 4))
        data = base64.urlsafe_b64decode(payload + "=" * ((4 - len(payload) % 4) % 4))
    except IndexError:
        return False # raise ValueError('signed_request malformed')
    except TypeError:
        return False # raise ValueError('signed_request had corrupted payload')

    data = _parse_json(data)
    if data.get('algorithm', '').upper() != 'HMAC-SHA256':
        return False # raise ValueError('signed_request used unknown algorithm')

    expected_sig = hmac.new(app_secret, msg=payload, digestmod=hashlib.sha256).digest()
    if sig != expected_sig:
        return False # raise ValueError('signed_request had signature mismatch')

    return data

def auth_url(app_id, canvas_url, perms = None):
    url = "https://www.facebook.com/dialog/oauth?"
    kvps = {'client_id': app_id, 'redirect_uri': canvas_url}
    if perms:
        kvps['scope'] = ",".join(perms)
    return url + urllib.urlencode(kvps)

def get_app_access_token(application_id, application_secret):
    """
    Get the access_token for the app that can be used for insights and creating test users
    application_id = retrieved from the developer page
    application_secret = retrieved from the developer page
    returns the application access_token
    """
    # Get an app access token
    args = {'grant_type':'client_credentials',
            'client_id':application_id,
            'client_secret':application_secret}

    file = urllib2.urlopen("https://graph.facebook.com/oauth/access_token?" +
                              urllib.urlencode(args))

    try:
        result = file.read().split("=")[1]
    finally:
        file.close()

    return result
<|endoftext|>


<commit_before>from __future__ import unicode_literals, division, absolute_import
from builtins import *  # pylint: disable=unused-import, redefined-builtin

import copy
import logging
from math import ceil

from flask import jsonify
from flask import request
from sqlalchemy.orm.exc import NoResultFound

from flexget.api import api, APIResource
from flexget.plugins.list import movie_list as ml
from flexget.plugins.list.movie_list import MovieListBase
from flexget.utils.tools import split_title_year

log = logging.getLogger('movie_list')

movie_list_api = api.namespace('movie_list', description='Movie List operations')

default_error_schema = {
    'type': 'object',
    'properties': {
        'status': {'type': 'string'},
        'message': {'type': 'string'}
    }
}
empty_response = api.schema('empty', {'type': 'object'})

default_error_schema = api.schema('default_error_schema', default_error_schema)
empty_response = api.schema('empty_response', empty_response)

input_movie_list_id_object = {
    'type': 'array',
    'items': {
        'type': 'object',
        'minProperties': 1,
        'additionalProperties': True
    }
}

input_movie_entry = {
    'type': 'object',
    'properties': {
        'title': {'type': 'string'},
        'original_url': {'type': 'string'},
        'movie_name': {'type': 'string'},
        'movie_year': {'type': 'integer'},
        'movie_identifiers': input_movie_list_id_object
    },
    'additionalProperties': True,
    'required': ['original_url'],
    'anyOf': [
        {'required': ['title']},
        {'required': ['movie_name', 'movie_year']}
    ]
}

return_movie_list_id_object = copy.deepcopy(input_movie_list_id_object)
return_movie_list_id_object.update(
    {'properties': {
        'id': {'type': 'integer'},
        'added_on': {'type': 'string'},
        'movie_id': {'type': 'integer'}
    }})

movie_list_object = {
    'type': 'object',
    'properties': {
        'title': {'type': 'string'},
        'added_on': {'type': 'string'},
        'year': {'type': 'integer'},
        'list_id': {'type': 'integer'},
        'movie_list_ids': {
            'type': 'array',
            'items': return_movie_list_id_object
        },
    }
}

list_object = {
    'type': 'object',
    'properties': {
        'id': {'type': 'integer'},
        'added_on': {'type': 'string'},
        'name': {'type': 'string'}
    }
}

list_input = copy.deepcopy(list_object)
del list_input['properties']['id']
del list_input['properties']['added_on']

return_movies = {
    'type': 'object',
    'properties': {
        'movies': {
            'type': 'array',
            'items': movie_list_object
        },
        'number_of_movies': {'type': 'integer'},
        'total_number_of_movies': {'type': 'integer'},
        'page_number': {'type': 'integer'}
    }
}
return_lists = {'type': 'array', 'items': list_object}

input_movie_entry_schema = api.schema('input_movie_entry', input_movie_entry)
input_movie_list_id_schema = api.schema('input_movie_list_id_object', input_movie_list_id_object)

movie_list_id_object_schema = api.schema('movie_list_id_object', return_movie_list_id_object)
movie_list_object_schema = api.schema('movie_list_object', movie_list_object)
list_object_schema = api.schema('list_object', list_object)
return_lists_schema = api.schema('return_lists', return_lists)
return_movies_schema = api.schema('return_movies', return_movies)

new_list_schema = api.schema('new_list', list_input)

movie_list_parser = api.parser()
movie_list_parser.add_argument('name', help='Filter results by list name')

@movie_list_api.route('/')
class MovieListAPI(APIResource):

    @api.response(200, model=return_lists_schema)
    @api.doc(parser=movie_list_parser)
    def get(self, session=None):
        """ Gets movies lists """
        args = movie_list_parser.parse_args()
        name = args.get('name')
        movie_lists = [movie_list.to_dict() for movie_list in ml.get_movie_lists(name=name, session=session)]
        return jsonify({'movie_lists': movie_lists})

    @api.validate(new_list_schema)
    @api.response(201, model=list_object_schema)
    @api.response(500, description='List already exist', model=default_error_schema)
    def post(self, session=None):
        """ Create a new list """
        data = request.json
        name = data.get('name')
        try:
            movie_list = ml.get_list_by_exact_name(name=name, session=session)
        except NoResultFound:
            movie_list = None
        if movie_list:
            return {'status': 'error',
                    'message': "list with name '%s' already exists" % name}, 500
        movie_list = ml.MovieListList(name=name)
        session.add(movie_list)
        session.commit()
        resp = jsonify(movie_list.to_dict())
        resp.status_code = 201
        return resp

@movie_list_api.route('/<int:list_id>/')
@api.doc(params={'list_id': 'ID of the list'})
class MovieListListAPI(APIResource):

    @api.response(404, model=default_error_schema)
    @api.response(200, model=list_object_schema)
    def get(self, list_id, session=None):
        """ Get list by ID """
        try:
            movie_list = ml.get_list_by_id(list_id=list_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'list_id %d does not exist' % list_id}, 404
        return jsonify(movie_list.to_dict())

    @api.response(200, model=empty_response)
    @api.response(404, model=default_error_schema)
    def delete(self, list_id, session=None):
        """ Delete list by ID """
        try:
            movie_list = ml.get_list_by_id(list_id=list_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'list_id %d does not exist' % list_id}, 404
        session.delete(movie_list)
        return {}

movie_identifiers_doc = "Use movie identifier using the following format:\n[{'ID_NAME: 'ID_VALUE'}]." \
                        " Has to be one of %s" % " ,".join(MovieListBase().supported_ids)

movies_parser = api.parser()
movies_parser.add_argument('sort_by', choices=('id', 'added', 'title', 'year'), default='title',
                           help='Sort by attribute')
movies_parser.add_argument('order', choices=('desc', 'asc'), default='desc', help='Sorting order')
movies_parser.add_argument('page', type=int, default=1, help='Page number')
movies_parser.add_argument('page_size', type=int, default=10, help='Number of movies per page')

@movie_list_api.route('/<int:list_id>/movies/')
class MovieListMoviesAPI(APIResource):

    @api.response(404, model=default_error_schema)
    @api.response(200, model=return_movies_schema)
    @api.doc(params={'list_id': 'ID of the list'}, parser=movies_parser)
    def get(self, list_id, session=None):
        """ Get movies by list ID """

        args = movies_parser.parse_args()
        page = args.get('page')
        page_size = args.get('page_size')

        start = page_size * (page - 1)
        stop = start + page_size
        if args.get('order') == 'desc':
            descending = True
        else:
            descending = False

        kwargs = {
            'start': start,
            'stop': stop,
            'list_id': list_id,
            'order_by': args.get('sort_by'),
            'descending': descending,
            'session': session
        }
        try:
            ml.get_list_by_id(list_id=list_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'list_id %d does not exist' % list_id}, 404
        count = ml.get_movies_by_list_id(count=True, **kwargs)
        movies = [movie.to_dict() for movie in ml.get_movies_by_list_id(**kwargs)]
        pages = int(ceil(count / float(page_size)))

        number_of_movies = min(page_size, count)

        return jsonify({'movies': movies,
                        'number_of_movies': number_of_movies,
                        'total_number_of_movies': count,
                        'page': page,
                        'total_number_of_pages': pages})

    @api.validate(model=input_movie_entry_schema, description=movie_identifiers_doc)
    @api.response(201, model=movie_list_object_schema)
    @api.response(404, description='List not found', model=default_error_schema)
    @api.response(500, description='Movie already exist in list', model=default_error_schema)
    @api.response(501, description='Movie identifier not allowed', model=default_error_schema)
    def post(self, list_id, session=None):
        """ Add movies to list by ID """
        try:
            movie_list = ml.get_list_by_id(list_id=list_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'list_id %d does not exist' % list_id}, 404
        data = request.json

        # Validates ID type based on allowed ID
        # TODO pass this to json schema validation
        for id_name in data.get('movie_identifiers'):
            if set(id_name.keys()) & set(MovieListBase().supported_ids) == set([]):
                return {'status': 'error',
                        'message': 'movie identifier %s is not allowed' % id_name}, 501
        if 'movie_name' in data:
            title, year = data['movie_name'], data.get('movie_year')
        else:
            title, year = split_title_year(data['title'])
        movie = ml.get_movie_by_title(list_id=list_id, title=title, session=session)
        if movie:
            return {'status': 'error',
                    'message': 'movie with name "%s" already exist in list %d' % (title, list_id)}, 500
        movie = ml.MovieListMovie()
        movie.title = title
        movie.year = year
        movie.ids = ml.get_db_movie_identifiers(identifier_list=data.get('movie_identifiers'), session=session)
        movie.list_id = list_id
        session.add(movie)
        session.commit()
        response = jsonify({'movie': movie.to_dict()})
        response.status_code = 201
        return response

@movie_list_api.route('/<int:list_id>/movies/<int:movie_id>/')
@api.doc(params={'list_id': 'ID of the list', 'movie_id': 'ID of the movie'})
@api.response(404, description='List or movie not found', model=default_error_schema)
class MovieListMovieAPI(APIResource):

    @api.response(200, model=movie_list_object_schema)
    def get(self, list_id, movie_id, session=None):
        """ Get a movie by list ID and movie ID """
        try:
            movie = ml.get_movie_by_id(list_id=list_id, movie_id=movie_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'could not find movie with id %d in list %d' % (movie_id, list_id)}, 404
        return jsonify(movie.to_dict())

    @api.response(200, model=empty_response)
    def delete(self, list_id, movie_id, session=None):
        """ Delete a movie by list ID and movie ID """
        try:
            movie = ml.get_movie_by_id(list_id=list_id, movie_id=movie_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'could not find movie with id %d in list %d' % (movie_id, list_id)}, 404
        log.debug('deleting movie %d', movie.id)
        session.delete(movie)
        return {}

    @api.validate(model=input_movie_list_id_schema, description=movie_identifiers_doc)
    @api.response(200, model=movie_list_object_schema)
    @api.response(501, description='Movie identifier not allowed', model=default_error_schema)
    @api.doc(description='Sent movie identifiers will override any existing identifiers that the movie currently holds')
    def put(self, list_id, movie_id, session=None):
        """ Sets movie identifiers """
        try:
            movie = ml.get_movie_by_id(list_id=list_id, movie_id=movie_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'could not find movie with id %d in list %d' % (movie_id, list_id)}, 404
        data = request.json

        # Validates ID type based on allowed ID
        # TODO pass this to json schema validation
        for id_name in data:
            if set(id_name.keys()) & set(MovieListBase().supported_ids) == set([]):
                return {'status': 'error',
                        'message': 'movie identifier %s is not allowed' % id_name}, 501
        movie.ids[:] = ml.get_db_movie_identifiers(identifier_list=data, movie_id=movie_id, session=session)
        session.commit()
        return jsonify(movie.to_dict())
<commit_msg>Assigning bool of test<commit_after>from __future__ import unicode_literals, division, absolute_import
from builtins import *  # pylint: disable=unused-import, redefined-builtin

import copy
import logging
from math import ceil

from flask import jsonify
from flask import request
from sqlalchemy.orm.exc import NoResultFound

from flexget.api import api, APIResource
from flexget.plugins.list import movie_list as ml
from flexget.plugins.list.movie_list import MovieListBase
from flexget.utils.tools import split_title_year

log = logging.getLogger('movie_list')

movie_list_api = api.namespace('movie_list', description='Movie List operations')

default_error_schema = {
    'type': 'object',
    'properties': {
        'status': {'type': 'string'},
        'message': {'type': 'string'}
    }
}
empty_response = api.schema('empty', {'type': 'object'})

default_error_schema = api.schema('default_error_schema', default_error_schema)
empty_response = api.schema('empty_response', empty_response)

input_movie_list_id_object = {
    'type': 'array',
    'items': {
        'type': 'object',
        'minProperties': 1,
        'additionalProperties': True
    }
}

input_movie_entry = {
    'type': 'object',
    'properties': {
        'title': {'type': 'string'},
        'original_url': {'type': 'string'},
        'movie_name': {'type': 'string'},
        'movie_year': {'type': 'integer'},
        'movie_identifiers': input_movie_list_id_object
    },
    'additionalProperties': True,
    'required': ['original_url'],
    'anyOf': [
        {'required': ['title']},
        {'required': ['movie_name', 'movie_year']}
    ]
}

return_movie_list_id_object = copy.deepcopy(input_movie_list_id_object)
return_movie_list_id_object.update(
    {'properties': {
        'id': {'type': 'integer'},
        'added_on': {'type': 'string'},
        'movie_id': {'type': 'integer'}
    }})

movie_list_object = {
    'type': 'object',
    'properties': {
        'title': {'type': 'string'},
        'added_on': {'type': 'string'},
        'year': {'type': 'integer'},
        'list_id': {'type': 'integer'},
        'movie_list_ids': {
            'type': 'array',
            'items': return_movie_list_id_object
        },
    }
}

list_object = {
    'type': 'object',
    'properties': {
        'id': {'type': 'integer'},
        'added_on': {'type': 'string'},
        'name': {'type': 'string'}
    }
}

list_input = copy.deepcopy(list_object)
del list_input['properties']['id']
del list_input['properties']['added_on']

return_movies = {
    'type': 'object',
    'properties': {
        'movies': {
            'type': 'array',
            'items': movie_list_object
        },
        'number_of_movies': {'type': 'integer'},
        'total_number_of_movies': {'type': 'integer'},
        'page_number': {'type': 'integer'}
    }
}
return_lists = {'type': 'array', 'items': list_object}

input_movie_entry_schema = api.schema('input_movie_entry', input_movie_entry)
input_movie_list_id_schema = api.schema('input_movie_list_id_object', input_movie_list_id_object)

movie_list_id_object_schema = api.schema('movie_list_id_object', return_movie_list_id_object)
movie_list_object_schema = api.schema('movie_list_object', movie_list_object)
list_object_schema = api.schema('list_object', list_object)
return_lists_schema = api.schema('return_lists', return_lists)
return_movies_schema = api.schema('return_movies', return_movies)

new_list_schema = api.schema('new_list', list_input)

movie_list_parser = api.parser()
movie_list_parser.add_argument('name', help='Filter results by list name')

@movie_list_api.route('/')
class MovieListAPI(APIResource):
    @api.response(200, model=return_lists_schema)
    @api.doc(parser=movie_list_parser)
    def get(self, session=None):
        """ Gets movies lists """
        args = movie_list_parser.parse_args()
        name = args.get('name')
        movie_lists = [movie_list.to_dict() for movie_list in ml.get_movie_lists(name=name, session=session)]
        return jsonify({'movie_lists': movie_lists})

    @api.validate(new_list_schema)
    @api.response(201, model=list_object_schema)
    @api.response(500, description='List already exist', model=default_error_schema)
    def post(self, session=None):
        """ Create a new list """
        data = request.json
        name = data.get('name')
        try:
            movie_list = ml.get_list_by_exact_name(name=name, session=session)
        except NoResultFound:
            movie_list = None
        if movie_list:
            return {'status': 'error',
                    'message': "list with name '%s' already exists" % name}, 500
        movie_list = ml.MovieListList(name=name)
        session.add(movie_list)
        session.commit()
        resp = jsonify(movie_list.to_dict())
        resp.status_code = 201
        return resp

@movie_list_api.route('/<int:list_id>/')
@api.doc(params={'list_id': 'ID of the list'})
class MovieListListAPI(APIResource):
    @api.response(404, model=default_error_schema)
    @api.response(200, model=list_object_schema)
    def get(self, list_id, session=None):
        """ Get list by ID """
        try:
            movie_list = ml.get_list_by_id(list_id=list_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'list_id %d does not exist' % list_id}, 404
        return jsonify(movie_list.to_dict())

    @api.response(200, model=empty_response)
    @api.response(404, model=default_error_schema)
    def delete(self, list_id, session=None):
        """ Delete list by ID """
        try:
            movie_list = ml.get_list_by_id(list_id=list_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'list_id %d does not exist' % list_id}, 404
        session.delete(movie_list)
        return {}

movie_identifiers_doc = "Use movie identifier using the following format:\n[{'ID_NAME: 'ID_VALUE'}]." \
                        " Has to be one of %s" % " ,".join(MovieListBase().supported_ids)

movies_parser = api.parser()
movies_parser.add_argument('sort_by', choices=('id', 'added', 'title', 'year'), default='title',
                           help='Sort by attribute')
movies_parser.add_argument('order', choices=('desc', 'asc'), default='desc', help='Sorting order')
movies_parser.add_argument('page', type=int, default=1, help='Page number')
movies_parser.add_argument('page_size', type=int, default=10, help='Number of movies per page')

@movie_list_api.route('/<int:list_id>/movies/')
class MovieListMoviesAPI(APIResource):
    @api.response(404, model=default_error_schema)
    @api.response(200, model=return_movies_schema)
    @api.doc(params={'list_id': 'ID of the list'}, parser=movies_parser)
    def get(self, list_id, session=None):
        """ Get movies by list ID """

        args = movies_parser.parse_args()
        page = args.get('page')
        page_size = args.get('page_size')

        start = page_size * (page - 1)
        stop = start + page_size
        descending = bool(args.get('order') == 'desc')

        kwargs = {
            'start': start,
            'stop': stop,
            'list_id': list_id,
            'order_by': args.get('sort_by'),
            'descending': descending,
            'session': session
        }
        try:
            ml.get_list_by_id(list_id=list_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'list_id %d does not exist' % list_id}, 404
        count = ml.get_movies_by_list_id(count=True, **kwargs)
        movies = [movie.to_dict() for movie in ml.get_movies_by_list_id(**kwargs)]
        pages = int(ceil(count / float(page_size)))

        number_of_movies = min(page_size, count)

        return jsonify({'movies': movies,
                        'number_of_movies': number_of_movies,
                        'total_number_of_movies': count,
                        'page': page,
                        'total_number_of_pages': pages})

    @api.validate(model=input_movie_entry_schema, description=movie_identifiers_doc)
    @api.response(201, model=movie_list_object_schema)
    @api.response(404, description='List not found', model=default_error_schema)
    @api.response(500, description='Movie already exist in list', model=default_error_schema)
    @api.response(501, description='Movie identifier not allowed', model=default_error_schema)
    def post(self, list_id, session=None):
        """ Add movies to list by ID """
        try:
            movie_list = ml.get_list_by_id(list_id=list_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'list_id %d does not exist' % list_id}, 404
        data = request.json

        # Validates ID type based on allowed ID
        # TODO pass this to json schema validation
        for id_name in data.get('movie_identifiers'):
            if set(id_name.keys()) & set(MovieListBase().supported_ids) == set([]):
                return {'status': 'error',
                        'message': 'movie identifier %s is not allowed' % id_name}, 501
        if 'movie_name' in data:
            title, year = data['movie_name'], data.get('movie_year')
        else:
            title, year = split_title_year(data['title'])
        movie = ml.get_movie_by_title(list_id=list_id, title=title, session=session)
        if movie:
            return {'status': 'error',
                    'message': 'movie with name "%s" already exist in list %d' % (title, list_id)}, 500
        movie = ml.MovieListMovie()
        movie.title = title
        movie.year = year
        movie.ids = ml.get_db_movie_identifiers(identifier_list=data.get('movie_identifiers'), session=session)
        movie.list_id = list_id
        session.add(movie)
        session.commit()
        response = jsonify({'movie': movie.to_dict()})
        response.status_code = 201
        return response

@movie_list_api.route('/<int:list_id>/movies/<int:movie_id>/')
@api.doc(params={'list_id': 'ID of the list', 'movie_id': 'ID of the movie'})
@api.response(404, description='List or movie not found', model=default_error_schema)
class MovieListMovieAPI(APIResource):
    @api.response(200, model=movie_list_object_schema)
    def get(self, list_id, movie_id, session=None):
        """ Get a movie by list ID and movie ID """
        try:
            movie = ml.get_movie_by_id(list_id=list_id, movie_id=movie_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'could not find movie with id %d in list %d' % (movie_id, list_id)}, 404
        return jsonify(movie.to_dict())

    @api.response(200, model=empty_response)
    def delete(self, list_id, movie_id, session=None):
        """ Delete a movie by list ID and movie ID """
        try:
            movie = ml.get_movie_by_id(list_id=list_id, movie_id=movie_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'could not find movie with id %d in list %d' % (movie_id, list_id)}, 404
        log.debug('deleting movie %d', movie.id)
        session.delete(movie)
        return {}

    @api.validate(model=input_movie_list_id_schema, description=movie_identifiers_doc)
    @api.response(200, model=movie_list_object_schema)
    @api.response(501, description='Movie identifier not allowed', model=default_error_schema)
    @api.doc(description='Sent movie identifiers will override any existing identifiers that the movie currently holds')
    def put(self, list_id, movie_id, session=None):
        """ Sets movie identifiers """
        try:
            movie = ml.get_movie_by_id(list_id=list_id, movie_id=movie_id, session=session)
        except NoResultFound:
            return {'status': 'error',
                    'message': 'could not find movie with id %d in list %d' % (movie_id, list_id)}, 404
        data = request.json

        # Validates ID type based on allowed ID
        # TODO pass this to json schema validation
        for id_name in data:
            if set(id_name.keys()) & set(MovieListBase().supported_ids) == set([]):
                return {'status': 'error',
                        'message': 'movie identifier %s is not allowed' % id_name}, 501
        movie.ids[:] = ml.get_db_movie_identifiers(identifier_list=data, movie_id=movie_id, session=session)
        session.commit()
        return jsonify(movie.to_dict())
<|endoftext|>


<commit_before>from __future__ import unicode_literals, division, absolute_import
import logging
import os
import rarfile

from flexget import plugin
from flexget.entry import Entry
from flexget.event import event

log = logging.getLogger('rarfile')

def open_rar(rarpath):

    rar = None

    log.debug('Attempting to open RAR: %s' % rarpath)
    try:
        rar = rarfile.RarFile(rarfile=rarpath)
    except Exception as e:
        log.warn("Failed to open RAR: %s" % e)

    return rar

def to_url(path):
    """Convert a local file system to URL format"""

    url = path

    if not url.startswith('/'):
        url = item + '/'

    url = 'file:/' + url

    return url

class RarList(object):
    """
    Uses a local RAR file as an input and creates entries for files that match mask.

    You can specify either the mask key, in shell file matching format, (see python fnmatch module,)
    or regexp key.

    Example:

      rar_list:
        path: /downloads/episode.rar
        mask: *.mkv

    Example:

      rar_list:
        path: /downloads/episode.rar
        regexp: .*\.(avi|mkv)$
    """

    schema = {
        'type': 'object',
        'properties': {
            'path': {'type': 'string', 'format': 'file'},
            'mask': {'type': 'string'},
            'regexp': {'type': 'string', 'format': 'regex'}
        },
        'required': ['path'],
        'additionalProperties': False
    }

    def prepare_config(self, config):
        """Prepare the  mask/regexp provided in config"""

        from fnmatch import translate

        config.setdefault('recursive', False)
        # If mask was specified, turn it in to a regexp
        if config.get('mask'):
            config['regexp'] = translate(config['mask'])
        # If no mask or regexp specified, accept all files
        if not config.get('regexp'):
            config['regexp'] = '.'

    def on_task_input(self, task, config):
        """Creates entries for the contents of a RAR archive that match the provided pattern."""

        import re

        self.prepare_config(config)
        entries = []
        match = re.compile(config['regexp'], re.IGNORECASE).match
        rarpath = config['path']

        rar = open_rar(rarpath)

        if not rar:
            return

        url_prefix = to_url(rarpath)

        for info in rar.infolist():
            path = info.filename

            if not match(path):
                log.debug('File did not match regexp: %s' % path)
                continue

            log.debug('Found matching file: %s' %path)

            title = os.path.basename(path)
            url = url_prefix + '::' + path
            size = info.file_size
            timestamp = '%02i-%02i-%02i %02i:%02i:%02i' % info.date_time

            entry = Entry (
                    title = title,
                    location = path,
                    rar_path = rarpath,
                    url = url,
                    size = size,
                    timestamp = timestamp
                )

            entries.append(entry)

        return entries

class RarVolumes(object):
    """
    Creates entries for the files that compose a RAR archive (e.g. r01, r02, etc). This is useful
    for deleting an archive once it's been extracted.

    Example:

      rar_volumes:
        path: /downloads/episode.rar
    """

    schema = {
        'type': 'object',
        'properties': {
            'path': {'type': 'string', 'format': 'file'}
        },
        'required': ['path'],
        'additionalProperties': False
    }

    def on_task_input(self, task, config):
        """Creates entries for the ."""

        entries = []
        rarpath = config['path']
        rar = open_rar(rarpath)

        if not rar:
            return

        for volume in rar.volumelist():

            url = to_url(volume)
            title = os.path.basename(volume)

            entry = Entry (
                    title = title,
                    location = volume,
                    url = url
                )

            entries.append(entry)

        return entries

class RarExtract(object):
    """
    Extracts files from RAR archives. By default this plugin will extract to the same directory as
    the source archive, preserving directory structure from the archive.

    This plugin requires the unrar command line utility to extract compressed archives. If its
    location is not specified in your PATH environment variable, you can specify its path using
    the unrar_tool config value.

    Example:

      rar_extract:
        to: '/Volumes/External/TV/Show/Season 1'

    Example:

      rar_extract:
        to: '/Volumes/External/TV/Show/Season 1'
        keep_dirs: no
        unrar_tool: '/unar/unrar'

    Example:

        rar_extract: yes
    """

    schema = {
        'anyOf': [
            {'type': 'boolean'},
            {
                'type': 'object',
                'properties': {
                    'to': {'type': 'string'},
                    'keep_dirs': {'type': 'boolean'},
                    'unrar_tool': {'type': 'string'}
                },
                'additionalProperties': False
            }
        ]
    }

    def prepare_config(self, config):
        if not isinstance(config, dict):
            config = {}

        config.setdefault('to', '')
        config.setdefault('keep_dirs', True)
        config.setdefault('unrar_tool', 'unrar')
        return config

    def handle_entry(self, entry, config):
        """Extract the file listed in entry"""

        rarpath = entry['rar_path']
        source_path = entry['location']
        source_file = os.path.basename(source_path)

        rar = open_rar(rarpath)

        # Build the destination path
        if config['keep_dirs']:
            path_suffix = source_path
        else:
            path_suffix = source_file

        to = config['to']
        if not to:
            to = os.path.dirname(rarpath)

        destination = os.path.join(to, path_suffix)
        dest_dir = os.path.dirname(destination)

        if not os.path.exists(dest_dir):
            os.makedirs(dest_dir)

        if not os.path.exists(destination):
            log.debug('Attempting to extract %s to %s' % (source_file, dest_dir))
            try:
                rar.extract(source_path, dest_dir)
                log.info('Extracted %s' % source_file )
            except Exception as e:
                log.error('Failed to extract file %s (%s)' % (source_file, e) )
        else:
            log.debug('File already exists')

    def on_task_output(self, task, config):
        """Extracts entries for the  contents of a RAR archive that match the provided pattern."""
        config = self.prepare_config(config)

        # Set the path of the unrar tool if it's not specified in PATH
        unrar_tool = config['unrar_tool']
        if unrar_tool != 'unrar':
            rarfile.UNRAR_TOOL = unrar_tool
            log.debug('Set RarFile.unrar_tool to: %s' % unrar_tool)

        for entry in task.accepted:
            if not 'rar_path' in entry:
                self.log.verbose('Cannot handle %s because it does not have the field rar_path.' % entry['title'])
                continue

            self.handle_entry(entry, config)

@event('plugin.register')
def register_plugin():
    plugin.register(RarList, 'rar_list', api_ver=2)
    plugin.register(RarVolumes, 'rar_volumes', api_ver=2)
    plugin.register(RarExtract, 'rar_extract', api_ver=2)
<commit_msg>Completely refactored so there's only one plugin; can now be used with find plugin.<commit_after>from __future__ import unicode_literals, division, absolute_import
import logging
import os
import rarfile
import re

from flexget import plugin
from flexget.entry import Entry
from flexget.event import event
from flexget.utils.template import render_from_entry, RenderError

log = logging.getLogger('rarfile')

class RarExtract(object):
    """
    Extracts files from RAR archives. By default this plugin will extract to the same directory as
    the source archive, preserving directory structure from the archive.

    This plugin requires the unrar command line utility to extract compressed archives. If its
    location is not specified in your PATH environment variable, you can specify its path using
    the unrar_tool config value.

    Example:

      rar_extract: yes

    Example:

      rar_extract:
        keep_dirs: no
        mask: *.mkv
        unrar_tool: '/unar/unrar'

    Example:

      rar_extract:
        keep_dirs: yes
        fail_entries: yes
        regexp: '.*s\d{1,2}e\d{1,2}.*.mkv'
    """

    schema = {
        'anyOf': [
            {'type': 'boolean'},
            {
                'type': 'object',
                'properties': {
                    'to': {'type': 'string'},
                    'keep_dirs': {'type': 'boolean'},
                    'mask': {'type': 'string'},
                    'regexp': {'type': 'string', 'format': 'regex'},
                    'fail_entries': {'type': 'boolean'},
                    'unrar_tool': {'type': 'string'},
                    'delete_rar': {'type': 'boolean'}
                },
                'additionalProperties': False
            }
        ]
    }

    def prepare_config(self, config):
        if not isinstance(config, dict):
            config = {}

        config.setdefault('to', '')
        config.setdefault('keep_dirs', True)
        config.setdefault('fail_entries', False)
        config.setdefault('unrar_tool', '')
        config.setdefault('delete_rar', False)

        # If mask was specified, turn it in to a regexp
        if 'mask' in config:
            config['regexp'] = translate(config['mask'])
        # If no mask or regexp specified, accept all files
        if 'regexp' not in config:
            config['regexp'] = '.'

        return config

    def handle_entry(self, entry, match, config):
        """Extract the file listed in entry"""

        rar_path = entry['location']
        rar_dir = os.path.dirname(rar_path)
        rar_file = os.path.basename(rar_path)

        try:
            rar = rarfile.RarFile(rarfile=rar_path)
        except rarfile.RarWarning as e:
            log.warn('Nonfatal error: %s (%s)' % (rar_path, e))
        except rarfile.NeedFirstVolume:
            log.error('Not the first volume: %s' % rar_path)
            return
        except rarfile.NotRarFile:
            log.error('Not a RAR file: %s' % rar_path)
            return
        except rarfile.RarFatalError as e:
            error = 'Failed to open RAR: %s (%s)' (rar_path, e)
            log.error(error)
            if config['fail_entries']:
                entry.fail(error)
            return

        to = config['to']
        if to:
            try:
                to = render_from_entry(to, entry)
            except RenderError as e:
                error = 'Could not render path: %s' % to
                log.error(error)

                if config['fail_entries']:
                    entry.fail(error)
                return
        else:
            to = rar_dir

        for info in rar.infolist():
            path = info.filename
            filename = os.path.basename(path)

            if not match(path):
                log.debug('File did not match regexp: %s' % path)
                continue

            log.debug('Found matching file: %s' %path)

            if config['keep_dirs']:
                path_suffix = path
            else:
                path_suffix = filename
            destination = os.path.join(to, path_suffix)
            dest_dir = os.path.dirname(destination)

            if not os.path.exists(dest_dir):
                log.debug('Creating path: %s' % dest_dir)
                os.makedirs(dest_dir)

            if not os.path.exists(destination):
                log.debug('Attempting to extract: %s to %s' % (rar_file, dest_dir))
                try:
                    rar.extract(path, dest_dir)
                    log.verbose('Extracted: %s' % path )
                except Exception as e:
                    error = 'Failed to extract file: %s (%s)' % (path, e)
                    log.error(error)
                    entry.fail(error)
                    return
            else:
                log.verbose('File already exists: %s' % dest_dir)

        if config['delete_rar']:
            volumes = rar.volumelist()
            rar.close()

            for volume in volumes:
                log.debug('Deleting volume: %s' % volume)
                os.remove(volume)

            log.verbose('Deleted RAR: %s' % rar_file)
        else:
            rar.close()

    def on_task_output(self, task, config):
        """Extracts entries for the  contents of a RAR archive that match the provided pattern."""
        if isinstance(config, bool) and not config:
            return

        config = self.prepare_config(config)

        match = re.compile(config['regexp'], re.IGNORECASE).match

        # Set the path of the unrar tool if it's not specified in PATH
        unrar_tool = config['unrar_tool']
        if unrar_tool:
            rarfile.UNRAR_TOOL = unrar_tool
            log.debug('Set RarFile.unrar_tool to: %s' % unrar_tool)

        for entry in task.accepted:
            self.handle_entry(entry, match, config)

@event('plugin.register')
def register_plugin():
    plugin.register(RarExtract, 'rar_extract', api_ver=2)
<|endoftext|>


<commit_before>#!/usr/bin/python
# vi:si:et:sw=4:sts=4:ts=4

import pygst
pygst.require('0.10')
import gst
import gobject

class DvbBin(gst.Bin):

    # pids we currently allow through the dvbsrc filter
    pids = []
    # current program number
    program_number = -1
    # dict keyed on the padname (corresponds to pid)
    # values are a tuple of (queue, decoder, identity) element objects
    pids_elements = {}
    # value of the following is (identity, event_probe_id)
    video_elements = None
    audio_elements = None
    # following are currently used pads for demuxer
    current_video_pad = None
    current_audio_pad = None
    # the following are tuples of: (padname, queue, decoder)
    pending_video = None
    pending_audio = None
    # This flag means that we will not receive new pads for this current
    # segment
    no_more_pads = False

    def __init__(self, tuning_params, program_number):
        gobject.GObject.__init__(self)
        self.src = gst.element_factory_make("dvbsrc")
        for param in tuning_params:
            print "Setting property %s to %r" % (param[0], param[1])
            self.src.set_property(param[0], param[1])
        self.src.set_property("pids", "16:17:18")
        self.demux = gst.element_factory_make("flutsdemux")
        self.add(self.src, self.demux)
        self.program_number = program_number
        self.src.link(self.demux)
        self.demux.connect("pad-added", self._pad_added_cb)
        self.demux.connect("pad-removed", self._pad_removed_cb)
        self.demux.connect("no-more-pads", self._no_more_pads_cb)
        self.demux.connect("notify::pat-info", self._pat_info_changed_cb)
        self.demux.connect("notify::pmt-info", self._pmt_info_changed_cb)

    def _pat_info_changed_cb(self, demux, param):
        self.debug("PAT info has changed")
        pi = demux.get_property("pat-info")
        for prog in pi:
            self.debug("PAT: Program %d on PID 0x%04x" % (
                prog.props.program_number, prog.props.pid))
            if prog.props.program_number == self.program_number:
                self.pids = [prog.props.pid]
                self.src.set_property("pids", "16:17:18:%d" % prog.props.pid)
                self.demux.set_property("program-number", self.program_number)

    def _pmt_info_changed_cb(self, demux, param):
        pi = demux.get_property("pmt-info")
        self.debug("PMT info for program %s has changed" %
            pi.props.program_number)
        self.debug("PMT: PCR pid is 0x%04x" % pi.props.pcr_pid)
        for s in pi.props.stream_info:
            self.debug("PMT: Stream on pid 0x%04x" % s.props.pid)
            self.pids.append(s.props.pid)
        pid_string = "16:17:18"
        for pid in self.pids:
            pid_string = "%s:%d" % (pid_string, pid)
        self.debug ("Setting pid filter to %s" % (pid_string))
        self.src.set_property("pids", pid_string)

    def _pad_added_cb(self, demux, pad):
        padname = pad.get_name()
        caps_str = pad.get_caps().to_string()
        is_video = True
        self.debug("pad %s got added with caps: %s" % (padname, caps_str))

        if "video/mpeg" in caps_str:
            if self.no_more_pads:
                self.info("This pad added is in a new segment")
                if self.pending_video:
                    self.info("We already have a pending video pad, ignoring")
                else:
                    # let's create a new queue and decoder
                    queue = gst.element_factory_make("queue")
                    queue.set_property("max-size-time", 0)
                    queue.set_property("max-size-buffers", 0)
                    decoder = gst.element_factory_make("mpeg2dec")
                    self.add(queue, decoder)
                    queue.link(decoder)
                    # we will have to wait, cannot connect identity now
                    pad.link(queue.get_pad("sink"))
                    # don't set to playing yet
                    self.pending_video = (padname, queue, decoder)
            elif self.current_video_pad:
                self.info("We already have a current video pad, so ignoring")
                return
            self.current_video_pad = pad
        if "audio/mpeg" in caps_str:
            if self.no_more_pads:
                self.info("This pad added is in a new segment")
                if self.pending_audio:
                    self.info("We already have a pending video pad, ignoring")
                else:
                    # let's create a new queue and decoder
                    queue = gst.element_factory_make("queue")
                    queue.set_property("max-size-time", 0)
                    queue.set_property("max-size-buffers", 0)
                    decoder = gst.element_factory_make("mad")
                    self.add(queue, decoder)
                    queue.link(decoder)
                    # we will have to wait, cannot connect identity now
                    pad.link(queue.get_pad("sink"))
                    # don't set to playing yet
                    self.pending_audio = (padname, queue, decoder)

            if self.current_audio_pad:
                self.info("We already have a current audio pad, so ignoring")
                return
            self.current_audio_pad = pad
            is_video = False
        if padname in self.pids_elements:
            self.debug("we already have this pad, so must be soon removing")
        else:
            decoder = None
            if is_video:
                decoder = gst.element_factory_make("mpeg2dec")
            else:
                decoder = gst.element_factory_make("mad")

            if decoder:
                queue = gst.element_factory_make("queue")
                queue.set_property("max-size-time", 0)
                queue.set_property("max-size-buffers", 0)
                identity = None
                if is_video and self.video_elements:
                    identity = self.video_elements[0]
                elif not is_video and self.audio_elements:
                    identity = self.audio_elements[0]
                else:
                    identity = gst.element_factory_make("identity")
                    identity.set_property("single-segment", True)
                    identity.set_property("silent", True)
                    self.add(identity)
                self.add(queue, decoder)
                pad.link(queue.get_pad("sink"))
                queue.link(decoder)
                decoder.link(identity)
                queue.set_state(gst.STATE_PLAYING)
                decoder.set_state(gst.STATE_PLAYING)
                identity.set_state(gst.STATE_PLAYING)
                self.pids_elements[padname] = (queue, decoder, identity)
                if is_video and not self.video_elements:
                    event_probe_id = identity.get_pad("sink").add_event_probe(
                        self._event_probe_cb, padname)
                    self.video_elements = (identity, event_probe_id)
                elif not is_video and not self.audio_elements:
                    event_probe_id = identity.get_pad("sink").add_event_probe(
                        self._event_probe_cb, padname)
                    self.audio_elements = (identity, event_probe_id)
                identity.get_pad("src").set_blocked_async(True,
                    self._pad_blocked_cb, padname)
                self.debug("just added decoder for this pad")

    def _pad_removed_cb(self, pad):
        padname = pad.get_name()
        self.debug("%s has been removed from demuxer" % padname)

    def _no_more_pads_cb(self):
        self.no_more_pads = True

    def _pad_blocked_cb(self, pad, blocked, padname):
        if blocked:
            identity_ghostpad = gst.GhostPad(padname, pad)
            self.add_pad(identity_ghostpad)
            pad.set_blocked_async (False, self._pad_blocked_cb, padname)
        else:
            self.debug("unblocked pad %s" % padname)

    def _event_probe_cb(self, pad, event, padname):
        if event.type == gst.EVENT_EOS:
            self.info("End of stream received for identity referring to %s",
                padname)
            gobject.idle_add(self._remove_elements, padname)
            return False
        return True

    def _remove_elements(self, padname):
        self.info("Removing elements for padname %s" % padname)
        element_tuple = self.pid_elements[padname]
        element_tuple[0].unlink(element_tuple[1])
        element_tuple[1].unlink(element_tuple[2])
        self.remove(element_tuple[0])
        self.remove(element_tuple[1])
        del self.pid_elements[padname]
        if "video" in padname and not self.current_audio_pad:
            # we have now removed both video and audio
            self.current_video_pad = None
            if self.pending_video:
                padname = self.pending_video[0]
                queue = self.pending_video[1]
                decoder = self.pending_video[2]
                decoder.link(element_tuple[2])
                queue.set_state(gst.STATE_PLAYING)
                decoder.set_state(gst.STATE_PLAYING)
                self.pending_video = None
            self.no_more_pads = False
        elif "audio" in padname and not self.current_video_pad:
            # we have now removed both video and audio
            self.current_audio_pad = None
            if self.pending_audio:
                padname = self.pending_video[0]
                queue = self.pending_video[1]
                decoder = self.pending_video[2]
                decoder.link(element_tuple[2])
                queue.set_state(gst.STATE_PLAYING)
                decoder.set_state(gst.STATE_PLAYING)
                self.pending_audio = None
            self.no_more_pads = False
        return False

def dvb_pad_added(element, pad, dvbsrc, pipeline, what_we_have):
    if element == dvbsrc:
        if what_we_have["video"] and "video" in pad.get_name():
            print "We already have video, bye"
        elif "video" in pad.get_name():
            what_we_have["video"] = True
            print "we do not have video, connecting fakesink"
            fakesink = gst.element_factory_make("fakesink")
            fakesink.set_proprty("silent", True)
            fakesink.set_property("sync", False)
            pipeline.add(fakesink)
            pad.link(fakesink.get_pad("sink"))
            fakesink.set_state(gst.STATE_PLAYING)

        elif what_we_have["audio"] and "audio" in pad.get_name():
            print "We already have audio bye"
        elif "audio" in pad.get_name():
            what_we_have["audio"] = True
            print "we do not have audio, connecting fakesink"
            fakesink = gst.element_factory_make("fakesink")
            fakesink.set_property("silent", True)
            fakesink.set_property("sync", False)
            pipeline.add(fakesink)
            pad.link(fakesink.get_pad("sink"))
            fakesink.set_state(gst.STATE_PLAYING)

def bus_watch_func(bus, message):
    t = message.type
    src = message.src

    # print 'message:', t, src and src.get_name() or '(no source)'
    if t == gst.MESSAGE_STATE_CHANGED:
        old, new, pending = message.parse_state_changed()
        print "state change: %r %s->%s pending: %s" % (src, old.value_nick,
            new.value_nick, pending.value_nick)

gobject.threads_init()
pipeline = gst.Pipeline()
bus=pipeline.get_bus()
bus.add_signal_watch()
bus.connect("message", bus_watch_func)
tuning_params = [ ("modulation", "QAM 64"), ("trans-mode", "8k"),
    ("bandwidth", "8"), ("freq", 794000000), ("code-rate-lp", "AUTO"),
    ("code-rate-hp", "2/3"), ("guard", "4") ]
dvbsrc = DvbBin(tuning_params, 801)
what_we_have = { "audio": False, "video": False }
dvbsrc.connect("pad-added", dvb_pad_added, dvbsrc, pipeline, what_we_have)
pipeline.add(dvbsrc)
pipeline.set_state(gst.STATE_PLAYING)
mainloop = gobject.MainLoop()
try:
    mainloop.run()
except KeyboardInterrupt:
    print "keyboard interrupted"
<commit_msg>Fix typo. Activate pad before adding to bin.<commit_after>#!/usr/bin/python
# vi:si:et:sw=4:sts=4:ts=4

import pygst
pygst.require('0.10')
import gst
import gobject

class DvbBin(gst.Bin):

    # pids we currently allow through the dvbsrc filter
    pids = []
    # current program number
    program_number = -1
    # dict keyed on the padname (corresponds to pid)
    # values are a tuple of (queue, decoder, identity) element objects
    pids_elements = {}
    # value of the following is (identity, event_probe_id)
    video_elements = None
    audio_elements = None
    # following are currently used pads for demuxer
    current_video_pad = None
    current_audio_pad = None
    # the following are tuples of: (padname, queue, decoder)
    pending_video = None
    pending_audio = None
    # This flag means that we will not receive new pads for this current
    # segment
    no_more_pads = False

    ghostpads = {}

    def __init__(self, tuning_params, program_number):
        gobject.GObject.__init__(self)
        self.src = gst.element_factory_make("dvbsrc")
        for param in tuning_params:
            print "Setting property %s to %r" % (param[0], param[1])
            self.src.set_property(param[0], param[1])
        self.src.set_property("pids", "16:17:18")
        self.demux = gst.element_factory_make("flutsdemux")
        self.add(self.src, self.demux)
        self.program_number = program_number
        self.src.link(self.demux)
        self.demux.connect("pad-added", self._pad_added_cb)
        self.demux.connect("pad-removed", self._pad_removed_cb)
        self.demux.connect("no-more-pads", self._no_more_pads_cb)
        self.demux.connect("notify::pat-info", self._pat_info_changed_cb)
        self.demux.connect("notify::pmt-info", self._pmt_info_changed_cb)

    def _pat_info_changed_cb(self, demux, param):
        self.debug("PAT info has changed")
        pi = demux.get_property("pat-info")
        for prog in pi:
            self.debug("PAT: Program %d on PID 0x%04x" % (
                prog.props.program_number, prog.props.pid))
            if prog.props.program_number == self.program_number:
                self.pids = [prog.props.pid]
                self.src.set_property("pids", "16:17:18:%d" % prog.props.pid)
                self.demux.set_property("program-number", self.program_number)

    def _pmt_info_changed_cb(self, demux, param):
        pi = demux.get_property("pmt-info")
        self.debug("PMT info for program %s has changed" %
            pi.props.program_number)
        self.debug("PMT: PCR pid is 0x%04x" % pi.props.pcr_pid)
        for s in pi.props.stream_info:
            self.debug("PMT: Stream on pid 0x%04x" % s.props.pid)
            self.pids.append(s.props.pid)
        pid_string = "16:17:18"
        for pid in self.pids:
            pid_string = "%s:%d" % (pid_string, pid)
        self.debug ("Setting pid filter to %s" % (pid_string))
        self.src.set_property("pids", pid_string)

    def _pad_added_cb(self, demux, pad):
        padname = pad.get_name()
        caps_str = pad.get_caps().to_string()
        is_video = True
        self.debug("pad %s got added with caps: %s" % (padname, caps_str))

        if "video/mpeg" in caps_str:
            if self.no_more_pads:
                self.info("This pad added is in a new segment")
                if self.pending_video:
                    self.info("We already have a pending video pad, ignoring")
                else:
                    # let's create a new queue and decoder
                    queue = gst.element_factory_make("queue")
                    queue.set_property("max-size-time", 0)
                    queue.set_property("max-size-buffers", 0)
                    decoder = gst.element_factory_make("mpeg2dec")
                    self.add(queue, decoder)
                    queue.link(decoder)
                    # we will have to wait, cannot connect identity now
                    pad.link(queue.get_pad("sink"))
                    # don't set to playing yet
                    self.pending_video = (padname, queue, decoder)
            elif self.current_video_pad:
                self.info("We already have a current video pad, so ignoring")
                return
            self.current_video_pad = pad
        if "audio/mpeg" in caps_str:
            if self.no_more_pads:
                self.info("This pad added is in a new segment")
                if self.pending_audio:
                    self.info("We already have a pending video pad, ignoring")
                else:
                    # let's create a new queue and decoder
                    queue = gst.element_factory_make("queue")
                    queue.set_property("max-size-time", 0)
                    queue.set_property("max-size-buffers", 0)
                    decoder = gst.element_factory_make("mad")
                    self.add(queue, decoder)
                    queue.link(decoder)
                    # we will have to wait, cannot connect identity now
                    pad.link(queue.get_pad("sink"))
                    # don't set to playing yet
                    self.pending_audio = (padname, queue, decoder)

            if self.current_audio_pad:
                self.info("We already have a current audio pad, so ignoring")
                return
            self.current_audio_pad = pad
            is_video = False
        if padname in self.pids_elements:
            self.debug("we already have this pad, so must be soon removing")
        else:
            decoder = None
            if is_video:
                decoder = gst.element_factory_make("mpeg2dec")
            else:
                decoder = gst.element_factory_make("mad")

            if decoder:
                queue = gst.element_factory_make("queue")
                queue.set_property("max-size-time", 0)
                queue.set_property("max-size-buffers", 0)
                identity = None
                if is_video and self.video_elements:
                    identity = self.video_elements[0]
                elif not is_video and self.audio_elements:
                    identity = self.audio_elements[0]
                else:
                    identity = gst.element_factory_make("identity")
                    identity.set_property("single-segment", True)
                    identity.set_property("silent", True)
                    self.add(identity)
                self.add(queue, decoder)
                pad.link(queue.get_pad("sink"))
                queue.link(decoder)
                decoder.link(identity)
                queue.set_state(gst.STATE_PLAYING)
                decoder.set_state(gst.STATE_PLAYING)
                identity.set_state(gst.STATE_PLAYING)
                self.pids_elements[padname] = (queue, decoder, identity)
                if is_video and not self.video_elements:
                    event_probe_id = identity.get_pad("sink").add_event_probe(
                        self._event_probe_cb, padname)
                    self.video_elements = (identity, event_probe_id)
                elif not is_video and not self.audio_elements:
                    event_probe_id = identity.get_pad("sink").add_event_probe(
                        self._event_probe_cb, padname)
                    self.audio_elements = (identity, event_probe_id)
                identity.get_pad("src").set_blocked_async(True,
                    self._pad_blocked_cb, padname)
                self.debug("just added decoder for this pad")

    def _pad_removed_cb(self, pad):
        padname = pad.get_name()
        self.debug("%s has been removed from demuxer" % padname)

    def _no_more_pads_cb(self):
        self.no_more_pads = True

    def _pad_blocked_cb(self, pad, blocked, padname):
        if blocked:
            identity_ghostpad = gst.GhostPad(padname, pad)
            self.debug("adding ghostpad")
            pad.set_blocked_async (False, self._pad_blocked_cb, padname)
            self.ghostpads[padname] = identity_ghostpad
        else:
            self.debug("unblocked pad %s" % padname)
            if padname in self.ghostpads:
                self.ghostpads[padname].set_active(True)
                self.add_pad(self.ghostpads[padname])
                del self.ghostpads[padname]

    def _event_probe_cb(self, pad, event, padname):
        if event.type == gst.EVENT_EOS:
            self.info("End of stream received for identity referring to %s",
                padname)
            gobject.idle_add(self._remove_elements, padname)
            return False
        return True

    def _remove_elements(self, padname):
        self.info("Removing elements for padname %s" % padname)
        element_tuple = self.pid_elements[padname]
        element_tuple[0].unlink(element_tuple[1])
        element_tuple[1].unlink(element_tuple[2])
        self.remove(element_tuple[0])
        self.remove(element_tuple[1])
        del self.pid_elements[padname]
        if "video" in padname and not self.current_audio_pad:
            # we have now removed both video and audio
            self.current_video_pad = None
            if self.pending_video:
                padname = self.pending_video[0]
                queue = self.pending_video[1]
                decoder = self.pending_video[2]
                decoder.link(element_tuple[2])
                queue.set_state(gst.STATE_PLAYING)
                decoder.set_state(gst.STATE_PLAYING)
                self.pending_video = None
            self.no_more_pads = False
        elif "audio" in padname and not self.current_video_pad:
            # we have now removed both video and audio
            self.current_audio_pad = None
            if self.pending_audio:
                padname = self.pending_video[0]
                queue = self.pending_video[1]
                decoder = self.pending_video[2]
                decoder.link(element_tuple[2])
                queue.set_state(gst.STATE_PLAYING)
                decoder.set_state(gst.STATE_PLAYING)
                self.pending_audio = None
            self.no_more_pads = False
        return False

def dvb_pad_added(element, pad, dvbsrc, pipeline, what_we_have):
    if element == dvbsrc:
        if what_we_have["video"] and "video" in pad.get_name():
            print "We already have video, bye"
        elif "video" in pad.get_name():
            what_we_have["video"] = True
            print "we do not have video, connecting fakesink"
            fakesink = gst.element_factory_make("fakesink")
            fakesink.set_property("silent", True)
            fakesink.set_property("sync", False)
            pipeline.add(fakesink)
            pad.link(fakesink.get_pad("sink"))
            fakesink.set_state(gst.STATE_PLAYING)

        elif what_we_have["audio"] and "audio" in pad.get_name():
            print "We already have audio bye"
        elif "audio" in pad.get_name():
            what_we_have["audio"] = True
            print "we do not have audio, connecting fakesink"
            fakesink = gst.element_factory_make("fakesink")
            fakesink.set_property("silent", True)
            fakesink.set_property("sync", False)
            pipeline.add(fakesink)
            pad.link(fakesink.get_pad("sink"))
            fakesink.set_state(gst.STATE_PLAYING)

def bus_watch_func(bus, message):
    t = message.type
    src = message.src

    # print 'message:', t, src and src.get_name() or '(no source)'
    if t == gst.MESSAGE_STATE_CHANGED:
        old, new, pending = message.parse_state_changed()
        print "state change: %r %s->%s pending: %s" % (src, old.value_nick,
            new.value_nick, pending.value_nick)

gobject.threads_init()
pipeline = gst.Pipeline()
bus=pipeline.get_bus()
bus.add_signal_watch()
bus.connect("message", bus_watch_func)
tuning_params = [ ("modulation", "QAM 64"), ("trans-mode", "8k"),
    ("bandwidth", "8"), ("freq", 794000000), ("code-rate-lp", "AUTO"),
    ("code-rate-hp", "2/3"), ("guard", "4") ]
dvbsrc = DvbBin(tuning_params, 801)
what_we_have = { "audio": False, "video": False }
dvbsrc.connect("pad-added", dvb_pad_added, dvbsrc, pipeline, what_we_have)
pipeline.add(dvbsrc)
pipeline.set_state(gst.STATE_PLAYING)
mainloop = gobject.MainLoop()
try:
    mainloop.run()
except KeyboardInterrupt:
    print "keyboard interrupted"
<|endoftext|>