Beispiel #1
0
    def get_ad_data_or_sleep(self, ad):
        """
        Get needed data for ads or sleep
        :param ad:
        :return:
        """
        while True:
            ad_data = {'ad_id': ad}

            try:

                ad = Ad(ad)

                creative = AdCreative(
                    ad.api_get(fields=[Ad.Field.creative])['creative']['id'])
                fields = [
                    AdCreative.Field.call_to_action_type,
                    AdCreative.Field.object_story_spec
                ]

                creative.api_get(fields=fields)
                creative = dict(creative)
                try:
                    call_to_action = creative['object_story_spec'][
                        'video_data']['call_to_action']
                    try:
                        print(creative['object_story_spec']['photo_data'])
                    except KeyError:
                        pass

                    ad_data['website_url'] = call_to_action['value']['link']

                    ad_data['call_to_action_type'] = call_to_action['type']
                    ad_data['headline'] = creative['object_story_spec'][
                        'video_data']['title']
                    ad_data['description'] = creative['object_story_spec'][
                        'video_data']['message']

                    video = AdVideo(creative['object_story_spec']['video_data']
                                    ['video_id'])
                    ad_data['video_name'] = video.api_get(
                        fields=[AdVideo.Field.title])['title']
                except KeyError:
                    pass
                self.current_ads.append(ad_data)
            except FacebookRequestError as e:
                print(type(e.api_error_code()))
                if e.api_error_code() == 803:
                    break
                print('Sleeping right now for ads')

                time.sleep(600)
            else:
                break

        return ad_data
    # duplicate adset
    asr = adsett.create_copy(params={'deep_copy': True})

    adset = AdSet(asr._data['copied_adset_id'])
    adset.api_update(params={AdSet.Field.name: vname})  # rename adset

    # adset ads
    ads = adset.get_ads(fields=[Ad.Field.id, Ad.Field.name, Ad.Field.creative])

    ad = ads[0]  # should only be one ad

    ad[Ad.Field.name] = vname
    ad.api_update(params={Ad.Field.name: vname})  # rename ad

    adc = AdCreative(ad[Ad.Field.creative][AdCreative.Field.id])
    adc = adc.api_get(fields=[AdCreative.Field.object_story_spec])

    fields = [
        AdCreative.Field.account_id,
        AdCreative.Field.actor_id,
        AdCreative.Field.adlabels,
        AdCreative.Field.applink_treatment,
        AdCreative.Field.asset_feed_spec,
        AdCreative.Field.authorization_category,
        AdCreative.Field.auto_update,
        AdCreative.Field.body,
        AdCreative.Field.branded_content_sponsor_page_id,
        AdCreative.Field.bundle_folder_id,
        AdCreative.Field.call_to_action_type,
        AdCreative.Field.categorization_criteria,
        AdCreative.Field.category_media_source,