Example #1
0
 def __init__(self):        
     # Connect to Latitude
     storage = Storage('latitude.dat')
     credentials = storage.get()
     if credentials is None or credentials.invalid == True:
         auth_discovery = build("latitude", "v1").auth_discovery()
         flow = FlowThreeLegged(auth_discovery,
             # You MUST have a consumer key and secret tied to a
             # registered domain to use the latitude API.
             #
             # https://www.google.com/accounts/ManageDomains
             consumer_key='maleadt.be',
             consumer_secret='Yh0qdTQ-pHGQiyguyINr64WK',
             user_agent='efficient-latitude/0.1',
             domain='maleadt.be',
             scope='https://www.googleapis.com/auth/latitude',
             xoauth_displayname='Efficient Latitude',
             location='all',
             granularity='best'
             )
         # Work around https://code.google.com/p/google-api-python-client/issues/detail?id=34
         while len(sys.argv) > 1:
             sys.argv.pop()
         credentials = run(flow, storage)
         if credentials is None or credentials.invalid == True:
             raise Exception("Invalid Latitude credentials")
     http = httplib2.Http()
     http = credentials.authorize(http)
     self.service = build("latitude", "v1", http=http)
Example #2
0
def oauth_check(a_details=None):
    """
        Établie une connexion à l'API de Google et vérifie les 
        informations entrées en argument.
    """
    api_auth = None
    base_url = "https://accounts.google.com/o/oauth2/auth"
    client_id = "921839184200-o06bafl8996ghfl4s7bvvpmuhkfphm8d.\
    apps.googleusercontent.com"
    redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
    response = "code"
    scope = "https://www.googleapis.com/auth/youtube.upload \
    https://www.googleapis.com/auth/youtube"
    secret_file_path = "auth/client_secrets.json"

    oauth_flow = flow_from_clientsecrets(secret_file_path, 
                                         scope, 
                                         message="Fichier client_secrets.json \
                                         manquant ou invalide!")
    storage = Storage("auth/oauth2.json")
    credentials = storage.get()
    if (credentials is None) or credentials.invalid:
        try:
            credentials = run_flow(oauth_flow, storage, a_details)
            api_auth = build("youtube", "v3", http=credentials.authorize\
                                              (httplib2.Http()))
        except:
            raise SystemExit("L'application doit avoir accès aux APIs.")
    return build("youtube", "v3", http=credentials.authorize(httplib2.Http()))
Example #3
0
    def auth(self, oauth2json = None, oauth2storage = None, scope = None):
        if oauth2json is not None:
            self.oauth2json = oauth2json
        if oauth2storage is not None:
            self.oauth2storage = oauth2storage
        if scope is not None:
            self.scope = scope
        if self.oauth2json is None:
            raise ValueError('Attribute oauth2json needs to be defined')
        if self.oauth2storage is None:
            raise ValueError('Attribute oauth2storage needs to be defined')
        if self.scope is None:
            raise ValueError('Attribute scope needs to be defined')

        logging.debug('Authenticating to Google, using json(%s) and store(%s)' % (self.oauth2json, self.oauth2storage))
        self.store = Storage(self.oauth2storage)
        self.creds = self.store.get()
        if self.creds is None or self.creds.invalid:
            flow = oauth2client.client.flow_from_clientsecrets(self.oauth2json, self.scope)
            self.creds = oauth2client.tools.run_flow(flow, self.store, parser.parse_args())
            self.store.put(self.creds)
        if 'spreadsheets' in self.scope.lower():
            logging.debug('Authenticating as sheets')
            discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?version=v4')
            self.service = discovery.build('sheets', 'v4', http = self.creds.authorize(httplib2.Http()),
                discoveryServiceUrl = discoveryUrl)
        else:
            logging.debug('Authenticating as drive')
            self.service = discovery.build('drive', 'v3', http = self.creds.authorize(httplib2.Http()))
        logging.debug('Authentication to Google is done')
Example #4
0
 def schedule(self, pairs, no_pair=None, meeting_dt=None):
     """
         This schedule function is built around Googles Api. Its goal is to schedule
         google calendar events for each set of pairs. To do this it uses the following
         api resources:
             Google Calendar: https://developers.google.com/google-apps/calendar/?hl=en
             Google Directory: https://developers.google.com/admin-sdk/directory/
         It also makes use of Google Service Accounts: https://developers.google.com/identity/protocols/OAuth2ServiceAccount
         If meeting_dt is not passed, assume that the meetings should be schedule a week from today at 10 a.m.
     """
     if meeting_dt is None:
         now = datetime.datetime.now()
         one_week_from_now = now + datetime.timedelta(7)
         meeting_start = datetime.datetime(one_week_from_now.year, one_week_from_now.month, one_week_from_now.day, 10, 30, 0).isoformat()
         meeting_end = datetime.datetime(one_week_from_now.year, one_week_from_now.month, one_week_from_now.day, 11, 0, 0).isoformat()
     else:
         dt_start = datetime.datetime(meeting_dt.year, meeting_dt.month, meeting_dt.day, meeting_dt.hour, meeting_dt.minute, meeting_dt.second)
         dt_end = datetime.datetime(meeting_dt.year, meeting_dt.month, meeting_dt.day, meeting_dt.hour, meeting_dt.minute, meeting_dt.second)
         meeting_start = dt_start.isoformat()
         meeting_end = dt_end.isoformat()
     credentials = self.get_credentials()
     http = credentials.authorize(httplib2.Http())
     calendar_access = discovery.build('calendar', 'v3', http=http)
     directory_access = discovery.build('admin', 'directory_v1', http=http)
     mail_access = discovery.build('gmail', 'v1', http=http)
     for pair in pairs:
         self.create_meeting(pair, meeting_start, meeting_end, calendar_access, directory_access)
     if no_pair:
         self.send_no_meeting_email(no_pair, mail_access, directory_access)
Example #5
0
File: service.py Project: gddb/gddb
def createDriveService(credential_path=None):
  """
  Create drive service based on service account credentialed from api_key in secret_files/config.json.  Utilizes 'https://www.googleapis.com/auth/drive' scope.
  """
  
  # Determine if running in local appengine SDK dev_server environment.  
  # AppAssertionCredentials does not load in this context.  
  # Therefore, service must be mimicked locally using an installed client oauth flow.
  if os.environ.get('SERVER_SOFTWARE') is None:
    os.environ['SERVER_SOFTWARE'] = 'Development'
    
  if 'Development' in os.environ.get('SERVER_SOFTWARE'):
    # Acquire credentials stored from running `python secret.py` from the commandline.
    if credential_path is None:
      credential_path = secret_cred.CREDENTIALS
    storage = Storage(credential_path)
    credentials = storage.get()
    http = httplib2.Http()
    http = credentials.authorize(http)

    return build('drive', 'v2', http=http)

    
  else:
    service_account_email = secret_cred.SERVICE_ACCOUNT
    api_key = secret_cred.API_KEY
    credentials = AppAssertionCredentials(scope=OAUTH_SCOPE)
    http = httplib2.Http()
    http = credentials.authorize(http)

    return build('drive', 'v2', http=http, developerKey=api_key)
Example #6
0
def callback():
    code = request.params.get('code')
    response.set_cookie('code', code)
    credentials = flow.step2_exchange(code)

    http = credentials.authorize(httplib2.Http())
    service = build('oauth2', 'v1', http=http)
    user = service.userinfo()
    info = user.get().execute()
    email = info['email']

    rd = redis_endpoint()
    rd.set(code, email)

    if rd.get(email) is None:

        service = build('calendar', 'v3', http=http)
        cals = service.calendarList()
        info = cals.list().execute()

        cal_info = [dict(name=d['summary'], busy=True, cid=d['id']) 
                for d in info['items']]

        uinfo = {'code':code, 
                'cals':cal_info,
                'cred':credentials}
        pred_set(rd, email, uinfo)

    if email.endswith(ending):
        email = email[:-len(ending)]
    return redirect('/' + email)
Example #7
0
    def init(self):
    #----------------------------------------------------------------------
        '''
        any processing which needs to be done at the beginning of any method
        '''
        # must authorize if not already authorized
        if debug: print 'RunningRoutesTable.init()'

        # load credentials for us and for self.files instance
        credentials = get_credentials(APP_CRED_FOLDER)
        if not credentials or not get_email():
            if debug: print "url_for('authorize') = {}".format(url_for('authorize'))
            return redirect(url_for('authorize'))

        # set up form mapping
        self.dte = DataTablesEditor(self.dbmapping, self.formmapping)

        # set up services
        self.drive = discovery.build(DRIVE_SERVICE, DRIVE_VERSION, credentials=credentials)
        self.sheets = discovery.build(SHEETS_SERVICE, SHEETS_VERSION, credentials=credentials)

        # get the header, and all the values
        # TODO: maybe init() should just get the header, and open() should get all the values
        fid = self.app.config['RR_DB_SHEET_ID']
        result = self.sheets.spreadsheets().values().get(spreadsheetId=fid, range='routes').execute()
        self.values = iter(result.get('values', []))
        self.header = self.values.next()

        # indicate no redirect
        return None
Example #8
0
def youtube_setup(course_id, force_load=False):
    global api_youtube
    global api_youtube_analytics

    youtube_scopes = ["https://www.googleapis.com/auth/youtube.readonly", "https://www.googleapis.com/auth/yt-analytics.readonly"]
    youtube_servicename = "youtube"
    youtube_version = "v3"
    youtube_analytics_servicename = "youtubeAnalytics"
    youtube_analytics_version = "v1"

    flow = OAuth2WebServerFlow(client_id=settings.YOUTUBE_CLIENT_ID,
                           client_secret=settings.YOUTUBE_CLIENT_SECRET,
                           scope=" ".join(youtube_scopes))

    storage = Storage("cache/youtube_"+course_id+"_oauth2.json")
    youtube_credentials = storage.get()

    print "AAA"
    print youtube_credentials
    print "BBB"

    if youtube_credentials is None or youtube_credentials.invalid:
        if not force_load:
            return False
        youtube_credentials = run(flow, storage)

    http = youtube_credentials.authorize(httplib2.Http())
    api_youtube = build(youtube_servicename, youtube_version, http=http)
    api_youtube_analytics = build(youtube_analytics_servicename, youtube_analytics_version, http=http)
    return True
def drive():
    # Initialize client object to use Google api.
    # You need client_secrets.json downloaded and stored
    # in the current working directory 
    flow = ece568helper.get_client_object(_addr, _port, SCOPES)
    auth_uri = flow.step1_get_authorize_url()
    if (not(request.query.code)):
        redirect(auth_uri)
    auth_code = request.query.code
    credentials = flow.step2_exchange(auth_code)
    ece568helper.output_helper('credentials', credentials)
    http_auth = credentials.authorize(httplib2.Http())

    profile_service = build('plus', 'v1', http=http_auth)
    people_resource = profile_service.people()
    people_document = people_resource.get(userId='me').execute()
    ece568helper.output_helper('profile', people_document)

    drive_service = build('drive', 'v2', http=http_auth)

    filename = 'profile.out'
    media_body = MediaFileUpload(filename, mimetype='text/plain', resumable=True)
    body = {
        'title': 'profile_out',
        'description': 'Profile information excluding email address',
        'mimeType': 'text/plain'
    }

    try:
        file = drive_service.files().insert(body=body, media_body=media_body).execute()
        return template('drive', result='success')
    except errors.HttpError, error:
        return template('drive', result='fail')
Example #10
0
    def auth(self, state=None, code=None):
        if code:
            credentials = self.flow.step2_exchange(code)
            http = credentials.authorize(httplib2.Http())
            users_service = build('oauth2', 'v2', http=http)
            user_document = users_service.userinfo().get().execute()

            c = cherrypy.thread_data.db.cursor()
            c.execute('insert or replace into users values("%s", "%s", \'%s\')' % (credentials.token_response['id_token']['id'], user_document['email'], credentials.to_json()))
            cherrypy.thread_data.db.commit()

            cherrypy.session.regenerate()
            cherrypy.session['userid'] = credentials.token_response['id_token']['id']
            cherrypy.session['credentials'] = credentials

            owner = self.get_user_data(cherrypy.config['google.calendar.ownerid'], True)
            if not 'error' in owner and cherrypy.config['google.calendar.ownerid'] != credentials.token_response['id_token']['id']:
                http = owner['credentials'].authorize(httplib2.Http())
                calendar_service = build('calendar', 'v3', http=http)
                calendar_service.acl().insert(calendarId = cherrypy.config['google.calendar.id'], body = {
                    'role': 'writer',
                    'scope': {
                        'type': 'user',
                        'value': user_document['email']
                        }
                    }).execute()

            if state != 'None' and state != None:
                self.handle_event(state, credentials.token_response['id_token']['id']) 

            raise cherrypy.HTTPRedirect('/')

        return 'error'
Example #11
0
def google_login():
    code = request.query.get('code','')
    #print "The code is: "+code
    credentials= flow.step2_exchange(code)
    gauthtoken = credentials.id_token['sub']
    print "The user token is: " + gauthtoken
    http = httplib2.Http()
    http = credentials.authorize(http)
    # Get user email
    users_service = build('oauth2', 'v2', http=http)
    user_document = users_service.userinfo().get().execute()
    # Get user name
    users_service = build('plus', 'v1', http=http)
    profile = users_service.people().get(userId='me').execute()
    user_name = profile['displayName']
    user_image = profile['image']['url']
    # Header file
    f = open('header.html','r')
    searchengine_header = f.read()
    f.close()
    # navbar div
    navbar = "<div id=\"navibar\"><ul id=\"profile\"><li><a href=\"#\"><img src=\""
    navbar += str(user_image)
    navbar += "\" /></a></li><li id=\"profileText\">"
    navbar += str(user_name)
    navbar += "<li id=\"profileText\">CSC326 Group 1</li><li><a href=\"/signout\"><div id=\"profileLink\">Sign Out</div></a></li></ul></div>"
    f = open('index.html','r')
    searchengine_content = f.read()
    f.close()
    # completed front page loading.
    front_page = (searchengine_header + navbar + searchengine_content)
    return front_page
Example #12
0
def main(file_name):
    """

    :param folder: only 1-level folder!
    :param file_name:
    :return:
    """
    # got creadentials first
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())

    # find the file before writing
    drive_service = discovery.build('drive', 'v3', http=http)

    file_id = get_file(drive_service, file_name)
    if file_id is None:
        return

    sheets_service = discovery.build('sheets', 'v4', http=http)

    results = sheets_service.spreadsheets().values().get(spreadsheetId=file_id, range='A:Z').execute()
    values = results['values']
    # write to stdout
    for arr in values:
        print('|', end='')
        for a in arr:
            print(a,end='|')
        print('')
Example #13
0
def require_google_login(request):
    storage = Storage(CredentialsModel, 'id', request.session.session_key, 'credential')
    credential = storage.get()
    if credential is None or credential.invalid == True:
        flow = OAuth2WebServerFlow(client_id=settings.GOOGLE_OAUTH_KEY,
                                   client_secret=settings.GOOGLE_OAUTH_SECRET,
                                   scope='https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email',
                                   user_agent='plus-django-sample/1.0',
                                   state=request.GET['next'])

        authorize_url = flow.step1_get_authorize_url(settings.GOOGLE_RETURN_URL)

        f = FlowModel(id=request.session.session_key, flow=flow)
        f.save()

        return redirect(authorize_url)

    http = httplib2.Http()
    plus = build('plus', 'v1', http=http)
    credential.authorize(http)
    name_data = plus.people().get(userId='me').execute()

    name = name_data["name"]["givenName"]
    last_name = name_data["name"]["familyName"]

    plus = build('oauth2', 'v2', http=http)
    credential.authorize(http)
    email_data = plus.userinfo().get().execute()
    email = email_data["email"]

    return _login_user(request, email, name, last_name, email)
Example #14
0
def expand_url(bot, trigger):
    regex = re.compile('^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[\/?#]\S*)?$', re.I)
    if regex.match(trigger.group(2)):
        http = httplib2.Http(cache="~/.sopelcache")
        service = build('urlshortener', 'v1', developerKey=bot.config.google.apikey)
        try:
            http = httplib2.Http(cache="~/.sopelcache")
            service = build('urlshortener', 'v1', developerKey=bot.config.google.apikey)
            global response
            response = service.url().get(shortUrl = trigger.group(2), fields='id, longUrl').execute()
            bot.reply(response['id'] + ' is ' + response['longUrl'] + '.')
        except HttpError:
            if response['error']['errors'][0]['reason'] == 'invalid':
                body = {'longUrl': trigger.group(2)}
                response2 = service.url().insert(body = body, fields='id, longUrl').execute()
                bot.reply('This is an invalid short URL, so I shortened it instead: ' + response2['longUrl'] + ' is ' + response2['id'] + '.')
            elif response['error']['errors'][0]['reason'] == 'notFound':
                bot.reply('Invalid short URL (1).')
            else:
                bot.reply('Unknown error: ' + response['error']['message'])
        except KeyError:
            bot.reply('Invalid short URL (2).')
        except:
            bot.reply('Unknown error.')
    else:
        bot.reply('Invalid URL entered (' + trigger.group(2) + '). Please note that the protocol needs to be in the entered URL.')
Example #15
0
def authorized():
    code = request.args['code']
    if code:
        # exchange the authorization code for user credentials
        flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, scope, redirect_uri=request.base_url)
        try:
            credentials = flow.step2_exchange(code)
            session['credentials'] = credentials.to_json()
        except Exception as e:
            print "Unable to get an access token because ", e.message
            flash(e.message)
            return redirect(url_for('index'))
        # store these credentials for the current user in the session
        # This stores them in a cookie, which is insecure. Update this
        # with something better if you deploy to production land
        http = httplib2.Http()
        http = credentials.authorize(http)
        user = build('oauth2', 'v2', http=http).userinfo().get().execute(http=http)
        taskservice = build('tasks', 'v1').tasks().list(tasklist='@default').execute(http=http)
        session['tasklists'] = taskservice.get('items', [])
        adminservice = build('admin', 'directory_v1', http=http).members().list(groupKey='*****@*****.**', roles='admin').execute(http=http)
        adminlist = adminservice.get('members', [])
        for member in adminlist:
            admins = []
            admins.append(member['email'])
        if user['email'] not in admins:
            session['privilege'] = 'user'
            session['name'] = user['name']
        else:
            session['privilege'] = 'admin'
            session['name'] = user['name']
        print session['privilege']

        return redirect(url_for('index'))
def search():
	if use_google_login:
		code = request.query.get("code", "")
		if(code == ""):
			redirect('/')

		flow = OAuth2WebServerFlow(client_id='470991490159.apps.googleusercontent.com', client_secret = 'Zleg_TsPX6CXU06z3XURewt8', scope = scope, redirect_uri = redirect_uri)
		credentials = flow.step2_exchange(code)
		token = credentials.id_token['sub']

		#retrieve user data with the access token
		http = httplib2.Http()
		http = credentials.authorize(http)

		#get user email
		users_service = build('oauth2', 'v2', http=http)
		user_document = users_service.userinfo().get().execute()
		user_email = user_document['email']

		#get username
		users_service = build('plus', 'v1', http=http)
		profile = users_service.people().get(userId='me').execute()
		user_name = profile['displayName']
		user_image = profile['image']['url']

	return disableBack + logoutButton + LogoString + "<br><br>" + searchHTML
Example #17
0
def main():
  
  storage = Storage('latitude.dat')
  credentials = storage.get()
  if credentials is None or credentials.invalid == True:
    auth_discovery = build("latitude", "v1").auth_discovery()
    flow = FlowThreeLegged(auth_discovery,
                           # You MUST have a consumer key and secret tied to a
                           # registered domain to use the latitude API.
                           #
                           # https://www.google.com/accounts/ManageDomains
                           consumer_key='svetsen.homeip.net',
                           consumer_secret='lzhGCgXu33_hj33YN4LYvL98',
                           user_agent='google-api-client-python-latitude/1.0',
                           domain='svetsen.homeip.net',
                           scope='https://www.googleapis.com/auth/latitude',
                           xoauth_displayname='Google API Latitude Example',
                           location='history',
                           granularity='city'
                           )

    credentials = run(flow, storage)

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

  service = build("latitude", "v1", http=http)

  body = {
      "data": {
          "kind": "latitude#location",
            "timestampMs": "%d" % (time.time()-12*60*60)*1000
          }
      }
  print service.location().list().execute()
Example #18
0
def main(folder, file_name):
    """

    :param folder: only 1-level folder!
    :param file_name:
    :return:
    """
    # got creadentials first
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())

    # find the file before writing
    drive_service = discovery.build('drive', 'v3', http=http)
    folder_id = create_folders(drive_service, folder)
    file_id = get_or_create_file(drive_service, file_name, folder_id, is_folder=False)


    sheets_service = discovery.build('sheets', 'v4', http=http)  

    # read stdin and write to sheets
    rows = 0
    while True:
        line = sys.stdin.readline()
        if len(line) == 0:
            break
        body = dict()
        body['values'] = []
        datas = []
        cells = line.split('|||')
        datas.append(cells)
        body['values'] = datas
        results = sheets_service.spreadsheets().values().append(spreadsheetId=file_id, range='A1', body=body, valueInputOption="USER_ENTERED").execute()
        rows = rows + 1
    print('{} updated with rows {}'.format(file_id, rows))
Example #19
0
    def _youtube(cls, authenticate=False):
        while True:
            try:
                if not authenticate:
                    return build(cls.YOUTUBE_API_SERVICE_NAME, cls.YOUTUBE_API_VERSION, developerKey=cls.DEVELOPER_KEY)

                storage = Storage('mpris-youtube', os.getlogin())
                credentials = storage.get()

                if credentials is None:
                    flow = OAuth2WebServerFlow(
                            client_id=cls.CLIENT_ID,
                            client_secret=cls.CLIENT_SECRET,
                            scope=cls.AUTH_SCOPE
                            #redirect_uri='urn:ietf:wg:oauth:2.0:oob'
                            )
                    credentials = run(flow, storage)

                http = httplib2.Http()
                credentials.authorize(http)
                return build(
                        cls.YOUTUBE_API_SERVICE_NAME,
                        cls.YOUTUBE_API_VERSION,
                        http=http)

            except Exception as e:
                print e
                time.sleep(3)
Example #20
0
  def open_connection(self, parameters):
    """ Connects to Google Compute Engine with the given credentials.

    Args:
      parameters: A dict that contains all the parameters necessary to
        authenticate this user with Google Compute Engine. We assume that the
        user has already authorized this account for use with GCE.
    Returns:
      An apiclient.discovery.Resource that is a connection valid for requests
      to Google Compute Engine for the given user, and a Credentials object that
      can be used to sign requests performed with that connection.
    Raises:
      AppScaleException if the user wants to abort.
    """
    is_autoscale_agent = parameters.get(self.PARAM_AUTOSCALE_AGENT, False)

    # Determine paths to credential files
    if is_autoscale_agent:
      client_secrets_path = self.CLIENT_SECRETS_LOCATION
      oauth2_storage_path = self.OAUTH2_STORAGE_LOCATION
    else:
      # Determine client secrets path
      client_secrets_path = LocalState.get_client_secrets_location(
        parameters[self.PARAM_KEYNAME])
      if not os.path.exists(client_secrets_path):
        client_secrets_path = parameters.get(self.PARAM_SECRETS, '')
      # Determine oauth2 storage
      oauth2_storage_path = parameters.get(self.PARAM_STORAGE)
      if not oauth2_storage_path or not os.path.exists(oauth2_storage_path):
        oauth2_storage_path = LocalState.get_oauth2_storage_location(
          parameters[self.PARAM_KEYNAME])

    if os.path.exists(client_secrets_path):
      # Attempt to perform authorization using Service account
      secrets_type = GCEAgent.get_secrets_type(client_secrets_path)
      if secrets_type == CredentialTypes.SERVICE:
        scopes = [GCPScopes.COMPUTE]
        credentials = ServiceAccountCredentials.from_json_keyfile_name(
          client_secrets_path, scopes=scopes)
        return discovery.build('compute', self.API_VERSION), credentials

    # Perform authorization using OAuth2 storage
    storage = oauth2client.file.Storage(oauth2_storage_path)
    credentials = storage.get()

    if not credentials or credentials.invalid:
      # If we couldn't get valid credentials from OAuth2 storage
      if not os.path.exists(client_secrets_path):
        raise AgentConfigurationException(
          'Couldn\'t find client secrets file at {}'.format(client_secrets_path)
        )
      # Run OAuth2 flow to get credentials
      flow = oauth2client.client.flow_from_clientsecrets(
        client_secrets_path, scope=self.GCE_SCOPE)
      flags = oauth2client.tools.argparser.parse_args(args=[])
      credentials = oauth2client.tools.run_flow(flow, storage, flags)

    # Build the service
    return discovery.build('compute', self.API_VERSION), credentials
def main():
    sender = '*****@*****.**'
    to     = ', '.join(['*****@*****.**', '*****@*****.**'])

    previous_files = []
    with open('upheaval_files.pkl', 'r') as f:
        previous_files = pickle.load(f)

    while True:
        print('Running at ' + str(datetime.now()))

        # Get the files currently in the Drive folder
        query_str = "'0B4BZrYisXMkDMzdpNGo1OW1CTmM' in parents"
        try:
            credentials = get_credentials()
            http = credentials.authorize(httplib2.Http())
            gmail_service = discovery.build('gmail', 'v1', http=http)
            drive_service = discovery.build('drive', 'v3', http=http)

            results = drive_service.files().list(q=query_str, pageSize=100).execute()
            items = results.get('files', [])
        except Exception as e:
            print('Error getting new files: ' + str(e))
            time.sleep(UPDATE_PERIOD * 60)
            continue

        if not items:
            print('No files found.')
        else:
            print('Files found:')
            for item in items:
                print('{}'.format(item['name']))

        current_files = set([item['name'] for item in items])

        # Compare the files currently in the Drive folder with the previous ones
        diff_files = current_files - set(previous_files)

        if diff_files:
            subject = str(len(diff_files)) + ' new UPHEAVAL recordings'

            files_str = ''
            for f in diff_files:
                files_str = files_str + str(f) + '\n'

            message_text = 'New files in the upheaval folder:\n' + files_str
            msg = CreateMessage(sender, to, subject, message_text)
            print('sending message: {}'.format(files_str))

            msg = SendMessage(gmail_service, 'me', msg)
            print('sent message')

            # Save the new file list
            with open('upheaval_files.pkl', 'w') as f:
                pickle.dump(current_files, f)

            previous_files = current_files

        time.sleep(UPDATE_PERIOD * 60)
Example #22
0
def _BuildDiscoveryService(scopes, api, version, url):
  """Helper functions to build discovery services for sheets and drive."""
  creds = service_account.ServiceAccountCredentials.from_json_keyfile_name(
      _PATH_TO_JSON_KEYFILE, scopes)
  http = creds.authorize(httplib2.Http())
  if url:
    return discovery.build(api, version, http=http, discoveryServiceUrl=url)
  return discovery.build(api, version, http=http)
Example #23
0
def get_service_objects(http_auth):
    """Builds google API service objects"""

    people_service = build('people', 'v1', http_auth)
    calendar_service = build('calendar', 'v3', http_auth)
    event_service = build('calendar', 'v3', http_auth)

    return people_service, calendar_service, event_service
Example #24
0
 def setUpClass(cls):
     cls.credentials = cls.create_credentials()
     http = cls.credentials.authorize(httplib2.Http())
     cls.credentials.refresh(http)
     cls.service = discovery.build('sheets', 'v4', http=http)
     cls.drive_service = discovery.build('drive', 'v3', http=http)
     # Hide STDOUT output generated by snippets.
     cls.stdout = sys.stdout
     sys.stdout = None
Example #25
0
def login_post_route():
    if flask.g.user:
        return JSONResponse({'email': flask.g.user.email})

    if 'credentials' not in flask.session:
        json_data = flask.request.get_json(True)
        auth_code = json_data['code']
        if not auth_code:
            return flask.redirect('client/login.html')

        constructor_kwargs = {
                'redirect_uri': sheet_config['redirectURI'],
                'auth_uri': "https://accounts.google.com/o/oauth2/auth",
                'token_uri': "https://accounts.google.com/o/oauth2/token",
            }
        flow = client.OAuth2WebServerFlow(
            sheet_config['clientID'], sheet_config['clientSecret'],
            'https://www.googleapis.com/auth/groups https://www.googleapis.com/auth/plus.me', **constructor_kwargs)

        credentials = flow.step2_exchange(auth_code)
        flask.session['credentials'] = credentials.to_json()
    else:
        credentials = client.OAuth2Credentials.from_json(flask.session['credentials'])
    http = credentials.authorize(httplib2.Http())

    plus_service = build("plus", "v1", http=http)
    user = plus_service.people().get(userId='me').execute(http=http)
    for i in range(0,len(user['emails'])):
            if user['emails'][i].get('type') == 'account':
                email = user['emails'][i].get('value')

    drive_service = build('script', 'v1', http=http)
    if sheet_config['isLoginBasedOnGroup'] == "true":
        request = {"function": "checkGroupMembership", "parameters": [sheet_config['loginGroupEmail'],email]}
        response = drive_service.scripts().run(body=request, scriptId=sheet_config['appsScriptID']).execute()
        if response.get('error') is not None or not response['response'].get('result'):
            flask.session.pop('credentials', None)
            flask.abort(401)

    request = {"function": "checkGroupMembership", "parameters": [sheet_config['editorGroupEmail'],email]}
    response = drive_service.scripts().run(body=request, scriptId=sheet_config['appsScriptID']).execute()
    if response.get('error') is not None:
        editor_enabled = 'false'
    else:
        editor_enabled = response['response'].get('result')

    state = models.get_state_model(user['id'])
    state.name = user['displayName']
    state.email = email
    state.provider = 'google'
    state.put()
    # Add the user to the current session
    image_url = user['image'].get('url')
    flask.session['user'] = dict(key_id=str(state.key.id()),id=user['id'],name=user['displayName'],email=email,provider='google',editor_enabled=editor_enabled,image_url=image_url)

    return JSONResponse({'email': email})
Example #26
0
def build_services(user):
    storage = Storage(CredentialsModel, 'id', user, 'credential')
    credential = storage.get()
    if credential and not credential.invalid:
        http = httplib2.Http()
        http = credential.authorize(http)
        credential.refresh(http)
        service_people = build('people', 'v1', http=http)
        service_mail = build('gmail', 'v1', http=http)
        return {'mail' : service_mail, 'people' : service_people}
Example #27
0
 def __init__(self, *args, **kwargs):
     self.credentials = gce.AppAssertionCredentials(scope=[
         'https://www.googleapis.com/auth/devstorage.full_control',
         'https://www.googleapis.com/auth/devstorage.read_only',
         'https://www.googleapis.com/auth/devstorage.read_write'
     ])
     self.http = self.credentials.authorize(httplib2.Http())
     self.bq = build('bigquery', 'v2', http=self.http)
     self.gcs = build('storage', 'v1', http=self.http)
     super(BqTableLoadTask, self).__init__(*args, **kwargs)
Example #28
0
 def create_credentials():
     scopes = ['https://www.googleapis.com/auth/admin.directory.resource.calendar',
               'https://www.googleapis.com/auth/calendar']
     client_secret_dict = ast.literal_eval(os.environ['MR_CLIENT_SECRET_JSON'])
     credentials = ServiceAccountCredentials.from_json_keyfile_dict(
         client_secret_dict, scopes=scopes).create_delegated(os.environ['MR_DELEGATED_ACCOUNT'])
     http = credentials.authorize(Http())
     directory = build('admin', 'directory_v1', http=http)
     calendar = build('calendar', 'v3', http=http)
     return directory, calendar
Example #29
0
def drive():
    # Initialize client object to use Google api.
    # You need client_secrets.json downloaded and stored
    # in the current working directory 
    flow = ece568helper.get_client_object(_addr, _port, SCOPES)

    if 'access_denied' in request.query_string:
        # user denied access
        return template('drive', result='fail')
    
    if 'code' not in request.query_string:
        # redirect user to Google's OAuth 2.0 server inside this if
        # to avoid endlessly repeating
        auth_uri = flow.step1_get_authorize_url()
        return redirect(auth_uri)

    # handle OAuth 2.0 server response (error or auth code)
    # FormsDict has virtual attributes to access any keyword
    auth_code = request.query.code
    # exchange authorization code for access token
    credentials = flow.step2_exchange(auth_code)
    ece568helper.output_helper('credentials', credentials)

    # apply access token to an Http object
    http_auth = credentials.authorize(httplib2.Http())

    # build service object
    plus_service = build('plus', 'v1', http=http_auth)

    # obtain basic profile info of authenticated user
    people_resource = plus_service.people()
    people_document = people_resource.get(userId='me').execute()
    ece568helper.output_helper('profile', people_document)

    # upload profile file to Google Drive
    media_body = MediaFileUpload(filename='profile.out', 
                                 mimetype='text/plain',
                                 resumable=True)
    body = {
        'title': 'profile.out',
        'description': 'Profile information excluding email address',
        'mimeType': 'text/plain'
    }

    # might want to first handle the request then redirect to another URL that
    # doesn't include response params (like auth code) as per Google documentation
    try:
        drive_service = build('drive', 'v2', http=http_auth)
        drive_service.files().insert(
            body=body,
            media_body=media_body).execute()

        return template('drive', result='success')
    except errors.HttpError, error:
        return template('drive', result='fail: '+str(error)) 
Example #30
0
    def _set_services(self):
    #----------------------------------------------------------------------
        if (debug): print 'RunningRoutesFiles._set_services()'

        if not self.datafolderid:
            credentials = get_credentials(APP_CRED_FOLDER)
            self.drive = discovery.build(DRIVE_SERVICE, DRIVE_VERSION, credentials=credentials)
            self.sheets = discovery.build(SHEETS_SERVICE, SHEETS_VERSION, credentials=credentials)
            fid = self.app.config['RR_DB_SHEET_ID']
            datafolder = self.sheets.spreadsheets().values().get(spreadsheetId=fid, range='datafolder').execute()
            self.datafolderid = datafolder['values'][0][0]
Example #31
0
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials',settings.SITE.verbose_name)
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'people.googleapis.com-python-quickstart.json')
    # credential_path = CLIENT_SECRET_FILE

    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        # flags = argparse.ArgumentParser(description='This is a PyMOTW sample program').parse_args()
        if flags:
            credentials = tools.run_flow(flow, store,flags)
        else:  # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials


credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('people', 'v1', http=http)
Example #32
0
def get_events():
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)

    today = datetime.datetime.now().date().isoformat(
    ) + 'T05:00:00.00000Z'  # 'Z' indicates UTC time
    tomorrow = (datetime.datetime.now().date() +
                datetime.timedelta(days=1)).isoformat() + 'T05:00:00.00000Z'

    # Get School Events
    school_events_response = service.events().list(
        calendarId='*****@*****.**',
        timeMin=today,
        timeMax=tomorrow,
        singleEvents=True,
        orderBy='startTime').execute()
    school_events = school_events_response.get('items', [])

    # Get Personal Events
    personal_events_response = service.events().list(
        calendarId='*****@*****.**',
        timeMin=today,
        timeMax=tomorrow,
        singleEvents=True,
        orderBy='startTime').execute()
    personal_events = personal_events_response.get('items', [])

    # Get OrgSync Events
    orgsync_events_response = service.events().list(
        calendarId=
        '*****@*****.**',
        timeMin=today,
        timeMax=tomorrow,
        singleEvents=True,
        orderBy='startTime').execute()
    orgsync_events = orgsync_events_response.get('items', [])

    # Get Classes
    classes_response = service.events().list(
        calendarId=
        '*****@*****.**',
        timeMin=today,
        timeMax=tomorrow,
        singleEvents=True,
        orderBy='startTime').execute()
    classes_events = classes_response.get('items', [])

    # Get SG Events
    sg_events_response = service.events().list(
        calendarId=
        '*****@*****.**',
        timeMin=today,
        timeMax=tomorrow,
        singleEvents=True,
        orderBy='startTime').execute()
    sg_events = sg_events_response.get('items', [])

    # Get Facebook Events
    fb_events_response = service.events().list(
        calendarId=
        '*****@*****.**',
        timeMin=today,
        timeMax=tomorrow,
        singleEvents=True,
        orderBy='startTime').execute()
    fb_events = fb_events_response.get('items', [])

    all_events = school_events + personal_events + orgsync_events + classes_events + sg_events + fb_events
    todays_events = [e for e in all_events if "dateTime" in e["start"]]
    return sorted(todays_events, key=lambda e: e['start']['dateTime'])
Example #33
0
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
import sys

SCOPES = (
    'https://www.googleapis.com/auth/spreadsheets.readonly',
    'https://www.googleapis.com/auth/presentations',
)
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
HTTP = creds.authorize(Http())
SHEETS = discovery.build('sheets', 'v4', http=HTTP)
SLIDES = discovery.build('slides', 'v1', http=HTTP)

print('** Fetch Sheets data')
sheetID = '10FsJHEfMaxOuZZTI1DU2Q17gr57yRSUQxJ88C7ZXMSY'  # use your own!

doc = SHEETS.spreadsheets().get(spreadsheetId=sheetID).execute()
print(doc)

for data in doc["sheets"]:
    print(data)

sheets = doc.get('sheets', '')
for sheet in sheets:
    title = sheet["properties"]["title"]
    if not title.startswith("_"):
Example #34
0
def remove_share(calendarId, email):
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)

    service.acl().delete(calendarId=calendarId, ruleId="user:"+email).execute()
Example #35
0
import os
from apiclient.discovery import build  # pip install google-api-python-client

# 環境変数からAPIキーを取得する
YOUTUBE_API_KEY = os.environ['YOUTUBE_API_KEY']

youtube = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)

search_response = youtube.search().list(
    part='snippet',
    q='pubg',
    type='video',
).execute()

for item in search_response['items']:
    # 動画のタイトルを表示する
    print(item['snippet']['title'])
Example #36
0
args = parser.parse_args()

# --- # --- # --- # --- # --- # --- # --- # --- # --- # --- # --- # --- # --- #

sServiceAccountName = str(args.service_account).strip()
sAccessToken = str(args.access_token).strip()

print("[+] sServiceAccountName: " + str(sServiceAccountName) + "")
print("[+] sAccessToken: " + str(sAccessToken) + "")

# --- # --- # --- # --- # --- # --- # --- # --- # --- # --- # --- # --- # --- #

sEnter = raw_input("Press the [Enter] key to continue... ")

credentials = google.oauth2.credentials.Credentials(sAccessToken)
service = build(serviceName='iamcredentials',
                version='v1',
                credentials=credentials)

dBody = {
    'scope': [
        'https://www.googleapis.com/auth/iam',
        'https://www.googleapis.com/auth/cloud-platform'
    ]
}

sName = 'projects/-/serviceAccounts/' + sServiceAccountName
res = service.projects().serviceAccounts().generateAccessToken(
    name=sName, body=dBody).execute()

print(json.dumps(res, indent=4))
 def get_ae_conn(self):
     """Returns: a App Engine service object."""
     credentials = GoogleCredentials.get_application_default()
     return build('appengine', 'v1', credentials=credentials)
Example #38
0
import pystache
from apiclient import discovery
from google.oauth2 import service_account

COAL_PER_TRAIN = 1600

base_dir = os.path.dirname(os.path.realpath(__file__))
secret_file = os.path.join(base_dir, 'auth.json')

scopes = ['https://www.googleapis.com/auth/spreadsheets']
sheet_id = '1U22HX4tFcHx5xe9potpsItoVeOq7sVpfwferiqbkpHs'

credentials = service_account.Credentials.from_service_account_file(
    secret_file, scopes=scopes)
service = discovery.build('sheets', 'v4', credentials=credentials)

result = service.spreadsheets().values().get(spreadsheetId=sheet_id,
                                             range='Services').execute()
rows = result.get('values', [])

services = [dict(zip(rows[0], row)) for row in rows[1:]]

service_ids = set()
relevant_services = []
for service in services:
    if service['Origin'] != 'North Blyth Gbrf' or service[
            'Destination'] != 'West Burton Ps (Gbrf)':
        continue

    if service['Arrival'] == '':
Example #39
0
import gspread
import time

# Google Sheets
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
from httplib2 import Http

scope = [
    'https://spreadsheets.google.com/feeds',
    'https://www.googleapis.com/auth/drive'
]
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    secrets.google_json_file, scope)
gc = gspread.authorize(credentials)
service = build('sheets', 'v4', http=credentials.authorize(Http()))

wb = gc.open_by_key(secrets.google_sheet_id)


def get_damage(reports, encounters):
    damage = []
    for report in reports:
        for encounter in encounters:
            r_json = get_encounter(report, 'damage-done', encounter)
            # totalTime = total combat time in milliseconds
            combat_time = r_json['totalTime'] / 1000
            for player in r_json['entries']:
                # DPS: player.total / (totalTime / 1000)
                dps = player['total'] / combat_time
                new_row = [
Example #40
0
    logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))

    # If the Credentials don't exist or are invalid run through the native client
    # flow. The Storage object will ensure that if successful the good
    # Credentials will get written back to a file.
    storage = Storage(FLAGS.credentials_filename)
    credentials = storage.get()
    if credentials is None or credentials.invalid:
        credentials = run(FLOW, storage)

    # Create an httplib2.Http object to handle our HTTP requests and authorize it
    # with our good Credentials.
    http = httplib2.Http()
    http = credentials.authorize(http)

    service = build('gan', 'v1beta1', http=http)

    advertisers = service.advertisers()

    # Filter out all params that aren't set.
    for key in FLAGS:
        if key in API_FLAGS and FLAGS[key].value != None:
            params[key] = FLAGS[key].value

    # Retrieve the relevant advertisers.
    stdout = {}
    html = {}
    try:
        if FLAGS.request_type == "GET":
            get_call = advertisers.get(**params)
Example #41
0
def build_service(credentials=None):
    if credentials is None:
        credentials = get_credentials()
    http_auth = credentials.authorize(
        httplib2.Http(disable_ssl_certificate_validation=True))
    return build('drive', 'v2', http=http_auth)
Example #42
0
    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
                                   message=MISSING_CLIENT_SECRETS_MESSAGE,
                                   scope=YOUTUBE_READ_WRITE_SCOPE)

    # Save the oAuth settings in a config file locally
    storage = Storage("%s-oauth2.json" % sys.argv[0])
    credentials = storage.get()

    # If oAuth fails
    if credentials is None or credentials.invalid:
        flags = argparser.parse_args()
        credentials = run_flow(flow, storage, flags)

    # Create the YouTube object
    youtube = build(YOUTUBE_API_SERVICE_NAME,
                    YOUTUBE_API_VERSION,
                    http=credentials.authorize(httplib2.Http()))

    # Load the users configuration (eg. playlists)
    json_data2 = open(script_path + '/client_config.json')
    CLIENT_DATA = json.load(json_data2)
    json_data2.close()

    # Load this users playlists
    # This code tests whether a playlist already exists
    playlists = youtube.playlists().list(part="snippet",
                                         maxResults="50",
                                         mine="true",
                                         fields="items,kind").execute()

    # LOOP config playlists
Example #43
0
def youtube_search(Max_Result, apiKey, CateID, DBIP, DBPort, DBID, DBPW,
                   DBName):
    youtube = build(YOUTUBE_API_SERVICE_NAME,
                    YOUTUBE_API_VERSION,
                    developerKey=apiKey)

    # Call the search.list method to retrieve results matching the specified
    # query term.
    videos_response = youtube.videos().list(part="snippet, statistics, player",
                                            chart='mostPopular',
                                            maxResults=Max_Result,
                                            regionCode='KR',
                                            videoCategoryId=CateID).execute()

    videos = []
    maria_controller = MariaController.MController(DBIP, DBPort, DBID, DBPW,
                                                   DBName)
    NowDate = datetime.date.today()
    NowDateTime = datetime.datetime.today()

    for search_result in videos_response.get("items", []):
        videos.append(search_result["id"])
        videos.append(search_result["snippet"]["channelId"])
        videos.append(search_result["snippet"]["publishedAt"])
        videos.append(search_result["snippet"]["title"])
        videos.append(search_result["snippet"]["description"])
        videos.append(search_result["snippet"]["channelTitle"])
        videos.append(search_result["snippet"]["thumbnails"]["maxres"]["url"])
        videos.append(search_result["statistics"]["viewCount"])
        try:
            videos.append(search_result["statistics"]["likeCount"])
        except:
            videos.append(-1)
        try:
            videos.append(search_result["statistics"]["dislikeCount"])
        except:
            videos.append(-1)
        try:
            videos.append(search_result["statistics"]["commentCount"])
        except:
            videos.append(-1)
        videos.append("https://www.youtube.com/watch?v=" + search_result["id"])

        qry = 'select * from videodata where VideoID = "{}" and ChannelID = "{}" and InsertDT = "{}"'.format(
            videos[0], videos[1], NowDate)
        sResult = maria_controller.select_query(qry)
        if len(sResult) == 0:
            qry = 'insert into videodata values ("{}","{}","{}",{},"{}","{}","{}","{}","{}",{},{},{},{},{},"{}","{}", "{}")'.format(
                videos[0],  #VideoID
                videos[1],  #ChannelID
                NowDate,  #InsertDT        
                CateID,  #CategoryId    
                videos[2],  #PublishedAT
                videos[3].replace(
                    '"',
                    '""',
                ),  #Title
                videos[4].replace(
                    '"',
                    '""',
                ),  #Description
                videos[5],  #ChannelTitle
                videos[6],  #Thumbnails
                videos[7],  #ViewCount
                videos[7],  #ZeroViewCount
                0,  #DailyViewCount    
                videos[8],  #LikeCount
                videos[9],  #DislikeCount 
                videos[10],  #CommentCount
                videos[11],  #URL     
                NowDateTime)  #UpdateDT
        else:
            qry = 'update videodata set Title="{}", Description="{}" ,ChannelTitle="{}",Thumbnails="{}",ViewCount={}, DailyViewCount={}, LikeCount={}, DislikeCount={}, CommentCount={}  where VideoID="{}" and ChannelID="{}" and InsertDT="{}" and CategoryId="{}"'.format(
                videos[3].replace(
                    '"',
                    '""',
                ),  #Title
                videos[4].replace(
                    '"',
                    '""',
                ),  #Description
                videos[5],  #ChannelTitle
                videos[6],  #Thumbnails
                videos[7],  #ViewCount
                int(videos[7]) - int(sResult[0][9]),  #DailyViewCount
                videos[8],  #LikeCount
                videos[9],  #DislikeCount
                videos[10],  #CommentCount    
                sResult[0][0],  #VideoID
                sResult[0][1],  #ChannelID
                sResult[0][2],  #InsetDT
                CateID  #CategoryId
            )
            # print(qry)              //for debug
        maria_controller.execute_query(qry)
        # print(videos[3])          //for debug
        videos = []
Example #44
0
                  type="int")
parser.add_option("--sort", dest="sort", help="Sort order", default="-views")
(options, args) = parser.parse_args()

flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
                               message=MISSING_CLIENT_SECRETS_MESSAGE,
                               scope=" ".join(YOUTUBE_SCOPES))

storage = Storage("%s-oauth2.json" % sys.argv[0])
credentials = storage.get()

if credentials is None or credentials.invalid:
    credentials = run(flow, storage)

http = credentials.authorize(httplib2.Http())
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, http=http)
youtube_analytics = build(YOUTUBE_ANALYTICS_API_SERVICE_NAME,
                          YOUTUBE_ANALYTICS_API_VERSION,
                          http=http)

channels_response = youtube.channels().list(mine="", part="id").execute()

for channel in channels_response.get("items", []):
    channel_id = channel["id"]

    analytics_response = youtube_analytics.reports().query(
        ids="channel==%s" % channel_id,
        metrics=options.metrics,
        dimensions=options.dimensions,
        start_date=options.start_date,
        end_date=options.end_date,
Example #45
0
def create_pubsub_client(credentials):
    """Build the pubsub client."""
    http = httplib2.Http()
    credentials.authorize(http)
    return discovery.build('pubsub', 'v1', http=http)
 def get_svc_conn(self):
     """Returns: a Services Management service object."""
     credentials = GoogleCredentials.get_application_default()
     return build('servicemanagement', 'v1', credentials=credentials)
from oauth2client import file, client, tools

# Setup the Slides API
IMG_FILE = 'google-slides.png'  # use your own!
TMPLFILE = 'title slide template'  # use your own!
SCOPES = (
    'https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/presentations',
)
#SCOPES = 'https://www.googleapis.com/auth/presentations.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
slides_service = build('slides', 'v1', http=creds.authorize(Http()))
drive_service = build('drive', 'v3', http=creds.authorize(Http()))

# Call the Slides API
PRESENTATION_ID = '1AnsN_4VnuWEo-a7bbRAL-NTys5-XnU3gXuPQJjMedOk'
presentation = slides_service.presentations().get(
    presentationId=PRESENTATION_ID).execute()
slides = presentation.get('slides')

print('The presentation contains {} slides:'.format(len(slides)))
for i, slide in enumerate(slides):
    print('- Slide #{} contains {} elements.'.format(
        i + 1, len(slide.get('pageElements'))))

print('** Copying slide **')
body = {'name': 'This is a COPY'}
Example #48
0
    def run(self):
        with open('/home/saeidtheblind/proj/tele-gmail/bot_token.txt',
                  'r') as f:
            TOKEN = f.read().strip()
        bot = telepot.Bot(TOKEN)
        while 1:
            users = User.query.all()
            for u in users:
                db.session.refresh(u)
                logging.info('checking user %s', u.email)
                if not u.chat_id:
                    logging.info("user %s doesn't have chat_id")
                    continue
                try:
                    credentials = client.OAuth2Credentials.from_json(
                        u.credentials)
                    http = credentials.authorize(httplib2.Http())
                    if credentials.access_token_expired:
                        logging.info(
                            "credentials for %s has expired, refreshing",
                            u.email)
                        credentials.refresh(http)
                        u.credentials = credentials.to_json()
                        db.session.commit()
                        logging.info("successful refresh")
                    service = discovery.build('gmail', 'v1', http=http)
                    #msgs = ListMessagesWithLabels(service, u.email, maxResults=1)
                    msgs = ListMessagesMatchingQuery(service, u.email,
                                                     'after: ' + u.previous)
                    if len(msgs) == 0:
                        logging.info("no new messages for %s", u.email)
                        continue
                    logging.info('found %d new messages', len(msgs))
                    pre_time = int(u.previous)
                    for m in msgs[::-1]:
                        message = GetMessage(service, u.email, m['id'])
                        msg_time = int(message['internalDate']) / 1000
                        if msg_time <= pre_time:
                            continue
                        u.previous = msg_time
                        mail = Mail()
                        mail.snippet = message['snippet']
                        mail.internal_time = datetime.datetime.utcfromtimestamp(
                            msg_time)
                        for x in message['payload']['headers']:
                            if x['name'] == 'Subject':
                                mail.subject = x['value']
                            #~ elif x['name'] == 'Date':
                            #~ mail.raw_date = x['value']
                            #~ print 'raw_date', x['value']
                            #~ mail.date = datetime_from_string_date(mail.raw_date)
                            #~ print 'date', mail.date
                            elif x['name'] == 'From':
                                mail.from_ = x['value']
                        bot.sendMessage(u.chat_id, createMessageFromMail(mail))
                        db.session.commit()
                except client.HttpAccessTokenRefreshError:
                    logging.info("access for %s has been revoked", u.email)
                    bot.sendMessage(u.chat_id, "Access has been revoked go to " + \
                                                "http://robotoos.ir:5000/login")
                    db.session.delete(u)
                    db.session.commit()

            logging.info("sleeping")
            sys.stdout.flush()
            time.sleep(120)
Example #49
0
    def sent_to_API(title, start_time1, end_time1):
        # credentials = pickle.load(open("token.pkl", "rb"))
        # service = build("calendar", "v3", credentials=credentials)
        """"Get Credentials"""
        home_dir = os.path.expanduser('~')
        credential_dir = os.path.join(home_dir, '.credentials')
        if not os.path.exists(credential_dir):
            os.makedirs(credential_dir)
        credential_path = os.path.join(credential_dir,
                                       'calendar-python-quickstart.json')

        store = file.Storage(credential_path)
        credentials = store.get()
        if not credentials or credentials.invalid is True:
            flow = get_flow(request)
            flow.params['state'] = xsrfutil.generate_token(
                config('SECRET_KEY'), request.user)
            request.session['flow'] = pickle.dumps(flow).decode('iso-8859-1')
            authorize_url = flow.step1_get_authorize_url()

            return HttpResponseRedirect(authorize_url)

        service = build("calendar", "v3", credentials=credentials)
        """"Get my calendar"""
        result = service.calendarList().list().execute()
        calendar_id = result['items'][0]['id']
        """ Create a new event"""
        start_time1 = datetime.strptime(
            start_time1, "%Y-%m-%d %H:%M:%S")  # convert to datetime
        end_time1 = datetime.strptime(
            end_time1, "%Y-%m-%d %H:%M:%S")  # convert to datetime
        timezone = 'America/Sao_Paulo'

        event = {
            'summary': title,
            'location': 'ValeVerde',
            'description': 'Teste',
            'start': {
                'dateTime': start_time1.strftime("%Y-%m-%dT%H:%M:%S"),
                'timeZone': timezone,
            },
            'end': {
                'dateTime': end_time1.strftime("%Y-%m-%dT%H:%M:%S"),
                'timeZone': timezone,
            },
            'reminders': {
                'useDefault':
                False,
                'overrides': [
                    {
                        'method': 'email',
                        'minutes': 24 * 60
                    },
                    {
                        'method': 'popup',
                        'minutes': 10
                    },
                ],
            },
        }
        service.events().insert(calendarId=calendar_id, body=event).execute()
        #!/usr/bin/env python

        from __future__ import print_function
        import os

        from apiclient.discovery import build
        from httplib2 import Http
        from oauth2client import file, client, tools
        import requests
        try:
            import argparse
            flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
        except ImportError:
            flags = None

        SCOPES = 'https://www.googleapis.com/auth/drive.file'
        store = file.Storage('storage.json')
        creds = store.get()
        if not creds or creds.invalid:
            flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
            creds = tools.run_flow(flow, store, flags) \
                    if flags else tools.run(flow, store)
        DRIVE = build('drive', 'v2', http=creds.authorize(Http()))
        print (DRIVE.children().list( folderId='0BwZkkLKYLl7WQ2FFeXFZcHpReUE').execute()['items'])


Example #51
0
def get_authenticated_service():
    flow = InstalledAppFlow.from_client_secrets_file(CLIENT, SCOPES)
    credentials = flow.run_console()
    return build(API_SERVICE_NAME, API_VERSION, credentials=credentials)
def get_walks():
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)

    page = requests.get("https://www.madwalkers.org.uk/walks/")
    tree = html.fromstring(page.content)

    walks_xpath = "/html/body/div[7]/div[1]/div[2]/div[position()>1]"

    walks = tree.xpath(walks_xpath)
    walks_number = tree.xpath("count(" + walks_xpath + ")")

    try:
        with open("walks.pickle", "rb") as input:
            all_walks_dict = pickle.load(input)
    except:
        all_walks_dict = {}

    try:
        for i in range(int(walks_number)):
            walk = walks[i]
            walk_dict = {}

            walk_id = walk.xpath("@id")[0]
            walk_dict["web_id"] = walk_id
            walk_dict[
                "web_link"] = "https://www.madwalkers.org.uk/walks/walk.php?id=" + walk_id[
                    5:]
            walk_dict["colour_id"] = 3

            walk_day = walk.xpath(".//p[@class='pIconDay']/text()")[0]
            walk_month_node = walk.xpath(
                ".//div[starts-with(@class, 'imgMon')]")
            walk_month = walk_month_node[0].xpath("@class")[0].replace(
                "imgMon", "").replace("walk-month", "")

            today = datetime.date.today()

            if int(today.month) > 9 and int(walk_month) < 5:
                walk_year = int(today.year) + 1
            else:
                walk_year = int(today.year)

            walk_dict["day"] = int(walk_day)
            walk_dict["month"] = int(walk_month)
            walk_dict["year"] = int(walk_year)

            walk_date = datetime.date(walk_dict["year"], walk_dict["month"],
                                      walk_dict["day"])

            walk_dict["days_to_walk"] = (walk_date - today).days

            walk_title = walk.xpath(".//a[@class='pWalkTitle']/text()")[0]
            walk_dict["title"] = walk_title

            walk_brief = "".join(walk.xpath(".//p[@class='pHdr']/text()"))
            walk_dict["brief"] = walk_brief.encode('utf-8')

            if "Public Transport" in walk_brief:
                walk_dict["transport"] = "Public"
            else:
                walk_dict["transport"] = "Direct"

            walk_full = ("".join(
                walk.xpath(
                    ".//div[@class='walk-info']/div[position()>2]//text()"))
                         ).replace("\t", "")

            walk_dict["full"] = walk_full.encode('utf-8')

            try:
                walk_special_notes = walk.xpath(
                    ".//span[contains(@style,'color:#ff0000')]//text()")
                walk_special_notes = " ".join(walk_special_notes)
            except:
                walk_special_notes = ""

            walk_dict["special"] = walk_special_notes.encode('utf-8')

            try:
                walk_length = walk.xpath(".//p[@class='pIconPanel']/text()")[0]
            except:
                walk_length = "TBD"

            walk_dict["length"] = walk_length

            try:
                walk_ascent = walk.xpath(
                    ".//p[@class='pIconAscent']/text()")[0]
            except:
                walk_ascent = "TBD"

            walk_dict["ascent"] = walk_ascent

            try:
                #time = walk.xpath(".//td[@class='tdMeetTm']/p/text()")[0]
                walk_start = " ".join(
                    walk.xpath(
                        ".//div[@class='walk-info']/div[position()=(last()-1)]/div[1]/div[2]/div[1]/p//text()"
                    ))

                #first_step = "".join(walk.xpath(".//td[@class='tdMeetDt']")[0].xpath(".//text()"))
                #walk_start = time+": "+first_step
            except:
                walk_start = "TBD"

            walk_dict["start"] = walk_start.encode('utf-8')
            #
            #            step_text = ""
            #            try:
            #                single_step = walk.xpath(".//table[@class='walkTableGD'][2]/tr[2]/td[2]/table/tr")
            #
            #
            #                steps = walk.xpath(".//td[@class='tdMeetDt']")
            #
            #                for i in range(len(single_step)):
            #                    step_text += "".join(single_step[i].xpath(".//td[@class='tdMeetTm']//text()"))+"\n"
            #                    step_text += "".join(single_step[i].xpath(".//td[@class='tdMeetDt']//text()"))+"\n"
            #
            #            except:
            #               step_text = ""
            #
            walk_dict["steps"] = ""  #step_text.encode('utf-8')
            if not walk_dict['web_id'] in all_walks_dict:
                new_walk(walk_dict, service)
                all_walks_dict[walk_dict["web_id"]] = walk_dict
                print(walk_dict['calendar_id'])
            else:
                walk_dict['calendar_id'] = all_walks_dict[
                    walk_dict["web_id"]]['calendar_id']
                if not all_walks_dict[walk_dict["web_id"]] == walk_dict:
                    changed_walk(walk_dict, service)
                    all_walks_dict[walk_dict["web_id"]] = walk_dict
    except:
        with open("walks.pickle", "wb") as output:
            pickle.dump(all_walks_dict, output, pickle.HIGHEST_PROTOCOL)
        raise

    with open("walks.pickle", "wb") as output:
        pickle.dump(all_walks_dict, output, pickle.HIGHEST_PROTOCOL)
Example #53
0
import requests
import csv
import json
import click
from bs4 import BeautifulSoup
from apiclient.discovery import build
from collections import Counter
from tqdm import tqdm
from config import YOUTUBE_API_KEY
from datetime import datetime

YOUTUBE_VIDEO = "https://www.youtube.com/watch?v={id}"
YOUTUBE_UPLOADS = "https://www.youtube.com/{}/videos?sort=dd&flow=grid"
YOUTUBE_EMBED = "https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={}"

yt = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)


def api_video(video_id):
    meta = yt.videos().list(id=video_id, part="id,snippet").execute()
    return meta["items"][0]


def get_channel_name(video_id):
    resp = requests.get(YOUTUBE_EMBED.format(video_id))
    try:
        data = resp.json()
        return data["author_name"], data["title"]
    except:
        print(video_id)
def SendMessage(sender, to, subject, msgPlain):
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('gmail', 'v1', http=http)
    message1 = CreateMessage(sender, to, subject, msgPlain)
    SendMessageInternal(service, "me", message1)
Example #55
0
def main():
    scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
    store = file.Storage('E:\Project\storage.json')
    creds = store.get()

    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('C:/creds.json', scope)
        creds = client.OAuth2Credentials
        creds = tools.run_flow(flow, store)

    SERVICE = build('drive', 'v3', http=creds.authorize(Http()))
    releases_dir_id = '0B-31_xdQ4lnzY1B5eWRhVElOdHc'

    payload = SERVICE.files().list(
        q=" mimeType = 'application/vnd.google-apps.folder' and \
                            trashed = false and \
                            %r in parents " % releases_dir_id,  # can add sharedWithMe = true
        fields='files(id, name)').execute()

    directories = payload.get('files', [])
    test = map(lambda x: x['name'], directories)
    print(test)

    client_gspread = gspread.authorize(creds)

    drive_sub_dir_name = "GA-2.0.0"  # input("enter the sub directory name")
    directory_id = get_directory_id_by_name(directories, drive_sub_dir_name)
    spreadsheet_list = get_spreadsheets_by_directory_id(directory_id, SERVICE)
    print(spreadsheet_list)

    sheet_arr = list()
    summary_sheet = client_gspread.create('sheet_new').sheet1
    print("New sheet is Created")
    print(type(spreadsheet_list))
    summary_sheet.clear()
    bool = True
    # open all the sheets in the folder
    for i in spreadsheet_list:
        try:
            spreadsheet_name = i['name']
            sh = client_gspread.open(spreadsheet_name)
            sum_sheet = sh.worksheet('Summary')
            cell_val = sum_sheet.find("Total")
            if (bool == True):
                sp_name = sum_sheet
                bool = False
        except:
            continue
        print(cell_val)
        print(type(cell_val))
        print("Found something at R%sC%s" % (cell_val.row, cell_val.col))

        summary_sheet.insert_row(sum_sheet.row_values(cell_val.row))
        #print("values of sheet", sum_sheet.row_values(cell_val.row))
        #print(cell_val)
        #print(spreadsheet_name)
        cell_val_2 = summary_sheet.find("Total")
        summary_sheet.update_cell(cell_val_2.row, cell_val_2.col, spreadsheet_name)

    # To clear contents of the newly created sheet
    summary_sheet.insert_row(list())
    summary_sheet.insert_row(sp_name.row_values(1))
    summary_sheet.update_cell(1, 1, " ")
Example #56
0
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

try:
    import auth
    # If modifying these scopes, delete your previously saved credentials
    # at ~/.credentials/drive-python-quickstart.json
    SCOPES = 'https://www.googleapis.com/auth/drive'
    CLIENT_SECRET_FILE = 'credentials.json'
    APPLICATION_NAME = 'Drive API Python Quickstart'
    authInst = auth.auth(SCOPES, CLIENT_SECRET_FILE, APPLICATION_NAME)
    credentials = authInst.getCredentials()

    http = credentials.authorize(httplib2.Http())
    drive_service = discovery.build('drive', 'v3', http=http)
except Exception as e:
    sendMail(e)


def listFiles():
    try:
        file_list = []
        response = drive_service.files().list(
            q="mimeType='application/x-7z-compressed'",
            spaces='drive',
            fields='nextPageToken, files(id, name)',
            pageToken=None).execute()
        for file in response.get('files', []):
            # Process change
            file_list.append(file.get('name'))
def youtube_search(q,
                   max_results=50,
                   order="relevance",
                   token=None,
                   location=None,
                   location_radius=None):

    youtube = build(YOUTUBE_API_SERVICE_NAME,
                    YOUTUBE_API_VERSION,
                    developerKey=DEVELOPER_KEY)

    search_response = youtube.search().list(
        q=q,
        type="video",
        pageToken=token,
        order=order,
        part=
        "id,snippet",  # Part signifies the different types of data you want 
        maxResults=max_results,
        location=location,
        locationRadius=location_radius).execute()



    videos,channelId,channelTitle,categoryId,title,videoId,viewCount,likeCount,dislikeCount,commentCount,favoriteCount,category,tags  = []

    for search_result in search_response.get("items", []):

        if search_result["id"]["kind"] == "youtube#video":

            title.append(search_result['snippet']['title'])
            videoId.append(search_result['id']['videoId'])

            response = youtube.videos().list(part='statistics, snippet',
                                             id=videoId_temp).execute()

            channelId.append(response['items'][0]['snippet']['channelId'])
            channelTitle.append(
                response['items'][0]['snippet']['channelTitle'])
            categoryId.append(response['items'][0]['snippet']['categoryId'])
            favoriteCount.append(
                response['items'][0]['statistics']['favoriteCount'])
            viewCount.append(response['items'][0]['statistics']['viewCount'])
            likeCount.append(response['items'][0]['statistics']['likeCount'])
            dislikeCount.append(
                response['items'][0]['statistics']['dislikeCount'])

            if 'commentCount' in response['items'][0]['statistics'].keys():
                commentCount.append(
                    response['items'][0]['statistics']['commentCount'])
            else:
                commentCount.append([])

            if 'tags' in response['items'][0]['snippet'].keys():
                tags.append(response['items'][0]['snippet']['tags'])
            else:
                tags.append([])

            youtube_dict = {
                'tags': tags,
                'channelId': channelId,
                'channelTitle': channelTitle,
                'categoryId': categoryId,
                'title': title,
                'videoId': videoId,
                'viewCount': viewCount,
                'likeCount': likeCount,
                'dislikeCount': dislikeCount,
                'commentCount': commentCount,
                'favoriteCount': favoriteCount
            }

            return youtube_dict
Example #58
0
    #store fdist in dictionary for use in creating combined fdist
    features['fDist'] = fdist
    #convert FreqDist to dictionary
    fdist = dict(fdist)
    #append FreqDist to features
    features.update(fdist)
    return features


if __name__ == '__main__':
    #Define youtube API parameters
    YOUTUBE_API_SERVICE_NAME = "youtube"
    YOUTUBE_API_VERSION = "v3"
    DEVELOPER_KEY = 'AIzaSyD4gc2e-6lPQYg_T-kq8l9gIPtitqVhM7o'
    youtube = build(YOUTUBE_API_SERVICE_NAME,
                    YOUTUBE_API_VERSION,
                    developerKey=DEVELOPER_KEY)

    #Initialize video dictionary
    videoComments = {}
    #read video Ids from csv file
    videoIds = pd.read_csv("USvideos.csv")["video_id"]
    #Iterate through video Ids
    j = 0
    for video_id in videoIds:
        print('Processing video: ' + video_id)
        try:
            #Intiliaze comment list
            comments = []
            video_comment_threads = get_comment_threads(
                youtube, video_id, comments)
Example #59
0
def Main():
    Credentials = GetCredentials()
    HTTPAuthorization = Credentials.authorize(httplib2.Http())
    CalendarService = discovery.build('calendar', 'v3', http = HTTPAuthorization)
    CurrentTime = datetime.datetime.utcnow().isoformat() + 'Z'

    CalendarEvents = CalendarService.events().list(
        calendarId = S.CalendarId,
        timeMin = CurrentTime,
        maxResults = S.CalendarMaxEvents,
        singleEvents = True,
        orderBy = 'startTime').execute()

    RetrievedEvents = CalendarEvents.get('items', [])
    MaxEvents = int(S.CalendarMaxEvents)

    if not RetrievedEvents:
        print('No upcoming events found.')

    if S.OpenHABPort.strip() != '':
        TrimmedHostAndPort = S.OpenHABHostName.strip() + ':' + S.OpenHABPort.strip()
    else:
        TrimmedHostAndPort = S.OpenHABHostName.strip()
        
    EventCounter = 0

    for SingleEvent in range(0, MaxEvents):
        EventCounter += 1

        CalendarEventSummaryItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_Summary'
        OpenHABResponse = requests.post(CalendarEventSummaryItemURL, data = '', allow_redirects = True)
       
        CalendarEventLocationItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_Location'
        OpenHABResponse = requests.post(CalendarEventLocationItemURL, data = '', allow_redirects = True)

        CalendarEventDescriptionItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_Description'
        OpenHABResponse = requests.post(CalendarEventDescriptionItemURL, data = '', allow_redirects = True)
        
        CalendarEventStartTimeItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_StartTime'
        OpenHABResponse = requests.post(CalendarEventStartTimeItemURL, data = '1909-12-19T00:00:00.000+0100', allow_redirects = True)

        CalendarEventEndTimeItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_EndTime'
        OpenHABResponse = requests.post(CalendarEventEndTimeItemURL, data = '1909-12-19T00:00:00.000+0100', allow_redirects = True)
        
    time.sleep(2)

    EventCounter = 0

    for SingleEvent in RetrievedEvents:
        EventSummary = ''
        EventLocation = ''
        EventDescription = ''
        EventStartTime = None
        EventEndTime = None

        EventCounter += 1

        if 'summary' in SingleEvent:
            EventSummary = SingleEvent['summary']

        if 'location' in SingleEvent:
            EventLocation = SingleEvent['location']

        if 'description' in SingleEvent:
            EventDescription = SingleEvent['description']

        if 'start' in SingleEvent:
            EventStartTime = SingleEvent['start'].get('dateTime', SingleEvent['start'].get('date'))

        try:
            datetime.datetime.strptime(EventStartTime, '%Y-%m-%dT%H:%M:%S' + S.CalendarTimeZone)
        except ValueError:
            EventStartTime = EventStartTime + 'T00:00:00' + S.CalendarTimeZone

        if 'end' in SingleEvent:
            EventEndTime = SingleEvent['end'].get('dateTime', SingleEvent['end'].get('date'))

        try:
            datetime.datetime.strptime(EventEndTime, '%Y-%m-%dT%H:%M:%S' + S.CalendarTimeZone)
        except ValueError:
            EventEndTime = EventEndTime + 'T00:00:00' + S.CalendarTimeZone            

        CalendarEventSummaryItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_Summary'
        OpenHABResponse = requests.post(CalendarEventSummaryItemURL, data = EventSummary.encode('utf-8'), allow_redirects = True)

        CalendarEventLocationItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_Location'
        OpenHABResponse = requests.post(CalendarEventLocationItemURL, data = EventLocation.encode('utf-8'), allow_redirects = True)

        CalendarEventDescriptionItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_Description'
        OpenHABResponse = requests.post(CalendarEventDescriptionItemURL, data = EventDescription.encode('utf-8'), allow_redirects = True)

        CalendarEventStartTimeItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_StartTime'
        OpenHABResponse = requests.post(CalendarEventStartTimeItemURL, data = EventStartTime, allow_redirects = True)
    
        CalendarEventEndTimeItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_EndTime'
        OpenHABResponse = requests.post(CalendarEventEndTimeItemURL, data = EventEndTime, allow_redirects = True)
Example #60
0
 def _get_service(self) -> SheetsService:
     http_authorized = self._authorize()
     return build('sheets', 'v4', http=http_authorized)