Skip to content

Commit

Permalink
Perform check on CSV file and create a new CSV file if the specified …
Browse files Browse the repository at this point in the history
…one doesn't exist. Console output updates.
  • Loading branch information
bhamodi committed Jul 9, 2015
1 parent e33320e commit 466357a
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@

import argparse
import csv
import os
import time

from datetime import datetime
from os.path import exists
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

def main():
print('\nStarting script...')
parser = argparse.ArgumentParser()
parser.add_argument('--user', dest='facebook_username', required=True, help='the email that you login to Facebook with')
parser.add_argument('--pass', dest='facebook_password', required=True, help='the password that you login to Facebook with')
parser.add_argument('--user', dest='facebook_username', required=True, help='the email that you login to facebook with')
parser.add_argument('--pass', dest='facebook_password', required=True, help='the password that you login to facebook with')
parser.add_argument('--path', dest='path_to_csv_file', required=True, help='the path to the csv file')

args = parser.parse_args()
Expand All @@ -22,11 +23,19 @@ def main():
path_to_csv_file = args.path_to_csv_file

# Verify that the CSV file exists before scraping
if not exists(path_to_csv_file):
raise Exception(path_to_csv_file + ' does not exist. Please try again.')
print('Verifying that the CSV file exists...')
if os.path.exists(path_to_csv_file):
print(path_to_csv_file + ' has been found.')
else:
print('[WARNING] ' + path_to_csv_file + ' does not exist. Creating a new CSV file now...')
path_to_csv_file = os.path.join(os.getcwd(), 'facebook_online_friend_tracker_data.csv')
with open(path_to_csv_file, 'w') as f:
writer = csv.writer(f)
writer.writerow(['Timestamp', 'Number of Online Friends'])
print('New CSV file created at: ' + path_to_csv_file)

# Initialize Chrome WebDriver and change default timeout
print('Initializing Chrome WebDriver...')
print('\nInitializing Chrome WebDriver...')
driver = webdriver.Chrome()
driver.implicitly_wait(180)

Expand All @@ -45,7 +54,7 @@ def main():

# Scrape the number of online friends
onlineFriendsCount = int(driver.find_element_by_xpath('//*[@id="fbDockChatBuddylistNub"]/a/span[2]/span').text.strip('()'))
print('Done! Detected ' + str(onlineFriendsCount) + ' online friends.')
print('\nDone! Detected ' + str(onlineFriendsCount) + ' online friends.')

# Close Chrome WebDriver
driver.close()
Expand All @@ -58,5 +67,3 @@ def main():
writer = csv.writer(f)
writer.writerow([today, onlineFriendsCount])
print('Added: ' + today + ' -> ' + str(onlineFriendsCount) + ' to the spreadsheet.')

main()

0 comments on commit 466357a

Please sign in to comment.