├── .gitignore ├── msql-backup └── google_drive_api.py └── status-update-checker ├── README.md ├── data.py ├── email_checker.py └── gmail_api.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.csv 2 | *.txt 3 | *.pyc 4 | .idea/ 5 | data/ 6 | -------------------------------------------------------------------------------- /msql-backup/google_drive_api.py: -------------------------------------------------------------------------------- 1 | import httplib2 2 | import os 3 | 4 | from googleapiclient.http import MediaFileUpload 5 | from oauth2client import client 6 | from oauth2client import tools 7 | from oauth2client.file import Storage 8 | 9 | try: 10 | import argparse 11 | flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() 12 | except ImportError: 13 | flags = None 14 | 15 | # If modifying these scopes, delete your previously saved credentials 16 | # at ~/.credentials/drive-python-quickstart.json 17 | SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly' 18 | CLIENT_SECRET_FILE = 'client_secret.json' 19 | APPLICATION_NAME = 'Drive API Python Quickstart' 20 | 21 | 22 | def get_credentials(): 23 | """Gets valid user credentials from storage. 24 | 25 | If nothing has been stored, or if the stored credentials are invalid, 26 | the OAuth2 flow is completed to obtain the new credentials. 27 | 28 | Returns: 29 | Credentials, the obtained credential. 30 | """ 31 | home_dir = os.path.expanduser('~') 32 | credential_dir = os.path.join(home_dir, '.credentials') 33 | if not os.path.exists(credential_dir): 34 | os.makedirs(credential_dir) 35 | credential_path = os.path.join(credential_dir, 36 | 'drive-python-quickstart.json') 37 | 38 | store = Storage(credential_path) 39 | credentials = store.get() 40 | if not credentials or credentials.invalid: 41 | flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) 42 | flow.user_agent = APPLICATION_NAME 43 | credentials = tools.run_flow(flow, store, flags) 44 | print('Storing credentials to ' + credential_path) 45 | return credentials 46 | 47 | 48 | def upload_file( 49 | drive_service, parent_folder_id, file_name, file_path, mime_type): 50 | """ 51 | :param drive_service: discovery.build('drive', 'v3', http=http) 52 | :param parent_folder_id: ID of folder to upload the file 53 | :param file_name: name of the file 54 | :param file_path: path to the file 55 | :param mime_type: mime type of file (ex: image/jpeg) 56 | :return: File id from Google drive 57 | """ 58 | file_metadata = {'name': file_name, 'parents': [parent_folder_id]} 59 | media = MediaFileUpload(file_path, 60 | mimetype=mime_type) 61 | file = drive_service.files().create(body=file_metadata, 62 | media_body=media, 63 | fields='id').execute() 64 | return file.get('id') 65 | 66 | 67 | def create_folder(drive_service, parent_folder_id, folder_name): 68 | """ 69 | :param drive_service: discovery.build('drive', 'v3', http=http) 70 | :param parent_folder_id: ID of folder to upload the file 71 | :param folder_name: Name of the folder to be created 72 | :return: 73 | """ 74 | file_metadata = { 75 | 'name': folder_name, 76 | 'parents': [parent_folder_id], 77 | 'mimeType': 'application/vnd.google-apps.folder' 78 | } 79 | folder = drive_service.files().create(body=file_metadata, 80 | fields='id').execute() 81 | return folder.get('id') 82 | 83 | 84 | if __name__ == '__main__': 85 | credentials = get_credentials() 86 | http = credentials.authorize(httplib2.Http()) 87 | -------------------------------------------------------------------------------- /status-update-checker/README.md: -------------------------------------------------------------------------------- 1 | ### This project is now obsolete and is marked as `archived`, and has been replaced by the Status Update Engine in [amFOSS CMS](https://github.com/amfoss/cms). 2 | 3 | # status-tracker 4 | This is a small script that provides user to access the mail id's of the people who sent messages matching the required query. 5 | ## Getting Started 6 | First clone this repository and then follow the given steps below :- 7 | 1) Go to this link https://developers.google.com/gmail/api/quickstart/python 8 | 2) Follow the steps specified on the redireccted page 9 | 3) Then you can run the scripts if you are done with the above process 10 | ### common errors 11 | 1) Don't forget to register your project in https://console.developers.google.com/flows/enableapi?apiid=gmail and Make sure that you get a redirected page that ask's you to allow the api to your inbox 12 | ### Prerequisites 13 | You need to have a google account to do this and the rest are specified under getting started itself 14 | ## Running the script 15 | ```python 16 | python email_checker.py '14/02/2018' 17 | ``` 18 | 19 | ## Built with 20 | Python 21 | ## Organisation 22 | foss@amrita:- http://foss.amrita.ac.in/ 23 | ## Author 24 | Guvvala Prasanth Reddy 25 | 26 | Github : - https://github.com/automatefoss 27 | ## Mentor 28 | Chirath R 29 | 30 | Github : - https://github.com/Chirath02 31 | -------------------------------------------------------------------------------- /status-update-checker/data.py: -------------------------------------------------------------------------------- 1 | import os 2 | import csv 3 | 4 | file = open('data.txt', 'r') 5 | cs = open('1.csv', 'w') 6 | a = [] 7 | original_email_ids = [] 8 | with cs: 9 | write = csv.writer(cs) 10 | for line in file: 11 | a = line.strip('|').split('|') 12 | for item in a: 13 | item.strip() 14 | write.writerow(a) 15 | -------------------------------------------------------------------------------- /status-update-checker/email_checker.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import test 3 | import csv 4 | 5 | 6 | date = sys.argv[1] 7 | 8 | cs = open('data/1.csv', 'r') 9 | y1 = [] 10 | y2 = [] 11 | y3 = [] 12 | y4 = [] 13 | y = [] 14 | 15 | y1.append(10 * "-" + 'FIRST YEARS' + 10 * "-") 16 | y2.append(10 * "-" + 'SECOND YEARS' + 10 * "-") 17 | y3.append(10 * "-" + 'THIRD YEARS' + 10 * "-") 18 | y4.append(10 * "-" + "FOURTH YEARS" + 10 * "-") 19 | 20 | message = test.main(date) 21 | 22 | print ("Number of status mails %d" % len(message)) 23 | 24 | with cs: 25 | wcs = csv.reader(cs) 26 | for row in wcs: 27 | if row[2].strip() not in message: 28 | print(row[2]) 29 | if row[3] == '1': 30 | y1.append(row[0] + row[1]) 31 | elif row[3] == '2': 32 | y2.append(row[0] + row[1]) 33 | elif row[3] == '3': 34 | y3.append(row[0] + row[1]) 35 | elif row[3] == '4': 36 | y4.append(row[0] + row[1]) 37 | y.append(y1) 38 | y.append(y2) 39 | y.append(y3) 40 | y.append(y4) 41 | for item in y: 42 | for string in item: 43 | print(string) 44 | -------------------------------------------------------------------------------- /status-update-checker/gmail_api.py: -------------------------------------------------------------------------------- 1 | import os 2 | import httplib2 3 | from apiclient import discovery 4 | from oauth2client import client 5 | from oauth2client import tools 6 | from oauth2client.file import Storage 7 | from apiclient import errors 8 | 9 | try: 10 | import argparse 11 | 12 | flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() 13 | except ImportError: 14 | flags = None 15 | 16 | # If modifying these scopes, delete your previously saved credentials 17 | # at ~/.credentials/gmail-python-quickstart.json 18 | SCOPES = 'https://www.googleapis.com/auth/gmail.readonly' 19 | CLIENT_SECRET_FILE = 'client_secret.json' 20 | APPLICATION_NAME = 'Gmail API Python Quickstart' 21 | 22 | 23 | def get_credentials(): 24 | """Gets valid user credentials from storage. 25 | 26 | If nothing has been stored, or if the stored credentials are invalid, 27 | the OAuth2 flow is completed to obtain the new credentials. 28 | 29 | Returns: 30 | Credentials, the obtained credential. 31 | """ 32 | home_dir = os.path.expanduser('~') 33 | credential_dir = os.path.join(home_dir, '.credentials') 34 | if not os.path.exists(credential_dir): 35 | os.makedirs(credential_dir) 36 | credential_path = os.path.join(credential_dir, 37 | 'gmail-python-quickstart.json') 38 | 39 | store = Storage(credential_path) 40 | credentials = store.get() 41 | if not credentials or credentials.invalid: 42 | flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) 43 | flow.user_agent = APPLICATION_NAME 44 | if flags: 45 | credentials = tools.run_flow(flow, store, flags) 46 | else: # Needed only for compatibility with Python 2.6 47 | credentials = tools.run(flow, store) 48 | print('Storing credentials to ' + credential_path) 49 | return credentials 50 | 51 | 52 | def get_label_id(service, user_id, label_name): 53 | """ 54 | Return the label id of a Gmail label 55 | :param service: Authorized Gmail API service instance. 56 | :param user_id: User's email address. The special value "me" 57 | :param label_name: The name of the label id required 58 | :return: string(label_id) 59 | """ 60 | label_id = '' 61 | try: 62 | response = service.users().labels().list(userId=user_id).execute() 63 | labels = response['labels'] 64 | for label in labels: 65 | if label['name'] == label_name: 66 | label_id = label['id'] 67 | except errors.HttpError: 68 | print('An error occurred') 69 | 70 | return label_id 71 | 72 | 73 | def list_messages_matching_query(service, user_id, query=''): 74 | """List all Messages of the user's mailbox matching the query. 75 | 76 | Args: 77 | service: Authorized Gmail API service instance. 78 | user_id: User's email address. The special value "me" 79 | can be used to indicate the authenticated user. 80 | query: String used to filter messages returned. 81 | Eg.- 'from:user@some_domain.com' for Messages from a particular sender. 82 | 83 | Returns: 84 | List of Messages that match the criteria of the query. Note that the 85 | returned list contains Message IDs, you must use get with the 86 | appropriate ID to get the details of a Message. 87 | """ 88 | try: 89 | response = service.users().messages().list(userId=user_id, 90 | q=query).execute() 91 | messages = [] 92 | if 'messages' in response: 93 | messages.extend(response['messages']) 94 | 95 | while 'nextPageToken' in response: 96 | page_token = response['nextPageToken'] 97 | response = service.users().messages().list( 98 | userId=user_id, q=query, pageToken=page_token).execute() 99 | messages.extend(response['messages']) 100 | return messages 101 | 102 | except errors.HttpError: 103 | print('An error occurred') 104 | 105 | 106 | def get_sender_email_id(service, user_id, msg_id): 107 | """ 108 | This functions return the email id of the sender 109 | :param service: The google service(here Gmail) 110 | :param user_id: The user id of the user 111 | :param msg_id: The message id that is to be retrived 112 | :return: string(email id) 113 | """ 114 | email_id = '' 115 | 116 | try: 117 | message = service.users().messages().get(userId=user_id, id=msg_id, 118 | format='metadata').execute() 119 | 120 | header_data = message["payload"]["headers"] 121 | 122 | sender_text = "X-Original-Sender" 123 | 124 | # Get email id from header data 125 | for data in header_data: 126 | if sender_text in data["name"]: 127 | email_id = data["value"] 128 | return email_id 129 | 130 | except errors.HttpError: 131 | print('An error occurred') 132 | 133 | 134 | def get_status_update_emails(status_update_date): 135 | """ 136 | 137 | :param status_update_date: Date of the status update you want to query 138 | :return: 139 | """ 140 | credentials = get_credentials() 141 | http = credentials.authorize(httplib2.Http()) 142 | service = discovery.build('gmail', 'v1', http=http) 143 | status_update_date_string = status_update_date.strftime('%d-%m-%Y') 144 | query = '[foss-2017] Status Update [%s]' % status_update_date_string 145 | 146 | emails = [] 147 | messages = list_messages_matching_query(service, user_id='me', query=query) 148 | 149 | for message in messages: 150 | email = get_sender_email_id( 151 | service, user_id="me", msg_id=message['id']) 152 | emails.append(email) 153 | 154 | return emails 155 | 156 | 157 | if __name__ == '__main__': 158 | credentials = get_credentials() 159 | http = credentials.authorize(httplib2.Http()) 160 | --------------------------------------------------------------------------------