{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('EECS491', 'Assignment 3 - Inference in Larger Graphical Models.txt')\n"
     ]
    }
   ],
   "source": [
    "# selenium for web driving\n",
    "import selenium\n",
    "from selenium import webdriver\n",
    "\n",
    "# time for pausing between navigation\n",
    "import time\n",
    "\n",
    "# Datetime for recording time of submission\n",
    "import datetime\n",
    "\n",
    "# os for file management\n",
    "import os\n",
    "\n",
    "# Build tuple of files to turn in\n",
    "submission_dir = 'C:/Users/Will Koehrsen/Desktop/completed_assignments'\n",
    "dir_list = list(os.listdir(submission_dir))\n",
    "\n",
    "for directory in dir_list:\n",
    "    file_list = list(os.listdir(os.path.join(submission_dir, directory)))\n",
    "    if len(file_list) != 0:\n",
    "        file_tup = (directory, file_list[0])\n",
    "    \n",
    "print(file_tup)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Using Chrome to access web\n",
    "driver = webdriver.Chrome()\n",
    "\n",
    "# Open the website\n",
    "driver.get('https://canvas.case.edu')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Password for Canvas\n",
    "with open('C:/Users/Will Koehrsen/Desktop/cp.txt', 'r') as f:\n",
    "    cp = f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Locate id and password\n",
    "id_box = driver.find_element_by_name('username')\n",
    "pass_box = driver.find_element_by_name('password')\n",
    "\n",
    "# Send information\n",
    "id_box.send_keys('wjk68')\n",
    "pass_box.send_keys(cp)\n",
    "\n",
    "# Click login\n",
    "login_button = driver.find_element_by_name('submit')\n",
    "login_button.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Find and click on list of courses\n",
    "courses_button = driver.find_element_by_id('global_nav_courses_link')\n",
    "courses_button.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Wait for the page to load\n",
    "time.sleep(2)\n",
    "\n",
    "# Get the name of the folder\n",
    "folder = file_tup[0]\n",
    "    \n",
    "# Class to select depends on folder\n",
    "if folder == 'DSCI451':\n",
    "    class_select = driver.find_element_by_link_text('Applied Data Science Research (100/5047)')\n",
    "elif folder == 'DCSI453':\n",
    "    class_select = driver.find_element_by_link_text('Data Science: Statistical Learning, Modeling and Prediction (100/5046)')\n",
    "elif folder == 'EECS491':\n",
    "    class_select = driver.find_element_by_link_text('Artificial Intelligence: Probabilistic Graphical Models (100/10039)')\n",
    "elif folder == 'EECS531':\n",
    "    class_select = driver.find_element_by_link_text('Computer Vision (100/10040)')\n",
    "\n",
    "# Click on the specific class\n",
    "class_select.click()\n",
    "\n",
    "assignment_button = driver.find_element_by_link_text('Assignments')\n",
    "assignment_button.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Wait for the page to load \n",
    "time.sleep(2)\n",
    "\n",
    "# Locate the specific assignment\n",
    "file_name = file_tup[1]\n",
    "file_locator = file_name.split('.')[0]\n",
    " \n",
    "specific_assigment = driver.find_element_by_link_text(file_locator)\n",
    "specific_assigment.click()\n",
    "\n",
    "# Click on the button to submit an assignment\n",
    "try:\n",
    "    submit_assignment_button = driver.find_element_by_link_text('Submit Assignment')\n",
    "except:\n",
    "    print('Assignment already submitted, re-submitting')\n",
    "    submit_assignment_button = driver.find_element_by_link_text('Re-submit Assignment')\n",
    "\n",
    "submit_assignment_button.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Wait for the page to load\n",
    "time.sleep(2)\n",
    "\n",
    "# Choose file button\n",
    "choose_file = driver.find_element_by_name('attachments[0][uploaded_data]')\n",
    "\n",
    "# Send the name of the file to the button\n",
    "file_location = os.path.join(submission_dir, folder, file_name)\n",
    "choose_file.send_keys(file_location)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Wait for the page\n",
    "time.sleep(2)\n",
    "\n",
    "# Move the file to the submitted folder\n",
    "submitted_dir = 'C:/Users/Will Koehrsen/Desktop/submitted_assignments'\n",
    "submitted_dir = os.path.join(submitted_dir, folder)\n",
    "submitted_file_name = 'Submitted ' + file_name\n",
    "\n",
    "submitted_file_location = os.path.join(submitted_dir, submitted_file_name)\n",
    "os.rename(file_location, submitted_file_location)\n",
    "\n",
    "print('{} Assignment for Class {} successfully submitted at {}.'.format(file_name, \n",
    "                    folder, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))\n",
    "\n",
    "print('Submitted assignment available at {}.'.format(submitted_file_location))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "submit_assignment = driver.find_element_by_id('submit_file_button')\n",
    "submit_assignent.click()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
