예제 #1
0
class GoogleAuth(object):
    google_app: typing.Optional[SocialApp] = None
    social_auth_token: typing.Optional[SocialToken]
    _credentials: typing.Optional[GoogleCredentials] = None

    def __init__(
        self,
        social_auth_token: typing.Optional[SocialToken] = None,
    ) -> None:
        self.social_auth_token = social_auth_token
        self.google_app = SocialApp.objects.filter(provider="google").first()

    def auth_token_expired(self):
        return (self.social_auth_token.expires_at is not None
                and self.social_auth_token.expires_at < timezone.now())

    def init_credentials(self):
        """Build a `GoogleCredentials` object from the `SocialToken`/client IDs we have."""
        if self._credentials is not None:
            return

        self._credentials = GoogleCredentials(
            self.social_auth_token.token,
            self.google_app.client_id,
            self.google_app.secret,
            self.social_auth_token.token_secret,
            self.social_auth_token.expires_at,
            GOOGLE_TOKEN_URI,
            "Stencila Hub Client",
        )

    @property
    def credentials(self) -> typing.Optional[GoogleCredentials]:
        if self.social_auth_token is None:
            return None

        if self.google_app is None:
            return None

        self.check_and_refresh_token()
        return self._credentials

    def update_social_auth_token(self):
        self.social_auth_token.token = self._credentials.access_token
        self.social_auth_token.expires_at = timezone.make_aware(
            self._credentials.token_expiry, timezone.utc)
        self.social_auth_token.save()

    def check_and_refresh_token(self):
        """Refresh the credentials from Google, if expired (according to us)."""
        self.init_credentials()

        if not self.auth_token_expired():
            return self.social_auth_token.token

        http = self._credentials.authorize(httplib2.Http())
        self._credentials.refresh(http)
        self.update_social_auth_token()
        return self.social_auth_token.token
예제 #2
0
def refresh(client_id, client_secret, refresh_token):
    cred = GoogleCredentials(None, client_id, client_secret, refresh_token,
                             None,
                             "https://accounts.google.com/o/oauth2/token",
                             None)
    http = cred.authorize(httplib2.Http())
    cred.refresh(http)
    return cred.access_token
예제 #3
0
def pull_google_contacts(user, book):
    app = SocialApp.objects.filter(provider='google')[0]
    token = SocialToken.objects.get(account__user=user, app=app)
    creds = GoogleCredentials(
        access_token=token.token,
        token_expiry=None,
        token_uri=GOOGLE_TOKEN_URI,
        client_id=app.client_id,
        client_secret=app.secret,
        refresh_token=None,
        user_agent='Python',
        revoke_uri=None,
    )

    http = httplib2.Http()
    http = creds.authorize(http)
    people_service = build(serviceName='people', version='v1', http=http)

    try:
        connections = people_service.people().connections().list(
            resourceName='people/me', pageSize=50).execute()
    except HttpError:
        time.sleep(60)
        connections = people_service.people().connections().list(
            resourceName='people/me', pageSize=50).execute()
    except HttpAccessTokenRefreshError:
        sentry.error("Bad google token for user",
                     exc_info=True,
                     extra={"user": user})
        return

    next_page = connections.get('nextPageToken')
    sync_token = connections.get('nextSyncToken')

    get_expanded_response_and_create_contacts(connections, people_service,
                                              sync_token, book)
    try:
        success = True
        while next_page:
            try:
                connections = people_service.people().connections().list(
                    resourceName='people/me', pageSize=50,
                    pageToken=next_page).execute()
            except HttpError:
                time.sleep(60)
                connections = people_service.people().connections().list(
                    resourceName='people/me', pageSize=50,
                    pageToken=next_page).execute()
            get_expanded_response_and_create_contacts(
                connections, people_service, connections.get('nextSyncToken'),
                book)
            next_page = connections.get('nextPageToken')
        return success
    except:
        sentry.error("Google Import Error",
                     exc_info=True,
                     extra={"book": book})
        return False
예제 #4
0
def getDriveService(user_agent):

    with open(TOKEN) as f:
        creds = json.load(f)

    credentials = GoogleCredentials(None,creds["client_id"],creds["client_secret"],
                                          creds["refresh_token"],None,"https://accounts.google.com/o/oauth2/token",user_agent)
    http = credentials.authorize(Http())
    credentials.refresh(http)
    drive_service = build('drive', 'v3', http)

    return drive_service
예제 #5
0
 def dispatch(self, request, *args, **kwargs):
     if not gargoyle.is_active('import_from_google', request):
         return HttpResponseRedirect('/')
     app = SocialApp.objects.filter(provider='google')[0]
     url = "{}?process=connect&next={}".format(
         reverse("google_login"),
         reverse("import-google-contacts",
                 kwargs={'book': self.request.current_book.id}),
     )
     try:
         token = SocialToken.objects.get(account__user=self.request.user,
                                         app=app)
     except SocialToken.DoesNotExist:
         sentry.error("Social token missing in google import",
                      extra={
                          "user": self.request.user,
                      })
         return HttpResponseRedirect(url)
     try:
         creds = GoogleCredentials(
             access_token=token.token,
             token_expiry=None,
             token_uri=GOOGLE_TOKEN_URI,
             client_id=app.client_id,
             client_secret=app.secret,
             refresh_token=None,
             user_agent='Python',
             revoke_uri=None,
         )
         http = httplib2.Http()
         http = creds.authorize(http)
         people_service = build(serviceName='people',
                                version='v1',
                                http=http)
         connections = people_service.people().connections().list(
             resourceName='people/me', pageSize=50).execute()
     except HttpAccessTokenRefreshError:
         return HttpResponseRedirect(url)
     cache.set("{}::google-import".format(request.user.username),
               "processing", 86400)
     Channel('import-google-contacts').send({
         'user_id':
         self.request.user.id,
         'book_id':
         self.request.current_book.id
     })
     messages.success(
         request,
         "We're importing your Google contacts now! You'll receive an email when we're done."
     )
     return HttpResponseRedirect('/')
예제 #6
0
    def _get_credentials(self) -> dict:
        """ Authenticates with google fit and returns a dictionary with
        the most recent valid credentials """
        online_credentials = self.parameter_manager.get_multiple('/google_fit/')
        credentials = GoogleCredentials(**online_credentials, token_expiry=None, user_agent=None)

        http = credentials.authorize(httplib2.Http())
        credentials.refresh(http)

        credentials_dict = json.loads(credentials.to_json())
        self.access_token = credentials_dict.get("access_token")
        self._store_credentials_online(credentials_dict)

        return credentials_dict        
예제 #7
0
 def get_service(self):
     if self.access_code == '' or self.access_token == '' or self.refresh_token == '':
         self._start_connection()
     else:
         credentials = GoogleCredentials(self.access_token, self.client_id,
                                         self.client_secret,
                                         self.refresh_token, 3920,
                                         google_drive_info['url'], 'test')
         http = httplib2.Http()
         http = credentials.authorize(http)
         service = build(google_drive_info['name'],
                         google_drive_info['version'],
                         http=http)
         return service
예제 #8
0
def connect_to_gdrive(access_token, refresh_token):
    credentials = GoogleCredentials(
        access_token,
        os.environ['GDRIVE_API_CLIENT_ID'],
        os.environ['GDRIVE_API_CLIENT_SECRET'],
        refresh_token,
        None,
        "https://www.googleapis.com/oauth2/v4/token",
        "cuely/1.0"
    )
    http = httplib2.Http()
    http = credentials.authorize(http)
    service = discovery.build('drive', 'v3', http=http)
    return service
예제 #9
0
def configure_youtube():
    cred = GoogleCredentials(None, os.environ.get('GOOGLE_CLIENT_ID'),
                             os.environ.get('GOOGLE_CLIENT_SECRET'),
                             os.environ.get('GOOGLE_REFRESH_TOKEN'), None,
                             "https://accounts.google.com/o/oauth2/token", '')
    http = cred.authorize(httplib2.Http())
    cred.refresh(http)

    api_service_name = "youtube"
    api_version = "v3"

    return googleapiclient.discovery.build(api_service_name,
                                           api_version,
                                           credentials=cred,
                                           cache_discovery=False)
예제 #10
0
def fit_client():
    """ build google fit service
    """
    credentials = GoogleCredentials(
        access_token=None,
        client_id=app.config['GOOGLE_CLIENT_ID'],
        client_secret=app.config['GOOGLE_CLIENT_SECRET'],
        refresh_token=app.config['GOOGLE_REFRESH_TOKEN'],
        token_expiry=None,
        token_uri=GOOGLE_TOKEN_URI,
        user_agent="My Dashboard",
    )
    http = httplib2.Http()
    http = credentials.authorize(http)

    return build('fitness', 'v1', http=http)
예제 #11
0
def fit_client():
    """ build google fit service
    """
    credentials = GoogleCredentials(
        access_token=None,
        client_id=app.config['GOOGLE_CLIENT_ID'],
        client_secret=app.config['GOOGLE_CLIENT_SECRET'],
        refresh_token=app.config['GOOGLE_REFRESH_TOKEN'],
        token_expiry=None,
        token_uri=GOOGLE_TOKEN_URI,
        user_agent="My Dashboard",
    )
    http = httplib2.Http()
    http = credentials.authorize(http)

    return build('fitness', 'v1', http=http)
예제 #12
0
def service(user):
    credentials = GoogleCredentials(
        access_token=user.socialaccount_set.get(
            provider="google").socialtoken_set.first().token,
        refresh_token=user.socialaccount_set.get(
            provider="google").socialtoken_set.first().token_secret,
        client_id=user.socialaccount_set.get(
            provider="google").socialtoken_set.first().app.client_id,
        client_secret=user.socialaccount_set.get(
            provider="google").socialtoken_set.first().app.secret,
        token_expiry=user.socialaccount_set.get(
            provider="google").socialtoken_set.first().expires_at,
        token_uri='https://accounts.google.com/o/oauth2/token',
        user_agent="Miwo",
    )
    http = credentials.authorize(httplib2.Http())
    service = build("youtube", "v3", http)
    return service
예제 #13
0
    def initialize_analyticsreporting(self):
        if not self.is_api_ready:
            credentials = GoogleCredentials(
                access_token=self.access_token,
                client_id=self.client_id,
                client_secret=self.client_secret,
                refresh_token=self.refresh_token,
                token_expiry=None,
                token_uri="https://accounts.google.com/o/oauth2/token",
                user_agent=None,
                revoke_uri=GOOGLE_REVOKE_URI,
            )

            http = credentials.authorize(httplib2.Http())
            credentials.refresh(http)

            self._service = build(serviceName="webmasters",
                                  version="v3",
                                  credentials=credentials,
                                  cache_discovery=False)
예제 #14
0
def analytics_job(request):
    profile1 = Profile.objects.filter(user_id=request.user.id).first()
    if profile1.connected == True:
        access_token = profile1.access_token
        refresh_token = profile1.refresh_token
        google_authcode = profile1.google_authcode
        client_id = profile1.client_id
        client_secret = profile1.client_secret
        token_expiry = profile1.token_expiry
        token_uri = profile1.token_uri
        revoke_uri = profile1.revoke_uri
        account = profile1.accountid
        # credentials = GoogleCredentials.get_application_default()
        # credentials = OAuth2Credentials.get_access_token()
        # credentials = AccessTokenCredentials(access_token,'my-user-agent/1.0')
        # if credentials.access_token_expired:
        #    http_auth = credentials.authorize(httplib2.Http())
        # else:
        #    print "1"
        credentials = GoogleCredentials(access_token, client_id, client_secret, refresh_token, token_expiry, token_uri,
                                        'my-user-agent/1.0', revoke_uri)
        http_auth = credentials.authorize(httplib2.Http())
        # http_auth = credentials.refresh(httplib2.Http())
        # print '@@@',credentials.access_token_expired
        # credentials = credentials.refresh(credentials)
        # print credentials
        # if credentials.access_token_expired:
        #     print credentials.refresh(http_auth)
        # if credentials.access_token_expired:
        #     http_auth = credentials.authorize(httplib2.Http())
        # else:
        #     print 'not expired'
        #     #http_auth = credentials.authorize(httplib2.Http())
        #     refresh = credentials.refresh(httplib2.Http())
        #     print jsonpickle.encode(refresh)
        #     print refresh
        service = build('analytics', 'v3', http_auth, cache_discovery=False)

        # accounts = service.management().accounts().list().execute()
        # if accounts.get('items'):
        #     account = accounts.get('items')[0].get('id')

        properties = service.management().webproperties().list(accountId=account).execute()
        if properties.get('items'):
            property = properties.get('items')[0].get('id')

            profiles = service.management().profiles().list(accountId=account, webPropertyId=property).execute()
            if profiles.get('items'):
                profile = profiles.get('items')[0].get('id')

        service = build('analytics', 'v4', http_auth, cache_discovery=False)
        try:
            conversion_rate = service.reports().batchGet(
                body={
                    'reportRequests': [
                        {
                            'viewId': profile,
                            'dateRanges': [{'startDate': '100daysAgo', 'endDate': 'today'}],
                            'metrics': [{'expression': 'ga:goalConversionRateAll'}]
                        }]
                }
            ).execute()

            for ccr in conversion_rate['reports']:
                for rate in ccr['data']['totals']:
                    conversion_vals = rate['values']
        except:
            conversion_vals = [0]
            pass

        try:
            bounce_rate = service.reports().batchGet(
                body={
                    'reportRequests': [
                        {
                            'viewId': profile,
                            'dateRanges': [{'startDate': '100daysAgo', 'endDate': 'today'}],
                            'metrics': [{'expression': 'ga:bounceRate'}]
                        }]
                }
            ).execute()

            for cbr in bounce_rate['reports']:
                for rate in cbr['data']['totals']:
                    bounce_vals = rate['values']

        except:
            bounce_vals = [0]
            pass
            # raise Exception('Permission Denied')

        try:
            metric_expression = {
                'expression': 'ga:transactionRevenue/ga:transactions',
                'formattingType': 'FLOAT'
            }

            avg_order_value = service.reports().batchGet(
                body={
                    'reportRequests': [
                        {
                            'viewId': profile,
                            'dateRanges': [{'startDate': '100daysAgo', 'endDate': 'today'}],
                            'metrics': [metric_expression],
                        }]
                }
            ).execute()

            for caov in avg_order_value['reports']:
                for order in caov['data']['totals']:
                    order_value = order['values']

        except:
            order_value = [0]
            pass
            # raise Exception('Permission Denied')

        try:
            cart_abandonment_rate = service.reports().batchGet(
                body={
                    'reportRequests': [
                        {
                            'viewId': profile,
                            'dateRanges': [{'startDate': '100daysAgo', 'endDate': 'today'}],
                            'metrics': [{'expression': 'ga:goalAbandonRateAll'}],
                        }]
                }
            ).execute()

            for ccar in cart_abandonment_rate['reports']:
                for cart_rate in ccar['data']['totals']:
                    cart_vals = cart_rate['values']

        except:
            cart_vals = [0]
            pass
            # raise Exception('Permission Denied')

        try:
            revenue_per_user = service.reports().batchGet(
                body={
                    'reportRequests': [
                        {
                            'viewId': profile,
                            'dateRanges': [{'startDate': '100daysAgo', 'endDate': 'today'}],
                            'metrics': [{'expression': 'ga:revenuePerUser'}],
                            # "dimensions":[{"name":"ga:transactionId"}],
                        }]
                }
            ).execute()

            for crpu in revenue_per_user['reports']:
                for revenue in crpu['data']['totals']:
                    revenue_vals = revenue['values']

        except:
            revenue_vals = [0]
            pass
            # raise Exception('Permission Denied')

        try:
            traffic = service.reports().batchGet(
                body={
                    'reportRequests': [
                        {
                            'viewId': profile,
                            'dateRanges': [{'startDate': '100daysAgo', 'endDate': 'today'}],
                            'metrics': [{'expression': 'ga:organicSearches'}],
                            "dimensions": [{"name": "ga:month"}],
                        }]
                }
            ).execute()

            for current_montn_traffic in traffic['reports']:
                for dimension in current_montn_traffic['data']['rows']:
                    pass

                a = max(int(d) for d in dimension['dimensions'])
                for traffic in current_montn_traffic['data']['totals']:
                    traffic_vals = cart_rate['values']

            # Analytics matrics of Traffic This Month
            traffic_this_month = service.reports().batchGet(
                body={
                    'reportRequests': [
                        {
                            'viewId': profile,
                            'dateRanges': [{'startDate': '100daysAgo', 'endDate': 'today'}],
                            'metrics': [{'expression': 'ga:organicSearches'}],
                            "dimensions": [{"name": "ga:month"}],
                            "dimensionFilterClauses": [
                                {
                                    "filters": [
                                        {
                                            "dimensionName": "ga:month",
                                            "operator": "EXACT",
                                            "expressions": ["0%s" % a]
                                        }
                                    ]
                                }]
                        }
                    ]
                }
            ).execute()

            for cttm in traffic_this_month['reports']:
                for traffic in cttm['data']['totals']:
                    traffic_this_vals = traffic['values']

        except:
            traffic_this_vals = [0]
            pass
            # raise Exception('Permission Denied')

        try:
            # Analytics matrics of Traffic Last Month
            traffic_last_month = service.reports().batchGet(
                body={
                    'reportRequests': [
                        {
                            'viewId': profile,
                            'dateRanges': [{'startDate': '30daysAgo', 'endDate': 'today'}],
                            'metrics': [{'expression': 'ga:organicSearches'}],
                            # "dimensions":[{"name":"ga:month"}],
                        }
                    ]
                }
            ).execute()

            for ctlm in traffic_last_month['reports']:
                for traffic_last in ctlm['data']['totals']:
                    traffic_last_vals = traffic_last['values']

        except:
            traffic_last_vals = [0]
            pass
            # raise Exception('Permission Denied')

        try:
            # Analytics matrics of Revenue This Month
            revenue_this_month = service.reports().batchGet(
                body={
                    'reportRequests': [
                        {
                            'viewId': profile,
                            'dateRanges': [{'startDate': '30daysAgo', 'endDate': 'today'}],
                            'metrics': [{'expression': 'ga:transactionRevenue'}],
                            "dimensions": [{"name": "ga:month"}],
                        }]
                }
            ).execute()

            for crtm in revenue_this_month['reports']:
                for revenue in crtm['data']['totals']:
                    revenue_this_vals = revenue['values']

        except:
            raise Exception('Permission Denied')

        try:
            # Analytics matrics of Revenue Last Month
            revenue_last_month = service.reports().batchGet(
                body={
                    'reportRequests': [
                        {
                            'viewId': profile,
                            'dateRanges': [{'startDate': '60daysAgo', 'endDate': '30daysago'}],
                            'metrics': [{'expression': 'ga:transactionRevenue'}],
                            # "dimensions":[{"name":"ga:month"}],
                        }]
                }
            ).execute()

            for crlm in revenue_last_month['reports']:
                for revenue in crlm['data']['totals']:
                    revenue_last_vals = revenue['values']

        except:
            revenue_last_vals = [0]
            pass
            # raise Exception('Permission Denied')

        try:
            optimal_revenue = service.reports().batchGet(
                body={
                    'reportRequests': [
                        {
                            'viewId': profile,
                            'dateRanges': [{'startDate': '30daysAgo', 'endDate': 'today'}],
                            'metrics': [{'expression': 'ga:transactionRevenue'}],
                            # "dimensions":[{"name":"ga:month"}],
                        }]
                }
            ).execute()

            for optrev in optimal_revenue['reports']:
                for revenue in optrev['data']['totals']:
                    optimal_revenue_vals = revenue['values']

        except:
            optimal_revenue_vals = [0]
            pass
            # raise Exception('Permission Denied')

        Profile.objects.filter(user_id=profile1.user_id).update(
            current_conversion_rate=float(conversion_vals[0]),
            optimal_conversion_rate=float(conversion_vals[0]),
            current_bounce_rate=float(bounce_vals[0]),
            optimal_bounce_rate=float(bounce_vals[0]),
            current_average_order_value=float(order_value[0]),
            optimal_average_order_value=float(order_value[0]),
            current_shopping_cart_abandonment_rate=float(cart_vals[0]),
            optimal_shopping_cart_abandonment_rate=float(cart_vals[0]),
            traffic_last_month=float(traffic_last_vals[0]),
            traffic_this_month=float(traffic_this_vals[0]),
            optimal_traffic=float(revenue_this_vals[0]),
            revenue_last_month=float(revenue_last_vals[0]),
            revenue_this_month=float(conversion_vals[0]),
            optimal_revenue=float(optimal_revenue_vals[0]),
            current_revenue_per_user=float(revenue_vals[0]),
            optimal_revenue_per_user=float(revenue_vals[0]),
            access_token=access_token
        )
    return 1
예제 #15
0
from gmail.gmail_credentials import get_stored_credentials
from apiclient.discovery import build
from oauth2client.client import GoogleCredentials
import webbrowser
import json
import httplib2

credencialos = get_stored_credentials(1)
dicto = json.loads(credencialos)
credencialos = GoogleCredentials(dicto['access_token'], dicto['client_id'],
                                 dicto['client_secret'],
                                 dicto['refresh_token'], dicto['token_expiry'],
                                 dicto['token_uri'], dicto['user_agent'],
                                 dicto['revoke_uri'])
http = credencialos.authorize(httplib2.Http())
service = build(
    'people',
    'v1',
    http=http,
    discoveryServiceUrl='https://people.googleapis.com/$discovery/rest')
#results = service.people().connections().list(resourceName='people/me',pageSize=10,personFields='names,emailAddresses,interests').execute()
results = service.people().get(resourceName='people/me',
                               personFields='ageRanges,interests').execute()
connections = results.get('connections', [])
with open('data.txt', 'w') as outfile:
    json.dump(results, outfile)
print(connections)
for person in connections:
    names = person.get('names', [])
    if len(names) > 0:
        name = names[0].get('displayName')
예제 #16
0
def settings(request):
    account = request.POST.get('account', '')
    if account == '' and request.GET.get('code', False) == False:
        profile = Profile.objects.filter(user_id=request.user.id).first()
        # GET REFERRAL OBJECT OF THIS USER
        url = '%s://%s/i/%s' % (
            request.scheme, request.META['HTTP_HOST'],
            profile.profilelink.short
        ) if profile and profile.profilelink and profile.profilelink.short else ''
        profilerank = profile.profilerank if profile else None
        return render(request, 'settings/settings.html', {
            'profile': profile,
            'profilerank': profilerank,
            'url': url
        })

    elif account == '':
        if request.GET.get('code', False):
            flow = client.flow_from_clientsecrets(
                'client_secret_sb_webapp_GA.json',
                scope='https://www.googleapis.com/auth/analytics',
                redirect_uri="%s://%s%s" %
                (request.scheme, request.META['HTTP_HOST'], '/settings/'),
                prompt='select_account')

            google_authcode = request.GET['code']
            credentials = flow.step2_exchange(google_authcode)
            access_token1 = credentials.access_token
            refresh_token1 = credentials.refresh_token
            client_id = credentials.client_id
            client_secret = credentials.client_secret
            token_expiry = credentials.token_expiry
            token_uri = credentials.token_uri
            user_agent = credentials.user_agent
            revoke_uri = credentials.revoke_uri

            Profile.objects.filter(user_id=request.user.id).update(
                google_authcode=google_authcode,
                access_token=access_token1,
                refresh_token=refresh_token1,
                client_id=client_id,
                client_secret=client_secret,
                token_expiry=token_expiry,
                token_uri=token_uri,
                user_agent=user_agent,
                revoke_uri=revoke_uri)

            http_auth = credentials.authorize(httplib2.Http())

            service = build('analytics',
                            'v3',
                            http_auth,
                            cache_discovery=False)
            accounts = service.management().accounts().list().execute()

            if accounts.get('items'):
                account1 = []
                account2 = []
                for account in accounts.get('items'):
                    account1 = account1 + [account.get('name')]
                    account2 = account2 + [account.get('id')]
                zipped_data = zip(account1, account2)
                return render(request, 'settings/settings.html',
                              {'accounts': zipped_data})
            else:
                profile = Profile.objects.filter(
                    user_id=request.user.id).first()
                # GET REFERRAL OBJECT OF THIS USER
                url = '%s://%s/i/%s' % (
                    request.scheme, request.META['HTTP_HOST'],
                    profile.profilelink.short
                ) if profile and profile.profilelink and profile.profilelink.short else ''
                profilerank = profile.profilerank if profile else None
                message = "You don't have analytics account "
                return render(
                    request, 'settings/settings.html', {
                        'profile': profile,
                        'profilerank': profilerank,
                        'url': url,
                        'message': message
                    })

    elif account:
        profile = Profile.objects.filter(user_id=request.user.id).first()

        access_token = profile.access_token
        refresh_token = profile.refresh_token
        client_id = profile.client_id
        client_secret = profile.client_secret
        token_expiry = profile.token_expiry
        token_uri = profile.token_uri
        user_agent = profile.user_agent
        revoke_uri = profile.revoke_uri

        credentials = GoogleCredentials(access_token, client_id, client_secret,
                                        refresh_token, token_expiry, token_uri,
                                        'my-user-agent/1.0', revoke_uri)
        http_auth = credentials.authorize(httplib2.Http())
        service = build('analytics', 'v3', http_auth, cache_discovery=False)
        # Get a list of all the properties for the first account.

        profile1 = None
        properties = service.management().webproperties().list(
            accountId=account).execute()
        if properties.get('items'):
            # Get the first property id.
            property = properties.get('items')[0].get('id')
            # Get a list of all views (profiles) for the first property.
            profiles = service.management().profiles().list(
                accountId=account, webPropertyId=property).execute()
            if profiles.get('items'):
                # return the first view (profile) id.
                profile1 = profiles.get('items')[0].get('id')

        if profile1 == None:
            profile = Profile.objects.filter(user_id=request.user.id).first()
            # GET REFERRAL OBJECT OF THIS USER
            url = '%s://%s/i/%s' % (
                request.scheme, request.META['HTTP_HOST'],
                profile.profilelink.short
            ) if profile and profile.profilelink and profile.profilelink.short else ''
            profilerank = profile.profilerank if profile else None
            message = "Your account  don't have sufficient permission "
            return render(
                request, 'settings/settings.html', {
                    'profile': profile,
                    'profilerank': profilerank,
                    'url': url,
                    'message': message
                })

        service = build('analytics', 'v4', http_auth, cache_discovery=False)
        message = 'Not have permission of'
        try:
            conversion_rate = service.reports().batchGet(
                body={
                    'reportRequests': [{
                        'viewId':
                        profile1,
                        'dateRanges': [{
                            'startDate': '100daysAgo',
                            'endDate': 'today'
                        }],
                        'metrics': [{
                            'expression': 'ga:goalConversionRateAll'
                        }]
                    }]
                }).execute()

            for ccr in conversion_rate['reports']:
                for rate in ccr['data']['totals']:
                    conversion_vals = rate['values']
                    # Profile._meta.get_field('current_conversion_rate').default = float(vals[0])
                    # Profile._meta.get_field('optimal_conversion_rate').default = float(vals[0])
        except:
            # raise Exception('Permission Denied')
            conversion_vals = [0]
            message = message + ' Conversion Rate '
            pass
            # return sb_traceback(request)
        try:
            bounce_rate = service.reports().batchGet(
                body={
                    'reportRequests': [{
                        'viewId':
                        profile1,
                        'dateRanges': [{
                            'startDate': '100daysAgo',
                            'endDate': 'today'
                        }],
                        'metrics': [{
                            'expression': 'ga:bounceRate'
                        }],
                    }]
                }).execute()

            for cbr in bounce_rate['reports']:
                for rate in cbr['data']['totals']:
                    bounce_vals = rate['values']
                    # Profile._meta.get_field('current_bounce_rate').default = float(vals[0])
                    # Profile._meta.get_field('optimal_bounce_rate').default = float(vals[0])
        except:
            # print traceback.print_exc()
            # print e
            bounce_vals = [0]
            message = message + ' Bounce Rate '
            pass
            # return sb_traceback(request)
            # raise Exception('Permission Denied')

        try:
            metric_expression = {
                'expression': 'ga:transactionRevenue/ga:transactions',
                'formattingType': 'FLOAT'
            }

            avg_order_value = service.reports().batchGet(
                body={
                    'reportRequests': [{
                        'viewId':
                        profile1,
                        'dateRanges': [{
                            'startDate': '100daysAgo',
                            'endDate': 'today'
                        }],
                        'metrics': [metric_expression],
                    }]
                }).execute()

            for caov in avg_order_value['reports']:
                for order in caov['data']['totals']:
                    order_value = order['values']
                    # Profile._meta.get_field('current_average_order_value').default = float(value[0])
                    # Profile._meta.get_field('optimal_average_order_value').default = float(value[0])
        except:
            order_value = [0]
            message = message + ' Average Order Value '
            pass
            # return sb_traceback(request)
            # raise Exception('Permission Denied')

        try:
            cart_abandonment_rate = service.reports().batchGet(
                body={
                    'reportRequests': [{
                        'viewId':
                        profile1,
                        'dateRanges': [{
                            'startDate': '100daysAgo',
                            'endDate': 'today'
                        }],
                        'metrics': [{
                            'expression': 'ga:goalAbandonRateAll'
                        }],
                    }]
                }).execute()

            for ccar in cart_abandonment_rate['reports']:
                for cart_rate in ccar['data']['totals']:
                    cart_vals = cart_rate['values']
                    # Profile._meta.get_field('current_shopping_cart_abandonment_rate').default = float(cart_vals[0])
                    # Profile._meta.get_field('optimal_shopping_cart_abandonment_rate').default = float(cart_vals[0])
        except:
            cart_vals = [0]
            message = message + ' Cart Abandonment Rate '
            pass
            # return sb_traceback(request)
            # raise Exception('Permission Denied')

        try:
            revenue_per_user = service.reports().batchGet(
                body={
                    'reportRequests': [{
                        'viewId':
                        profile1,
                        'dateRanges': [{
                            'startDate': '100daysAgo',
                            'endDate': 'today'
                        }],
                        'metrics': [{
                            'expression': 'ga:revenuePerUser'
                        }],
                        # "dimensions":[{"name":"ga:transactionId"}],
                    }]
                }).execute()

            for crpu in revenue_per_user['reports']:
                for revenue in crpu['data']['totals']:
                    revenue_vals = revenue['values']
                    # Profile._meta.get_field('current_revenue_per_user').default = float(vals[0])
                    # Profile._meta.get_field('optimal_revenue_per_user').default = float(vals[0])
        except:
            revenue_vals = [0]
            message = message + ' Revenue Per User '
            pass
            # return sb_traceback(request)
            # raise Exception('Permission Denied')

        try:
            traffic = service.reports().batchGet(
                body={
                    'reportRequests': [{
                        'viewId':
                        profile1,
                        'dateRanges': [{
                            'startDate': '100daysAgo',
                            'endDate': 'today'
                        }],
                        'metrics': [{
                            'expression': 'ga:organicSearches'
                        }],
                        "dimensions": [{
                            "name": "ga:month"
                        }],
                    }]
                }).execute()

            for current_montn_traffic in traffic['reports']:
                for dimension in current_montn_traffic['data']['rows']:
                    pass

                a = max(int(d) for d in dimension['dimensions'])
                for traffic in current_montn_traffic['data']['totals']:
                    traffic_vals = cart_rate['values']
                    # Profile._meta.get_field('optimal_traffic').default = float(traffic_vals[0])

            # Analytics matrics of Traffic This Month
            traffic_this_month = service.reports().batchGet(
                body={
                    'reportRequests': [{
                        'viewId':
                        profile1,
                        'dateRanges': [{
                            'startDate': '100daysAgo',
                            'endDate': 'today'
                        }],
                        'metrics': [{
                            'expression': 'ga:organicSearches'
                        }],
                        "dimensions": [{
                            "name": "ga:month"
                        }],
                        "dimensionFilterClauses": [{
                            "filters": [{
                                "dimensionName": "ga:month",
                                "operator": "EXACT",
                                "expressions": ["0%s" % a]
                            }]
                        }]
                    }]
                }).execute()

            for cttm in traffic_this_month['reports']:
                for traffic in cttm['data']['totals']:
                    traffic_this_vals = traffic['values']
                    # Profile._meta.get_field('traffic_this_month').default = float(vals[0])
        except:
            traffic_this_vals = [0]
            message = message + ' Traffic This Month '
            pass
            # return sb_traceback(request)
            # raise Exception('Permission Denied')

        try:
            # Analytics matrics of Traffic Last Month
            traffic_last_month = service.reports().batchGet(
                body={
                    'reportRequests': [{
                        'viewId':
                        profile1,
                        'dateRanges': [{
                            'startDate': '30daysAgo',
                            'endDate': 'today'
                        }],
                        'metrics': [{
                            'expression': 'ga:organicSearches'
                        }],
                        # "dimensions":[{"name":"ga:month"}],
                    }]
                }).execute()

            for ctlm in traffic_last_month['reports']:
                for traffic_last in ctlm['data']['totals']:
                    traffic_last_vals = traffic_last['values']
                    # Profile._meta.get_field('traffic_last_month').default = float(last_vals[0])
        except:
            traffic_last_vals = [0]
            message = message + ' Traffic Last Month '
            pass
            # return sb_traceback(request)
            # raise Exception('Permission Denied')

        try:
            # Analytics matrics of Revenue This Month
            revenue_this_month = service.reports().batchGet(
                body={
                    'reportRequests': [{
                        'viewId':
                        profile1,
                        'dateRanges': [{
                            'startDate': '30daysAgo',
                            'endDate': 'today'
                        }],
                        'metrics': [{
                            'expression': 'ga:transactionRevenue'
                        }],
                        "dimensions": [{
                            "name": "ga:month"
                        }],
                    }]
                }).execute()

            for crtm in revenue_this_month['reports']:
                for revenue in crtm['data']['totals']:
                    revenue_this_vals = revenue['values']
                    # Profile._meta.get_field('revenue_this_month').default = float(vals[0])
        except:
            revenue_this_vals = [0]
            message = message + ' Revenue This Month '
            pass
            # return sb_traceback(request)
            # raise Exception('Permission Denied')

        try:
            # Analytics matrics of Revenue Last Month
            revenue_last_month = service.reports().batchGet(
                body={
                    'reportRequests': [{
                        'viewId':
                        profile1,
                        'dateRanges': [{
                            'startDate': '60daysAgo',
                            'endDate': '30daysago'
                        }],
                        'metrics': [{
                            'expression': 'ga:transactionRevenue'
                        }],
                        # "dimensions":[{"name":"ga:month"}],
                    }]
                }).execute()

            for crlm in revenue_last_month['reports']:
                for revenue in crlm['data']['totals']:
                    revenue_last_vals = revenue['values']
                    # Profile._meta.get_field('revenue_last_month').default = float(vals[0])
        except:
            revenue_last_vals = [0]
            message = message + ' Revenue Last Month '
            pass
            # return sb_traceback(request)
            # raise Exception('Permission Denied')

        try:
            optimal_revenue = service.reports().batchGet(
                body={
                    'reportRequests': [{
                        'viewId':
                        profile1,
                        'dateRanges': [{
                            'startDate': '30daysAgo',
                            'endDate': 'today'
                        }],
                        'metrics': [{
                            'expression': 'ga:transactionRevenue'
                        }],
                        # "dimensions":[{"name":"ga:month"}],
                    }]
                }).execute()

            for optrev in optimal_revenue['reports']:
                for revenue in optrev['data']['totals']:
                    optimal_revenue_vals = revenue['values']
                    # Profile._meta.get_field('optimal_revenue').default = float(vals[0])
        except:
            optimal_revenue_vals = [0]
            message = message + ' Optimal Revenue '
            pass
            # return sb_traceback(request)
            # raise Exception('Permission Denied')

        profile = Profile.objects.filter(user_id=request.user.id).first()
        # if not UserTaskHistory:
        #   guide = Guide.objects.get(pk=8)
        #   steps = Step.objects.filter(guide=guide.id).first()
        #   tasks = Task.objects.filter(step=steps).first()
        # else:
        #   pass
        user = User.objects.get(id=request.user.id)
        if not profile:
            pr = Profile.objects.create(
                current_conversion_rate=float(conversion_vals[0]),
                optimal_conversion_rate=float(conversion_vals[0]),
                current_bounce_rate=float(bounce_vals[0]),
                optimal_bounce_rate=float(bounce_vals[0]),
                current_average_order_value=float(order_value[0]),
                optimal_average_order_value=float(order_value[0]),
                current_shopping_cart_abandonment_rate=float(cart_vals[0]),
                optimal_shopping_cart_abandonment_rate=float(cart_vals[0]),
                traffic_last_month=float(traffic_last_vals[0]),
                traffic_this_month=float(traffic_this_vals[0]),
                optimal_traffic=float(revenue_this_vals[0]),
                revenue_last_month=float(revenue_last_vals[0]),
                revenue_this_month=float(conversion_vals[0]),
                optimal_revenue=float(optimal_revenue_vals[0]),
                current_revenue_per_user=float(revenue_vals[0]),
                optimal_revenue_per_user=float(revenue_vals[0]),
                current_user_name=request.user.username,
                user_id=user,
                connected=True,
                accountid=account,
            )
            pr.save()
        else:
            Profile.objects.filter(user_id=request.user.id).update(
                current_conversion_rate=float(conversion_vals[0]),
                optimal_conversion_rate=float(conversion_vals[0]),
                current_bounce_rate=float(bounce_vals[0]),
                optimal_bounce_rate=float(bounce_vals[0]),
                current_average_order_value=float(order_value[0]),
                optimal_average_order_value=float(order_value[0]),
                current_shopping_cart_abandonment_rate=float(cart_vals[0]),
                optimal_shopping_cart_abandonment_rate=float(cart_vals[0]),
                traffic_last_month=float(traffic_last_vals[0]),
                traffic_this_month=float(traffic_this_vals[0]),
                optimal_traffic=float(revenue_this_vals[0]),
                revenue_last_month=float(revenue_last_vals[0]),
                revenue_this_month=float(conversion_vals[0]),
                optimal_revenue=float(optimal_revenue_vals[0]),
                current_revenue_per_user=float(revenue_vals[0]),
                optimal_revenue_per_user=float(revenue_vals[0]),
                current_user_name=request.user.username,
                connected=True,
                accountid=account,
            )

        profile = Profile.objects.filter(user_id=request.user.id).first()

        # GET REFERRAL OBJECT OF THIS USER
        url = '%s://%s/i/%s' % (
            request.scheme, request.META['HTTP_HOST'],
            profile.profilelink.short
        ) if profile and profile.profilelink and profile.profilelink.short else ''

        profilerank = profile.profilerank if profile else None

        message = '' if message == 'Not have permission of' else message

        return render(
            request, 'settings/settings.html', {
                'profile': profile,
                'profilerank': profilerank,
                'url': url,
                'message': message
            })
예제 #17
0
def analitica(land, success, start, end):

    credentials = GoogleCredentials(
        access_token, client_id, client_secret, refresh_token, 3920,
        'https://accounts.google.com/o/oauth2/token', 'test')

    http = httplib2.Http()
    http = credentials.authorize(http)
    service = build(
        'analytics',
        'v4',
        http=http,
        cache_discovery=False,
        discoveryServiceUrl=
        'https://analyticsreporting.googleapis.com/$discovery/rest?version=v4')
    #POMENYAT
    start = start
    stop = end
    if datetime.date(int(stop[:4]), int(stop[5:7]), int(
            stop[8:10])) > datetime.datetime.now().date():
        stop = str(datetime.datetime.now().date())
    else:
        pass
    print('!!!!!!')
    print(stop)
    df = return_ga_data(
        start_date=start,
        end_date=stop,
        view_id='129196190',
        metrics=[
            {
                "expression": "ga:uniquePageviews"
            },
        ],
        dimensions=[
            {
                'name': 'ga:pagePath'
            },
            {
                'name': 'ga:sourceMedium'
            },
        ],
        service=service,
    )
    #POMENYAT
    urltoLanding = uelgenerator(land)
    urltoSuccess = uelgenerator(success)
    data_namestraf = list(
        itertools.chain.from_iterable(
            df[df['ga:pagePath'] == urltoLanding][['ga:sourceMedium']].values))
    data_valuestraf = list(
        itertools.chain.from_iterable(df[df['ga:pagePath'] == urltoLanding][[
            'ga:uniquePageviews'
        ]].values))
    print(data_namestraf)
    slovartraf = dict(zip(data_namestraf, data_valuestraf))
    data = googleapi(slovartraf, df, urltoSuccess)
    data_names = list(data.keys())
    data_values = list(data.values())

    d = {
        'Источник':
        data_names,
        'Количество': ['0' for x in data_values],
        'Сила': ['—' for x in data_values],
        'Трафикфакт': [x[0] for x in data_values],
        'Конверсия': [
            str(int(x[1] / x[0] * 100)) + '%' if x[0] > 0 else x[0]
            for x in data_values
        ],
        'Регистрациифакт': [x[1] for x in data_values],
        'Бюджетплан': ['—' for x in data_values],
        'Бюджетфакт': ['—' for x in data_values]
    }
    vuz = 0
    set_data_valuestraf = set(data_namestraf)
    for i in set_data_valuestraf:
        if 'vuz-' in i or 'vuz_' in i:
            vuz += 1
    d['Количество'][5] = vuz

    return d
예제 #18
0
def colvodneyforday(start, stop, urlland):
    credentials = GoogleCredentials(
        access_token, client_id, client_secret, refresh_token, 3920,
        'https://accounts.google.com/o/oauth2/token', 'test')

    http = httplib2.Http()
    http = credentials.authorize(http)
    service = build(
        'analytics',
        'v4',
        http=http,
        cache_discovery=False,
        discoveryServiceUrl=
        'https://analyticsreporting.googleapis.com/$discovery/rest?version=v4')
    urltoLanding = uelgenerator(urlland)
    datedelta = []
    d1 = date(int(start[:4]), int(start[5:7]), int(start[8:10]))
    d2 = date(int(stop[:4]), int(stop[5:7]), int(stop[8:10]))

    # this will give you a list containing all of the dates
    dd = [d1 + timedelta(days=x) for x in range((d2 - d1).days + 1)]
    for x in dd:
        datedelta.append(str(x))
    df = return_ga_data(
        start_date=start,
        end_date=stop,
        view_id='129196190',
        metrics=[
            {
                "expression": "ga:uniquePageviews"
            },
        ],
        dimensions=[{
            'name': 'ga:pagePath'
        }, {
            'name': 'ga:sourceMedium'
        }, {
            'name': 'ga:date'
        }],
        service=service,
    )

    data_namestraf = list(
        itertools.chain.from_iterable(
            df[df['ga:pagePath'] == urltoLanding].values))

    qqq = []
    # print(data_namestraf)
    for j in datedelta:
        datee = []
        for i in range(len(data_namestraf)):
            if data_namestraf[i] == j[:4] + j[5:7] + j[8:10]:
                datee.append(data_namestraf[i:i + 4])
        qqq.append(datee)
    kolvodneyWEB = 0
    kolvodneyKONTEKST = 0
    kolvodnetTARGETING = 0
    kolvodneyORGANICA = 0
    k = []
    for i in range(len(qqq)):
        k.append(list(itertools.chain.from_iterable(qqq[i])))
    for i in range(len(k)):
        for j in range(len(k[i])):
            if 'cl-site' in str(k[i][j]) or 'Сl-site' in str(
                    k[i][j]) or 'cl_site' in str(k[i][j]) or 'Сl_site' in str(
                        k[i][j]):
                kolvodneyWEB += 1
                break
            else:
                pass

    for i in range(len(k)):
        for j in range(len(k[i])):
            if 'google / cpc' in str(k[i][j]) or 'youtube / instream' in str(
                    k[i][j]) or 'yandex / cpc' in str(k[i][j]):
                kolvodneyKONTEKST += 1
                break
            else:
                pass
    for i in range(len(k)):
        for j in range(len(k[i])):
            if 'vk / target' in str(k[i][j]) or 'vk / targetpost' in str(
                    k[i][j]) or 'vk / target-story' in str(
                        k[i][j]) or 'insta / target' in str(
                            k[i][j]) or 'insta / targetpost' in str(
                                k[i][j]) or 'insta / target-story' in str(
                                    k[i][j]) or 'fb / target' in str(
                                        k[i][j]) or 'fb / targetpost' in str(
                                            k[i][j]):
                kolvodnetTARGETING += 1
                break
            else:
                pass
    for i in range(len(k)):
        for j in range(len(k[i])):
            if '(direct) / (none)' in str(k[i][j]) or 'referral' in str(
                    k[i][j]) or 'organic' in str(k[i][j]):
                kolvodneyORGANICA += 1
                break
            else:
                pass
    #PICS
    unikalkarry = []
    digestarray = []
    telegaarray = []

    # lif 'digest' in i or 'Digest' in i:
    #  elif 'tg /' in i or 'Tg /' in i:
    for i in range(len(k)):
        for j in range(len(k[i])):
            if 'generalbase' in str(k[i][j]) or 'mailchimp' in str(k[i][j]):
                if platform == "darwin":
                    unikalkarry.append(int(k[i][j - 2]))
                else:
                    unikalkarry.append(int(k[i][j - 2]))
            elif 'digest' in str(k[i][j]) or 'Digest' in str(k[i][j]):
                if platform == "darwin":
                    digestarray.append(int(k[i][j - 2]))
                else:
                    digestarray.append(int(k[i][j - 2]))
            elif 'tg /' in str(k[i][j]) or 'Tg /' in str(k[i][j]):
                if platform == "darwin":
                    telegaarray.append(int(k[i][j - 2]))
                else:
                    telegaarray.append(int(k[i][j - 2]))
            else:
                pass
    #  if 'generalbase' in ii or 'mailchimp' in ii:

    max_value = 0
    for n in unikalkarry:
        if n > max_value:
            max_value = n

    if max_value == 1:
        lenUNIKALKA = sg.find_peaks_cwt(unikalkarry,
                                        np.arange(1, max_value + 1),
                                        max_distances=np.arange(
                                            1, max_value + 1))
    elif max_value == 0:
        lenUNIKALKA = []
    else:
        lenUNIKALKA = sg.find_peaks_cwt(unikalkarry,
                                        np.arange(1, max_value),
                                        max_distances=np.arange(1, max_value))

    max_value = 0
    for n in digestarray:
        if n > max_value:
            max_value = n
    if max_value == 1:
        lenDIGEST = sg.find_peaks_cwt(digestarray,
                                      np.arange(1, max_value + 1),
                                      max_distances=np.arange(
                                          1, max_value + 1))
    elif max_value == 0:
        lenDIGEST = []
    else:
        lenDIGEST = sg.find_peaks_cwt(digestarray,
                                      np.arange(1, max_value),
                                      max_distances=np.arange(1, max_value))

    max_value = 0
    for n in telegaarray:
        if n > max_value:
            max_value = n
    if max_value == 1:
        lenTELEGA = sg.find_peaks_cwt(telegaarray,
                                      np.arange(1, max_value + 1),
                                      max_distances=np.arange(
                                          1, max_value + 1))
    elif max_value == 0:
        lenTELEGA = []
    else:
        lenTELEGA = sg.find_peaks_cwt(telegaarray,
                                      np.arange(1, max_value),
                                      max_distances=np.arange(1, max_value))

    return [
        kolvodnetTARGETING, kolvodneyWEB, kolvodneyKONTEKST, kolvodneyORGANICA,
        len(lenUNIKALKA),
        len(lenDIGEST),
        len(lenTELEGA)
    ]
예제 #19
0
# import libraries
from credentials import client_id, client_secret, redirect_uri, access_code, access_token, refresh_token
from oauth2client.client import OAuth2WebServerFlow, GoogleCredentials
import httplib2
from googleapiclient.discovery import build

# create connection based on project credentials
flow = OAuth2WebServerFlow(client_id=client_id,
                           client_secret=client_secret,
                           scope='https://www.googleapis.com/auth/analytics',
                           redirect_uri=redirect_uri)

# capture different states of connection
if access_code == '':
    # first run prints oauth URL
    auth_uri = flow.step1_get_authorize_url()
    print (auth_uri)
elif access_token == '' and refresh_token == '':
    # second run returns access and refresh token
    credentials = flow.step2_exchange(access_code)
    print(credentials.access_token)
    print(credentials.refresh_token)
else:
    # third and future run connect through access token an refresh token
    credentials = GoogleCredentials(access_token, client_id, client_secret, refresh_token, 3920, 'https://accounts.google.com/o/oauth2/token', 'test')
    http = httplib2.Http()
    http = credentials.authorize(http)
    service = build('analytics', 'v4', http=http, discoveryServiceUrl=('https://analyticsreporting.googleapis.com/$discovery/rest'))
    #print(service)
예제 #20
0
import httplib2
from googleapiclient.errors import HttpError
from oauth2client.client import GoogleCredentials

from reddityoutubehaikus import RedditYoutubeHaikus
from youtubehelper import YoutubeHelper

creds = GoogleCredentials(environ['YOUTUBE_ACCESS_TOKEN'],
                          environ['YOUTUBE_CLIENT_ID'],
                          environ['YOUTUBE_CLIENT_SECRET'],
                          environ['YOUTUBE_REFRESH_TOKEN'], None,
                          "https://www.googleapis.com/oauth2/v4/token",
                          'Youtube Haikus')

http = creds.authorize(httplib2.Http())
creds.refresh(http)
youtube_helper = YoutubeHelper(creds)

time_periods = [{
    'interval': 'week',
    'title': 'Top of the week'
}, {
    'interval': 'month',
    'title': 'Top of the month'
}, {
    'interval': 'year',
    'title': 'Top of the year'
}, {
    'interval': 'all',
    'title': 'Top ever'
예제 #21
0
class APIClient:
    def __init__(self, config):
        self.credentials = GoogleCredentials(
            None,
            client_id=config.get('client_id'),
            client_secret=config.get('client_secret'),
            refresh_token=config.get('refresh_token'),
            token_expiry=None,
            token_uri="https://accounts.google.com/o/oauth2/token",
            user_agent=config.get('user_agent'))

        self.refresh_credentials()

    def refresh_credentials(self):
        http = self.credentials.authorize(Http())
        self.credentials.refresh(http)

    def get_report(self, profile_id, report_id):
        service = build('dfareporting', 'v3.0', credentials=self.credentials)

        logger.info("Running report {} for profile {}...".format(
            report_id, profile_id))

        run = service.reports().run(profileId=profile_id,
                                    reportId=report_id).execute()

        file_id = run.get('id')

        running = True

        while running:
            f = service.reports().files().get(profileId=profile_id,
                                              reportId=report_id,
                                              fileId=file_id).execute()

            if f.get('status') == 'PROCESSING':
                logger.info("> Report is still processing, retry in 5s.")
                sleep(5)
            else:
                running = False

        logger.info("Completed with status '{}'.".format(f.get('status')))

        file_value = service.reports().files().get_media(
            profileId=profile_id, reportId=report_id,
            fileId=file_id).execute().decode('utf-8')

        # the data comes in with some garbage on the first few, and
        # last, lines. remove it
        split = file_value.split('\n')
        header_lines = split[0:9]
        data_lines = split[9:]
        data_lines = data_lines[:-2]

        generated_time = datetime.strptime(
            # 2/16/18 1:42 PM
            header_lines[2].split(',')[1],
            '%m/%d/%y %I:%M %p')

        # 2/1/18 - 2/16/18
        time_range = header_lines[5].split(',')[1]

        output = []
        for index, line in enumerate(data_lines):
            if index == 0:
                output.append('{},{},{}'.format(line, 'generated_time',
                                                'time_range'))
            else:
                output.append('{},{},{}'.format(line, generated_time,
                                                time_range))

        return (
            file_id,
            '\n'.join(output).encode('utf-8'),
        )
예제 #22
0
파일: main.py 프로젝트: ottersome/dashTerm
    def checkgoogle(self):
        if self.googlecredencialos !=0:
            #kinda redundant but just in case
            self.win1.clear()
            self.win1.border(0)
            #we get the credentials thet were previously stored in gmail/credentials.json
            filedir = os.path.join(self.FILE_DIR,'gmail/client_credentials.json')
            clientstuff = json.load(open(filedir))
            dicto = json.loads(self.googlecredencialos)

            #change the title to suit the email address
            self.win1.addnstr(0,1,"[Gmail Inbox _ "+dicto['id_token']['email']+']',self.win1.getmaxyx()[1]-2)
            #we get credenetials according to google
            credentialos = GoogleCredentials(dicto['access_token'],dicto['client_id'],dicto['client_secret'],dicto['refresh_token'],dicto['token_expiry'],dicto['token_uri'],dicto['user_agent'],dicto['revoke_uri'])
            #we retrieve the emails using httplib2 and google apis, but this only gives us a list of api and misc information, not the content itself therefore on the following for loop we get every email for every id 
            http = httplib2.Http()
            http = credentialos.authorize(http)
            service = build('gmail','v1',http=http)
            mails = GmailMessages.ListMessages(service,dicto['id_token']['email'],'in:inbox is:unread -category:(promotions OR social)')
            
            #i and stringoList work as always
            i=0
            stringoList = []
            
            #this simply tests if there are no emails or there is some issue with the mail dictoinary 
            '''try:
                logging.log("Mail Snippet : "+mails[0]['snipppet'])
            except Exception as e:
                self.win1.addstr(2,2,"No new Mails were found")
                self.win1.refresh()
                return'''

            for item in mails:
                stringo = ''
                #get individual email
                indmessage = GmailMessages.getIndividualMessage(service,dicto['id_token']['email'],item['id'])

                #this whole bunch is just getting the headers and appending them to stringo 
                headerswanted = ('From','Subject','Date')
                headersdicto = indmessage['payload']['headers']
                for header in headersdicto:
                    for wanted_header in headerswanted:
                       if wanted_header == header['name']:
                            if stringo == '':
                                stringo = header['name']+' : '+header['value']
                            else:
                                stringo = stringo +'\n'+header['name']+' : '+header['value']
                
                #we finally append the email snippper(summary) to stringo 
                stringo = stringo + '\n'+indmessage['snippet']
                stringoList.append({"url":"https://mail.google.com/mail/u/0/#inbox/"+item['id'],"stringo":stringo})
                #print message
                #for thread safety
                if self.DISPLAYON == False:
                    return()
                maxes = self.win1.getmaxyx()
                amount_of_entries = int(maxes[0] / 6)
                inipos = (i*6+2,3)
                
                #save Stringo In Cache
                self.saveInCache(1,stringoList)
                if i > amount_of_entries:
                    return
                self.addMessage(self.win1,stringo,(inipos[0]+5,maxes[1]-2),inipos,i)
                i = i+1