Exemplo n.º 1
0
    def handle(cls, *args, **options):

        # fetching our new Influencers
        influencers = Influencer.objects.filter(Q(fb_url__icontains='facebook.com/askfmpage')
                                                | Q(tw_url__icontains='twitter.com/ask_fm')
                                                | Q(insta_url__icontains='instagram.com/ask_fm')
                                                | Q(insta_url__icontains='instagram.com/askfm')
                                                | Q(blog_url__startswith='http://ask.fm/')
                                                )
        influencers = influencers.filter(instagram_profile__isnull=False)
        inf_queryset = queryset_iterator(influencers)

        ctr = 0
        print('Started performing ask.fm Influencers (total: %s)...' % influencers.count())
        for inf in inf_queryset:
            blog_url_is_changed = False
            fb_is_changed = False
            tw_is_changed = False
            insta_is_changed = False

            if inf.blog_url is not None and inf.blog_url.startswith('http://ask.fm/'):
                blog_url_is_changed = True
                inf.blog_url = 'http://theshelf.com/artificial' + inf.blog_url
            if inf.fb_url is not None and 'facebook.com/askfmpage' in inf.fb_url:
                inf.fb_url = None
                fb_is_changed = True
            if inf.tw_url is not None and 'twitter.com/ask_fm' in inf.tw_url:
                inf.tw_url = None
                tw_is_changed = True
            if inf.insta_url is not None and ('instagram.com/ask_fm' in inf.insta_url or 'instagram.com/askfm' in inf.insta_url):
                inf.insta_url = None
                if inf.instagram_profile.count() == 1:
                    inf.insta_url = 'http://instagram.com/' + inf.instagram_profile.all()[0].username
                insta_is_changed = True

            if fb_is_changed or tw_is_changed or insta_is_changed or blog_url_is_changed:
                inf.description = None
                inf.save()
                if blog_url_is_changed:
                    admin_helpers.handle_blog_url_change(inf, inf.blog_url)
                if fb_is_changed:
                    admin_helpers.handle_social_handle_updates(inf, 'fb_url', inf.fb_url)
                if tw_is_changed:
                    admin_helpers.handle_social_handle_updates(inf, 'tw_url', inf.tw_url)
                if insta_is_changed:
                    admin_helpers.handle_social_handle_updates(inf, 'insta_url', inf.insta_url)

            # Increasing counter
            ctr += 1
            if ctr % 100 == 0:
                print('%s influencers performed' % ctr)

        print('Total: %s' % ctr)
Exemplo n.º 2
0
def restore_influencers_urls(profiles_ids, to_save=False):
    """
    Here we take all new Influencers discovered via Instagram for 'singapore',
    check their platforms. And if platform occurs set its platform url to Influencer's corresponding *_url field
    :return:
    """
    from debra import admin_helpers
    print('Got %s profiles to check and correct...' % len(profiles_ids))

    profiles_with_conflicting_influencer = []

    def handle_field(inf, field_name, current_value, new_value):
        if current_value is None:
            print('Field %s is None, so restoring it to %s ... ' %
                  (field_name, new_value))
        else:
            print(
                'Field %s has a not empty value of %s, overwriting it with %s '
                % (field_name, current_value, new_value))
        setattr(inf, field_name, new_value)

    for idx, pid in enumerate(profiles_ids):
        profile = InstagramProfile.objects.get(id=pid)
        print("===========================================")
        print("%s. Profile id %s %s" % (idx, profile.id, profile.username))
        inf = profile.discovered_influencer

        print("Influencer id %s and %s" % (inf.id, inf))
        print("Getting platforms... ")
        platforms = Platform.objects.filter(
            influencer=inf,
            autovalidated=True,
            platform_name__in=Platform.SOCIAL_PLATFORMS_CRAWLED).exclude(
                url_not_found=True).order_by("platform_name")

        print('This influencer has %s social crawled platforms: %s' %
              (platforms.count(), [pl.platform_name for pl in platforms]))

        platform_names = [pl.platform_name for pl in platforms]
        if not 'Instagram' in platform_names:
            current_value = getattr(inf, 'insta_url')
            handle_field(inf, 'insta_url', current_value,
                         'http://instagram.com/' + profile.username)
        conflict_found = False
        for pl in platforms:
            field_name = Influencer.platform_name_to_field.get(
                pl.platform_name)
            if field_name is not None:
                current_value = getattr(inf, field_name)
                handle_field(inf, field_name, current_value, pl.url)
                # check if there is a conflict => meaning that the influencer we connected this profile to has
                # another instagram url which is also validated. So, we need to look at these a bit more
                if field_name == 'insta_url' and current_value:
                    u1 = platformutils.url_to_handle(current_value.lower())
                    u2 = platformutils.url_to_handle(pl.url.lower())
                    if u1 != u2:
                        profiles_with_conflicting_influencer.append(pid)
                        conflict_found = True
            else:
                print(
                    'Platform %s does not have a separate url field, skipping it.'
                    % pl.platform_name)

        if to_save and not conflict_found:
            print("Saving now")
            inf.save()
            admin_helpers.handle_social_handle_updates(inf, 'fb_url',
                                                       inf.fb_url)
            admin_helpers.handle_social_handle_updates(inf, 'pin_url',
                                                       inf.pin_url)
            admin_helpers.handle_social_handle_updates(inf, 'tw_url',
                                                       inf.tw_url)
            admin_helpers.handle_social_handle_updates(inf, 'insta_url',
                                                       inf.insta_url)
            admin_helpers.handle_social_handle_updates(inf, 'youtube_url',
                                                       inf.youtube_url)

        if to_save and conflict_found:
            profile.discovered_influencer = None
            profile.save()

    return profiles_with_conflicting_influencer
Exemplo n.º 3
0
def create_influencer_from_instagram(profile_id, to_save):
    profile = InstagramProfile.objects.get(id=profile_id)

    existing_infs, valid_urls = find_matching_influencers_for_profile(profile)
    # We don't handle the case when there're matching influencers
    if existing_infs:
        return False, existing_infs
    '''
    algorithm:
        1. Create an influencer with a fake blog url
        2. Then create a platform object for each of the platforms that we're
           able to discover
            - It could be a youtube or facebook or pinterest or twitter
                - Mark all these platforms as autovalidated
            - Use these platforms to discover other related platforms
                - These should be automatically validated also
            - Issue fetch tasks for these automatically validated platforms
        3. Extract email if given
    '''
    plats = []
    # creating a unique influencer blog url that is concurrency-safe
    blog_url = 'http://www.theshelf.com/artificial_blog/{}.html'.format(
        int(time.time()))
    inf = helpers.create_influencer_and_blog_platform(
        blog_url,
        influencer_source='discovered_via_instagram',
        to_save=to_save,
        platform_name_fallback=True)
    log.info('Influencer object %s created/fetched.', inf.id)

    if to_save:
        inf.save()
        _ = PlatformDataOp.objects.create(
            influencer=inf, operation='inf_articial_blog_from_instagram_crawl')

    for valid_url in valid_urls:
        platform = create_platform_for_influencer(url=valid_url,
                                                  inf=inf,
                                                  profile=profile,
                                                  to_save=to_save)
        if not platform:
            continue
        if to_save:
            field_name = Influencer.platform_name_to_field[
                platform.platform_name]
            admin_helpers.handle_social_handle_updates(inf, field_name,
                                                       platform.url)
        plats.append((
            platform,
            'discovered_via_instagram',
        ))

    log.debug('After performing all urls, insta_url is: %s', inf.insta_url)

    # now, using the created platforms, see if we can create new platforms
    platformextractor.do_further_validation_using_validated_platforms(
        plats, [])

    log.debug('After do_further_validation, insta_url is: %s', inf.insta_url)

    profile.discovered_influencer = inf
    if to_save:
        profile.valid_influencer = True
        profile.save()
        for platform, _ in plats:
            fetchertasks.fetch_platform_data.apply_async(
                [
                    platform.id,
                ], queue='new_influencer')

    log.debug('Finally Influencer has insta_url: %s', inf.insta_url)
    log.debug(
        ('And finally, profile with id %s should have discovered influencer '
         'with id: %s (to_save is %s)'), profile.id, inf.id, to_save)

    # Here we are fetching email, blogname, name, locations from platforms
    get_influencers_email_name_location_for_profile(profile_id,
                                                    to_save=to_save)
    # TODO: links to other platforms using @ sign or just like (snapchat: blah)

    return True, inf
Exemplo n.º 4
0
def brand_edit(request, brand_url, brand_id):
    from debra.admin_helpers import handle_blog_url_change, handle_social_handle_updates, update_or_create_new_platform

    try:
        brands_qs = Brands.objects
        brand = brands_qs.get(id=brand_id, domain_name=brand_url)
    except:
        raise Http404()
    if brand != request.visitor["brand"] and not request.user.is_superuser:
        print "privilages mismatched"
        return HttpResponseForbidden()

    influencer = brand.pseudoinfluencer
    try:
        profile = brand.userprofile
    except:
        brand_helpers.create_profile_for_brand(brand)
        profile = brand.userprofile

    if request.is_ajax():
        try:
            data = json.loads(request.body)
        except ValueError:
            print "bad json"
            return HttpResponseBadRequest()

        old_style_tags = profile.style_tags
        profile.style_tags = ",".join(
            [x.strip() for x in data.get("tags") if x])

        influencer.blogname = data.get("blogname")
        influencer.email = data.get("email")
        influencer.demographics_location_normalized = data.get("location")
        influencer.demographics_location = data.get("location")
        influencer.description = data.get("bio")
        try:
            validated_on = json.loads(influencer.validated_on)
        except (ValueError, TypeError):
            validated_on = []
        validated_on.append(ADMIN_TABLE_INFLUENCER_SELF_MODIFIED)
        validated_on = list(set(validated_on))
        influencer.validated_on = json.dumps(validated_on)

        old_fb_url = influencer.fb_url
        influencer.fb_url = data.get('social', {}).get("facebook_page")
        if influencer.fb_url == "":
            influencer.fb_url = None
        old_tw_url = influencer.tw_url
        influencer.tw_url = data.get('social', {}).get("twitter_page")
        if influencer.tw_url == "":
            influencer.tw_url = None
        old_pin_url = influencer.pin_url
        influencer.pin_url = data.get('social', {}).get("pinterest_page")
        if influencer.pin_url == "":
            influencer.pin_url = None
        old_insta_url = influencer.insta_url
        influencer.insta_url = data.get('social', {}).get("instagram_page")
        if influencer.insta_url == "":
            influencer.insta_url = None
        old_blog_url = influencer.blog_url
        influencer.blog_url = data.get('social', {}).get("blog_page")
        if influencer.blog_url == "":
            influencer.blog_url = None

        brand.name = data.get("brandname")
        brand.save()
        influencer.save()
        profile.save()

        if influencer.blog_url != old_blog_url:
            handle_blog_url_change(influencer, influencer.blog_url)
        if influencer.fb_url != old_fb_url:
            handle_social_handle_updates(influencer, 'fb_url',
                                         influencer.fb_url)
        if influencer.tw_url != old_tw_url:
            handle_social_handle_updates(influencer, 'tw_url',
                                         influencer.tw_url)
        if influencer.pin_url != old_pin_url:
            handle_social_handle_updates(influencer, 'pin_url',
                                         influencer.pin_url)
        if influencer.insta_url != old_insta_url:
            handle_social_handle_updates(influencer, 'insta_url',
                                         influencer.insta_url)

        request.visitor.flush()
        return HttpResponse()
    else:
        profile_data = {
            "collaborations": None,
            "info_for_brands": None,
            'profile_img_url': brand.profile_pic,
            'cover_img_url': brand.cover_pic,
            "tags": profile.style_tags.split(","),
            "name": influencer.name,
            "blogname": influencer.blogname,
            "brandname": brand.name,
            "email": influencer.email,
            "location": influencer.demographics_location_normalized,
            "bio": influencer.description,
            "collaboration_types": influencer.collaboration_types,
            "how_you_work": influencer.how_you_work,
            "social": {
                "facebook_page": influencer.fb_url,
                "twitter_page": influencer.tw_url,
                "pinterest_page": influencer.pin_url,
                "instagram_page": influencer.insta_url,
                "bloglovin_page": influencer.bloglovin_url,
                "lb_page": influencer.lb_url,
                "blog_page": influencer.blog_url,
            }
        }

        return render(
            request, 'pages/brand_profile/brand_edit.html', {
                'influencer': brand.pseudoinfluencer,
                'brand': brand,
                'page': 'edit',
                'selected_tab': 'brand_profile',
                'hide_nav': False,
                'sub_page': 'brand_profile_edit',
                'profile_data': json.dumps(profile_data,
                                           cls=DjangoJSONEncoder),
                'collab_types': InfluencerCollaborations.COLLABORATION_TYPES
            })
Exemplo n.º 5
0
def run_on_new_infs():
    """
    This should be called every day so that new influencers are analyzed before QA can work on them.
    """
    query = Influencer.objects.filter(source__isnull=False,
                                      blog_url__isnull=False,
                                      blacklisted=False)
    query = query.filter(source__icontains='manual_')
    query = query.exclude(show_on_search=True)
    query = query.exclude(
        validated_on__contains=ADMIN_TABLE_INFLUENCER_INFORMATIONS)
    query = query.exclude(
        validated_on__contains=ADMIN_TABLE_INFLUENCER_SELF_MODIFIED)
    # this makes sure we have run the platform extraction
    with_social = query.filter(
        Q(gplus_url__isnull=False) | Q(fb_url__isnull=False)
        | Q(tw_url__isnull=False) | Q(pin_url__isnull=False)
        | Q(insta_url__isnull=False) | Q(youtube_url__isnull=False)
        | Q(bloglovin_url__isnull=False))

    for i in with_social:
        # first ensure that only valid social handles are left
        admin_helpers.handle_social_handle_updates(i, 'tw_url', i.tw_url)
        admin_helpers.handle_social_handle_updates(i, 'fb_url', i.fb_url)
        admin_helpers.handle_social_handle_updates(i, 'pin_url', i.pin_url)
        admin_helpers.handle_social_handle_updates(i, 'insta_url', i.insta_url)
        admin_helpers.handle_social_handle_updates(i, 'bloglovin_url',
                                                   i.bloglovin_url)
        admin_helpers.handle_social_handle_updates(i, 'gplus_url', i.gplus_url)
        detect_influencer_attributes(i)
Exemplo n.º 6
0
def save_account_settings(request):
    brand = request.visitor["brand"]
    user = request.visitor["user"]
    auth_user = request.visitor["auth_user"]
    base_brand = request.visitor["base_brand"]
    if not base_brand or not base_brand.is_subscribed:
        return HttpResponseForbidden('insufficient privileges')

    try:
        data = json.loads(request.body)
    except ValueError:
        return HttpResponseBadRequest('no data uploaded')

    form_data = data.get('data', {})
    if data.get("type") == 'personal_info':
        user.location = form_data.get('location')
        user.name = form_data.get('name')
        auth_user.email = form_data.get('email')
        user.set_setting("timezone", form_data.get('timezone'))
        user.save()
        auth_user.save()
    elif data.get("type") == 'change_password':
        if not auth_user.check_password(form_data.get('old_pw')):
            return HttpResponseForbidden('wrong current password')
        if form_data.get('new_pw2') != form_data.get('new_pw'):
            return HttpResponseForbidden('passwords are not same')
        auth_user.set_password(form_data.get('new_pw'))
        auth_user.save()
    elif data.get("type") == 'company_info':
        base_brand.name = form_data.get('company_name')
        base_brand.domain_name = utils.domain_from_url(
            form_data.get('company_url'))
        base_brand.save()
    elif data.get("type") == 'delete_account':
        auth_user.is_active = False
        prof = auth_user.userprofile
        brand = models.UserProfileBrandPrivilages.objects.filter(user_profile=prof)
        brand_names = ''
        for b in brand:
            brand_names += b.brand.domain_name
        auth_user.save()
        logout(request)
        send_mail(
            'Account delete requested from settings page',
            "User id=%i email=%s brands=%s requested account deletion" % (auth_user.id, auth_user.email, brand_names),
            '*****@*****.**',
            ['*****@*****.**', '*****@*****.**'],
            fail_silently=True
        )
    elif data.get("type") == 'brand_options':
        from debra.admin_helpers import handle_blog_url_change, handle_social_handle_updates, update_or_create_new_platform
        brand = get_object_or_404(models.Brands, id=form_data.get('id'))
        if not brand in base_brand.get_managed_brands():
            return HttpResponseForbidden()
        brand.name = form_data.get('name')
        brand.domain_name = utils.domain_from_url(form_data.get('url'))
        brand.save()

        pseudoinfluencer, _ = models.Influencer.objects.get_or_create(
            name=brand.domain_name, source='brands')
        pseudoinfluencer.blog_url = form_data.get('blogurl')
        pseudoinfluencer.fb_url = form_data.get('facebookurl')
        pseudoinfluencer.tw_url = form_data.get('twitterurl')
        pseudoinfluencer.pin_url = form_data.get('pinteresturl')
        pseudoinfluencer.insta_url = form_data.get('instagramurl')
        pseudoinfluencer.save()
        if pseudoinfluencer.blog_url:
            handle_blog_url_change(pseudoinfluencer, pseudoinfluencer.blog_url)
        if pseudoinfluencer.fb_url:
            handle_social_handle_updates(
                pseudoinfluencer, 'fb_url', pseudoinfluencer.fb_url)
        if pseudoinfluencer.tw_url:
            handle_social_handle_updates(
                pseudoinfluencer, 'tw_url', pseudoinfluencer.tw_url)
        if pseudoinfluencer.pin_url:
            handle_social_handle_updates(
                pseudoinfluencer, 'pin_url', pseudoinfluencer.pin_url)
        if pseudoinfluencer.insta_url:
            handle_social_handle_updates(
                pseudoinfluencer, 'insta_url', pseudoinfluencer.insta_url)
        pseudoinfluencer.save()

    elif data.get("type") == "email_settings":
        user.set_setting('email_features', form_data.get('feature', False))
        user.set_setting('email_content', form_data.get('content', False))
        user.set_setting('email_changes', form_data.get('changes', False))
        user.save()
    elif data.get("type") == "outreach":
        user.set_setting('outreach_all', form_data.get('all', False))
        for key, value in form_data.iteritems():
            if key.startswith('brand_'):
                try:
                    brand_id = int(key.split('brand_')[1])
                except:
                    return HttpResponseBadRequest()
                user.set_setting('outreach_brand_%i' % brand_id, value)
        user.save()
    else:
        print data

    return HttpResponse()
Exemplo n.º 7
0
def table_page(options):
    if options["request"].method == 'POST':

        try:
            body = json.loads(options["request"].body)
        except ValueError:
            body = options["request"].POST

        obj = get_object_or_404(options["model"], id=body.get('pk'))
        if obj.qa:
            obj.qa = " ".join(
                (obj.qa, options["request"].visitor["auth_user"].username))
        else:
            obj.qa = options["request"].visitor["auth_user"].username
        obj.save()

        name = body.get('name')

        if name == "update_collections":
            selected_collections = body.get('collections', [])
            groups = models.InfluencersGroup.objects.filter(
                id__in=constants.ATUL_COLLECTIONS_IDS)
            for group in groups:
                if group.id in selected_collections:
                    group.add_influencer(obj.influencer)
                else:
                    group.remove_influencer(obj.influencer)
            return HttpResponse()
        if name == "append_comment":
            data = body
            comment = data.get("new_comment")
            inf = models.Influencer.objects.get(id=data.get("influencer"))
            inf.append_comment(comment,
                               user=options["request"].visitor["auth_user"])
            return HttpResponse(
                serializers.transform_customer_comments(
                    value=inf.customer_comments))
        if name == "action:update":
            obj = None
            for k, v in options["request"].POST.iteritems():
                print("Inside action:update. [k,v]=[%r,%r]" % (k, v))
                if k.startswith("update:"):
                    k = k.split(':')
                    objtype = k[1]
                    fieldname = k[2]
                    print("objtype: [%r] fieldname: [%r]" %
                          (objtype, fieldname))
                    # WTF is this ???
                    if len(k) == 3:
                        row_id = k[3]
                    else:
                        row_id = options["request"].POST.get('iid')
                    if objtype == "i":
                        obj = get_object_or_404(models.Influencer, id=row_id)
                    elif objtype == "p":
                        obj = get_object_or_404(models.Platform, id=row_id)
                    print("Obj: [%r]" % obj)
                    if hasattr(obj, fieldname):
                        print("Setting %r field [%r] to %r, current: [%r]" %
                              (obj, fieldname, v, getattr(obj, fieldname)))
                        setattr(obj, fieldname, v)
                        obj.save()
                        if objtype == "i":
                            if fieldname == "blog_url":
                                admin_helpers.handle_blog_url_change(obj, v)
                            else:
                                #how to handle platforms which has no url in influencer model?
                                admin_helpers.handle_social_handle_updates(
                                    obj, fieldname, v)
            obj = get_object_or_404(options["model"],
                                    id=options["request"].POST.get('pk'))
            obj.status = models.InfluencerCheck.STATUS_MODIFIED
            obj.save()
            return HttpResponse()
        if name == "action:noproblem":

            obj = get_object_or_404(options["model"],
                                    id=options["request"].POST.get('pk'))
            obj.status = models.InfluencerCheck.STATUS_MODIFIED
            obj.save()
            print("Inside action:noproblem. [%r]" % obj)
            return HttpResponse()
        if name == "action:blacklist":
            row_id = options["request"].POST.get('iid')
            obj = get_object_or_404(models.Influencer, id=row_id)
            obj.set_blacklist_with_reason('suspicion_table')
            print("Inside action:blacklist. [%r]" % obj)
            obj = get_object_or_404(options["model"],
                                    id=options["request"].POST.get('pk'))
            obj.status = models.InfluencerCheck.STATUS_MODIFIED
            obj.save()

            return HttpResponse()
        if name == "action:blacklist_rel":
            print("Inside action:blacklist_rel. Not sure what this is")
            obj = get_object_or_404(options["model"],
                                    id=options["request"].POST.get('pk'))
            try:
                data_json = json.loads(obj.data_json)
            except:
                return HttpResponse()
            obj.status = models.InfluencerCheck.STATUS_MODIFIED
            obj.save()
            rel_obj = getattr(models,
                              data_json[0][0]).objects.get(id=data_json[0][1])
            rel_obj.set_blacklist_with_reason('suspicion_table')
            return HttpResponse()
        if name == "action:report":
            pass
        row_id = options["request"].POST.get('pk')
        obj = get_object_or_404(options["model"], id=row_id)
        value = options["request"].POST.get('value')
        data = {name: value}
        serializer = options["store_serializer"](obj, data=data, partial=True)
        if serializer.is_valid():
            serializer.save()
        else:
            return HttpResponseBadRequest(serializer.errors)
        return HttpResponse()
    else:
        if options.get("debug"):
            query = options["query"]
            data = admin_helpers.get_objects(options["request"],
                                             query,
                                             options["load_serializer"],
                                             context=options['context'])
            return HttpResponse("<body></body>")
        if options["request"].is_ajax():
            query = options["query"]
            data = admin_helpers.get_objects(options["request"],
                                             query,
                                             options["load_serializer"],
                                             context=options['context'])
            return HttpResponse(data, content_type="application/json")
        else:
            return render(options["request"],
                          options["template"],
                          options["context"],
                          context_instance=RequestContext(options["request"]))