Beispiel #1
0
def get_tw_handles_buffer(request, id):
    try:
        company = Company.objects.filter(company_id=id).first()
        
        existingIntegration = CompanyIntegration.objects(company_id = id ).first()
        if 'bufr' in existingIntegration['integrations']: # if Buffer is present and configured
            client_id = existingIntegration['integrations']['bufr']['client_id']
            client_secret = existingIntegration['integrations']['bufr']['client_secret']
            access_token = existingIntegration['integrations']['bufr']['access_token']
            buffer = Buffer()
            api = Buffer.get_api(buffer, client_id=client_id, client_secret=client_secret, access_token=access_token)
            profiles = Buffer.get_twitter_profiles(buffer, api)
            results = []
            for profile in profiles:
                new_profile = {}
                new_profile['id'] = profile['id']
                new_profile['service'] = profile['service']
                new_profile['service_id'] = profile['service_id']
                new_profile['service_username'] = profile['service_username']
                results.append(new_profile)
            #serializedList = BufferProfileSerializer(profiles, many=True)
            return JsonResponse({'results' :results})
        else:
            return JsonResponse({'error' : 'No integration found with Buffer'})
    except Exception as e:
        return JsonResponse({'Error' : str(e)})
Beispiel #2
0
def publishMl(request, id, masterlist_id):
    try:
        company = Company.objects.filter(company_id=id).first()
        ml = TweetMasterList.objects(Q(company=company.id) & Q(id=masterlist_id)).first()
        
        if ml is None:
            return HttpResponse("Could not find the Master List for publishing", status=status.HTTP_400_BAD_REQUEST)
        tweets = ml['tweets']
        shuffle(tweets)
        #buffer_profile_id = '558f150b7409ab382f11a39e' #change to parameter later
        
        existingIntegration = CompanyIntegration.objects(company_id = id ).first()
        if 'bufr' in existingIntegration['integrations']: # if Buffer is present and configured
            client_id = existingIntegration['integrations']['bufr']['client_id']
            client_secret = existingIntegration['integrations']['bufr']['client_secret']
            access_token = existingIntegration['integrations']['bufr']['access_token']
            buffer = Buffer()
            api = Buffer.get_api(buffer, client_id=client_id, client_secret=client_secret, access_token=access_token)
            #profile = Buffer.get_twitter_profile(buffer, api)
            profile = Buffer.get_twitter_profile_by_id(buffer, api, ml.buffer_profile_id)
            #print 'posting to profile ' + profile['id']
            for tweet in tweets:
                try:
                    post = quote_plus(tweet['text'])
                    Buffer.post_to_twitter(buffer, api, post, profile)
                except Exception as e:
                    continue
            TweetMasterList.objects(Q(company=company.id) & Q(id=masterlist_id)).update(published=True)
            TweetMasterList.objects(Q(company=company.id) & Q(id=masterlist_id)).update(published_date=datetime.utcnow())
            return HttpResponse("Tweets posted", status=status.HTTP_200_OK)
        else:
            return HttpResponse("No publishing integration found", status=status.HTTP_400_BAD_REQUEST)
    
    except Exception as e:
        return JsonResponse({'Error' : str(e)})
Beispiel #3
0
def retrieveBufrTwInteractions(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None):
    try:
        print 'starting retrieveBufrTwInteractions for company ' + str(company_id)
        existingIntegration = CompanyIntegration.objects(company_id = company_id).first()
        if 'bufr' in existingIntegration['integrations']: # if Buffer is present and configured
            print 'found buffer'
            client_id = existingIntegration['integrations']['bufr']['client_id']
            client_secret = existingIntegration['integrations']['bufr']['client_secret']
            access_token = existingIntegration['integrations']['bufr']['access_token']
            buffer = Buffer()
            api = Buffer.get_api(buffer, client_id=client_id, client_secret=client_secret, access_token=access_token)
            profiles = Buffer.get_twitter_profiles(buffer, api)
            for profile in profiles:
                results = buffer.get_twitter_updates(profile)
                saveBufrTwInteractions(user_id=user_id, company_id=company_id, results=results, job_id=job_id, run_type=run_type)
                #print 'Tw results are ' + str(results)
        else:
            print 'No integration found with Buffer'
            return JsonResponse({'error' : 'No integration found with Buffer'})
        try:
            message = 'Twitter interactions retrieved from Buffer'
            notification = Notification()
            #notification.company_id = company_id
            notification.owner = user_id
            notification.module = 'Social'
            notification.type = 'Background task' 
            notification.method = os.path.basename(__file__)
            notification.message = message
            notification.success = True
            notification.read = False
            notification.save()
        except Exception as e:
            send_notification(dict(
             type='error',
             success=False,
             message=str(e)
            ))         
    except Exception as e:
        send_notification(dict(
             type='error',
             success=False,
             message=str(e)
            ))