예제 #1
0
def main():
    """Shows basic usage of the Gmail API.

    Creates a Gmail API service object and outputs a list of label names
    of the user's Gmail account.
    """
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('gmail', 'v1', http=http)

    lastHarvest,file_id = get_last_collection_time_and_file_id(DATA_DIR_EMAIL)
    file_id = file_id + 1

    print('Collecting SENT emails since last harvest')
    
    lastHarvest = datetime.datetime.strptime(lastHarvest[0:19], "%Y-%m-%dT%H:%M:%S")
    print(lastHarvest)
    
    #results = service.users().labels().list(userId='me').execute()
    results = service.users().messages().list(userId='me',labelIds='SENT').execute()
    messages = results.get('messages')
    

    #TODO: I'll need to handle the case of when more than 100 messages need to get added    
    #nextPageToken = results.get('nextPageToken')
    with open('%s/zcarwile_%d.txt' % (DATA_DIR_EMAIL,file_id),'w') as f:
        if not messages:
            print('No messages found.')
        else:
            for message in messages:
                to = ""
                subj = ""
                date = ""
                m = service.users().messages().get(userId='me',id=message['id'],format='metadata').execute()
                for header in m.get('payload').get('headers'):
                    if header['name'] == 'To':
                        to = header['value']
                    if header['name'] == 'Subject':
                        subj = header['value']
                    if header['name'] == 'Date':
                        email_date = email.utils.parsedate(header['value'])
                        email_time = time.mktime(email_date)
                        date = datetime.datetime.fromtimestamp(email_time)
                        
                if date > lastHarvest:
                    f.write('%s\t%s\t%s\t%s\n' % (message['id'],date,to,subj))
                else:
                    break
                
        print('Written to %s/zcarwile_%d.txt' % (DATA_DIR_EMAIL,file_id))
예제 #2
0
def main():
    """Shows basic usage of the Google Calendar API.

    Creates a Google Calendar API service object and outputs a list of the next
    10 events on the user's calendar.
    """
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)

    startTime = datetime.datetime(2015,9,21).isoformat() + 'Z'
    now = datetime.datetime.utcnow().isoformat() + 'Z'
    lastHarvest,file_id = get_last_collection_time_and_file_id(DATA_DIR_CALENDAR)
    file_id = file_id + 1 

    with open('%s/zcarwile_%d.txt' % (DATA_DIR_CALENDAR,file_id),'w') as f:    
    
        print('Getting Google Calendar events since last harvest')
        print(lastHarvest)
        eventsResult = service.events().list(
            calendarId='primary', timeMin=lastHarvest, timeMax=now, singleEvents=True,
            orderBy='startTime').execute()
        events = eventsResult.get('items', [])
    
        if not events:
            print('No new events found.')
        else:
            print('%s new events found' % (str(len(events))))
        
        
            for event in events:
                
                event_id = event['id']
                start = event['start'].get('dateTime', event['start'].get('date'))
                end = event['end'].get('dateTime', event['end'].get('date'))
                org = event['organizer'].get('displayName')
                
                #TODO -- do I want to add support for attendees and title?        
                
                f.write("%s\t%s\t%s\t%s\t%s\n" % (event_id,start, end, org, event['summary']))
                #print("%s\t%s\t%s\t%s\n" % (start, end, org, event['summary']))
            
        print('Written to %s/zcarwile_%d.txt' % (DATA_DIR_CALENDAR,file_id))
예제 #3
0
from qlib import get_last_collection_time_and_file_id

DATA_DIR_RESCUE_TIME = '../quixotic/data/operational/rescue_time'

with open("rescue_time_key") as f:
    key=f.read().strip()
    
    # TODO: Parameters
    kind = "document" #document or activity
    interval = "hour"
    format = "json"
    
    # TODO: Parameterize from outside
    
    lastHarvest,file_id = get_last_collection_time_and_file_id(DATA_DIR_RESCUE_TIME)
    date = datetime.datetime.strptime(lastHarvest[0:10], "%Y-%m-%d")
    now = datetime.datetime.now()
    
    print("Last Rescue Time Harvest: ")
    print(date)
    
    if (date < now):
        print("\nCalling RT API")
    else:
        print("Nothing to harvest")
    
    while (date < now):    
        str_date = str(date)[0:10]  
        print(str_date)        
  
예제 #4
0
파일: qetl.py 프로젝트: zcarwile/quixotic
            try:            
                cursor.execute(query)
            except:
                print("ERROR: %s" % (query))
            
cnx.commit()
cursor.close()


# ## Emails

# In[5]:

### write emails from latest file
cursor = cnx.cursor()
junk,file_id_email = get_last_collection_time_and_file_id(DATA_DIR_EMAIL)     
file_email = DATA_DIR_EMAIL + os.sep + "zcarwile_" + str(file_id_email) + ".txt"
with open(file_email,"r") as f:
    for line in f:
        event = line.split("\t")
        features = event[0]
        start = event[1] # 2015-11-24 16:03:57 -- MYSQL STR_TO_DATE('2015-11-24 16:03:57', '%d-%m-%YT h:%i:%s')
        detail = event[2].replace("\"","")
        title = event[3].replace("\"","")
        tags = "Email"
        fout.write("%s\t%s\t%s\t%s\t%s\t%s\n" % (features, start, "", title, detail, tags))
        query = "INSERT INTO event (start, title, detail, tags, features) VALUES (\"%s\", \"%s\", \"%s\", \"%s\", \"%s\")" % (start, title, detail, tags, features)
        try:            
            cursor.execute(query)
        except:
            print("ERROR: %s" % (query))