Пример #1
0
def upload_to_albums(facebook_api: facebook.GraphAPI) -> None:
    """
    Uploads a picture from the user to the album the user must select
    
    Arguments:
        facebook_api (object) : facebook api graph
  
    """
    
    path = input_user_chat("Please enter the path of your picture: ")
    if path:
        albums_id = []
        show_albums(facebook_api, albums_id)
        select = int(input_user_chat("Select the album: ")) - 1
        select = validate_number(select, albums_id)
        caption = input_user_chat("Caption: ")
        try:
            facebook_api.put_photo(image = open(path, 'rb'), album_path = albums_id[select - 1] + "/photos",
                                   message = caption)
            print_write_chatbot("The photo has been uploaded successfully!", color = "green",
                                attrs_color = ["bold"])
        except Exception as error:
            write_log(STATUS_FILE, f"There was a problem opening the file, error: {error}", "Exception")
            print_write_chatbot(f"There was a problem opening the file, error: {error}", color = "red",
                                attrs_color = ["bold"])
Пример #2
0
def upload_to_facebook(img_path):
    # Takes image path
    config = ConfigParser()
    config.read('auth.ini')

    app_key = config.get('facebook_credentials', 'app_key')
    app_key_secret = config.get('facebook_credentials', 'app_key_secret')
    user_access_token = config.get('facebook_credentials', 'user_access_token')
    page_id = config.get('facebook_credentials', 'page_id')

    graph = GraphAPI(user_access_token)

    resp = graph.get('me/accounts')
    page_access_token = None

    for page in resp['data']:
        if page['id'] == page_id:
            page_access_token = page['access_token']

    graph = GraphAPI(page_access_token)

    graph = GraphAPI(access_token=access_token, version="3.1")

    print('Uploading photo...')
    image = open(img_path, 'rb')
    graph.put_photo(image, message='Test caption')
Пример #3
0
class JeromeBot():
    def __init__(self):
        self.graph = GraphAPI(access_token=token)

    def img_to_file(self, img):
        img_byte_arr = BytesIO()
        img.save(img_byte_arr, format='PNG')
        return (img_byte_arr.getvalue())

    def post_image(self, img, description):
        img_file = self.img_to_file(img)
        return self.graph.put_photo(image=img_file, message=description)

    def get_dolar(self):
        req = requests.get('https://economia.awesomeapi.com.br/all/USD-BRL')
        return req.json()['USD']['bid'][:4]

    def make_image(self, font, src_img, msgs):
        FONT = ImageFont.truetype(font, 60)
        base = Image.open(src_img).convert('RGBA')
        txt = Image.new('RGBA', base.size, (255, 255, 255, 0))
        d = ImageDraw.Draw(txt)
        for msg in msgs:
            d.text(msg['pos'], msg['msg'], font=FONT, fill=msg['color'])
        return Image.alpha_composite(base, txt)
Пример #4
0
def upload_photo(facebook_api: facebook.GraphAPI) -> None:
    """
    Asks the user the path of the photo and the caption
    the user wants to upload, and uploads the photo and the caption
    
    Arguments:
        facebook_api (object) : facebook api graphThe facebook api
    
    """
    path = search_file()
    caption = input_user_chat("Caption: ")
    try:
        facebook_api.put_photo(image = open(path, 'rb'), message = caption)
        print_write_chatbot("The photo has been uploaded successfully!", color = 'green', attrs_color = ["bold"])
    except Exception as error:
        write_log(STATUS_FILE, f"There was a problem uploading the file, error: {error}", 'Exception')
        print_write_chatbot(f"There was a problem uploading the file, error: {error}", color = "red",
                            attrs_color = ["bold"])
Пример #5
0
class FacebookAdapter(MessageProvider, ImageProvider, VideoProvider):
    id = 'facebook'
    name = 'FacebookAdapter'

    def __init__(self, user, *args, **kwargs):
        self.user = user
        self.social_token = SocialToken.objects.filter(app__provider='facebook',
                                                       account__provider='facebook',
                                                       account__user=user)
        self.facebook = GraphAPI(self.social_token.get().token)
        self.social_app = SocialApp.objects.filter(id=self.social_token.get().app.id)

    def publish_image(self, image, message='', **kwargs):
        try:
            logger.info('trying to update facebook status with an image, for user: %s' % self.user)
            result = self.facebook.put_photo(open(image.path), message, **kwargs)
            logger.info(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e

    def publish_message(self, message, **kwargs):
        try:
            logger.info('trying to update facebook status, for user: %s' % self.user)
            result = self.facebook.put_wall_post(message)
            logger.info(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e

    def publish_video(self, video, title='', description='', **kwargs):
        try:
            logger.info('trying to update facebook status with a video, for user: %s' % self.user)
            result = self.facebook.put_video(open(video.path), title=title, description=description)
            logger.info(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e
Пример #6
0
class FacebookAdapter(MessageProvider, ImageProvider, ActionMessageProvider):
    id = 'facebook'
    name = 'FacebookAdapter'

    def __init__(self, user, *args, **kwargs):
        self.user = user
        self.social_token = SocialToken.objects.filter(
            app__provider='facebook',
            account__provider='facebook',
            account__user=user)
        self.facebook = GraphAPI(self.social_token.get().token)
        self.social_app = SocialApp.objects.filter(
            id=self.social_token.get().app.id)

    def publish_image(self, image, message='', **kwargs):
        try:
            logger.info(
                'trying to update facebook status with an image, for user: %s'
                % self.user)
            result = self.facebook.put_photo(open(image.path), message,
                                             **kwargs)
            logger.info(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e

    def publish_message(self, message, **kwargs):
        """
            Attachment:
            {"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"}
        """
        try:
            logger.info('trying to update facebook status, for user: %s' %
                        self.user)
            attachment = dict(kwargs.pop('attachment', {}))
            if 'picture' in attachment:
                attachment['picture'] = '%s%s' % (attachment.pop(
                    'domain', ''), attachment['picture'].url)
            result = self.facebook.put_wall_post(message, attachment)
            logger.info(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e

    def publish_video(self, video, title='', description='', **kwargs):
        try:
            logger.info(
                'trying to update facebook status with a video, for user: %s' %
                self.user)
            result = self.facebook.put_video(open(video.path),
                                             title=title,
                                             description=description)
            logger.info(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e

    def publish_action_message(self, message, action_info, **kwargs):
        """
            action_info: dictionary with information about the corresponding app activity/action
            {
                 "link": the url for the app activity/action to point to,
                 "actor": the actor,
                 "action": the action performed by 'actor',
                 "verb": the verbal form to show for the action performed by 'actor',
                 "target": the target of the action,
                 "app": the application name,
                 "domain": the application domain
                 "picture": the picture to show
             }
        """
        try:
            logger.info('trying to update facebook status, for user: %s' %
                        self.user)
            from django.utils.translation import ugettext as _

            result = self.facebook.put_wall_post(
                message, {
                    'link':
                    action_info.get('link', action_info.get('domain', '')),
                    'caption':
                    "%s | %s" % (action_info.get(
                        'app', ''), action_info.get('action', '')),
                    'description':
                    "%s %s %s %s %s" %
                    (action_info.get('actor', ''), action_info.get(
                        'verb', ''), action_info.get('target', ''),
                     _(u'using'), action_info.get('app', _(u'application'))),
                    'picture':
                    "%s%s" %
                    (action_info.get('domain', ''), action_info['picture'].url)
                    if 'picture' in action_info else ''
                })
            logger.info(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e
Пример #7
0
class FacebookAdapter(MessageProvider, ImageProvider, ActionMessageProvider):
    id = 'facebook'
    name = 'FacebookAdapter'

    def __init__(self, user, *args, **kwargs):
        self.user = user
        self.social_token = SocialToken.objects.filter(app__provider='facebook',
                                                       account__provider='facebook',
                                                       account__user=user)
        self.facebook = GraphAPI(self.social_token.get().token)
        self.social_app = SocialApp.objects.filter(id=self.social_token.get().app.id)

    def publish_image(self, image, message='', **kwargs):
        try:
            logger.info('trying to update facebook status with an image, for user: %s' % self.user)
            result = self.facebook.put_photo(open(image.path), message, **kwargs)
            logger.info(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e

    def publish_message(self, message, **kwargs):
        """
            Attachment:
            {"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"}
        """
        try:
            logger.info('trying to update facebook status, for user: %s' % self.user)
            attachment = dict(kwargs.pop('attachment', {}))
            if 'picture' in attachment:
                attachment['picture'] = '%s%s' % (attachment.pop('domain', ''), attachment['picture'].url)
            result = self.facebook.put_wall_post(message, attachment)
            logger.info(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e

    def publish_video(self, video, title='', description='', **kwargs):
        try:
            logger.info('trying to update facebook status with a video, for user: %s' % self.user)
            result = self.facebook.put_video(open(video.path), title=title, description=description)
            logger.info(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e

    def publish_action_message(self, message, action_info, **kwargs):
        """
            action_info: dictionary with information about the corresponding app activity/action
            {
                 "link": the url for the app activity/action to point to,
                 "actor": the actor,
                 "action": the action performed by 'actor',
                 "verb": the verbal form to show for the action performed by 'actor',
                 "target": the target of the action,
                 "app": the application name,
                 "domain": the application domain
                 "picture": the picture to show
             }
        """
        try:
            logger.info('trying to update facebook status, for user: %s' % self.user)
            from django.utils.translation import ugettext as _

            result = self.facebook.put_wall_post(
                message, {
                    'link': action_info.get('link', action_info.get('domain', '')),
                    'caption': "%s | %s" % (action_info.get('app', ''), action_info.get('action', '')),
                    'description': "%s %s %s %s %s" % (
                        action_info.get('actor', ''),
                        action_info.get('verb', ''),
                        action_info.get('target', ''),
                        _(u'using'),
                        action_info.get('app', _(u'application'))
                    ),
                    'picture': "%s%s" % (action_info.get('domain', ''), action_info['picture'].url) if 'picture' in action_info else ''
                }
            )
            logger.info(str(result))
            return result
        except Exception as e:
            logger.error(e)
            raise e