{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a66c1692-9c84-41ec-a135-b417ef41344d",
   "metadata": {},
   "source": [
    "<p style=\"text-align:center\">\n",
    "    <a href=\"https://skills.network/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDA0321ENSkillsNetwork928-2023-01-01\">\n",
    "    <img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/assets/logos/SN_web_lightmode.png\" width=\"200\" alt=\"Skills Network Logo\"  />\n",
    "    </a>\n",
    "</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e786144-f1e4-45ee-b287-f2f4b87300d3",
   "metadata": {},
   "source": [
    "# **JOBS API Implementation Using FLASK**\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aae78227-6f87-4090-80ab-49edba9c3003",
   "metadata": {},
   "source": [
    "Estimated time needed: **45 to 60** minutes\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "817156a7-4bab-46b8-9a67-55f3b0564c01",
   "metadata": {},
   "source": [
    "## Objectives\n",
    "\n",
    "You will be executing this code  so that the client application **Collecting Jobs API**  will be accessing this code executing on the server \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ac527ce-f921-44e2-b729-28ed03634a8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install flask"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e5bba17-58e7-49a4-8040-0831e93466f5",
   "metadata": {},
   "source": [
    "## Dataset Used in this Assignment\n",
    "\n",
    "The dataset used in this lab comes from the following source: https://www.kaggle.com/promptcloud/jobs-on-naukricom under the under a **Public Domain license**.\n",
    "\n",
    "> Note: We are using a modified subset of that dataset for the lab, so to follow the lab instructions successfully please use the dataset provided with the lab, rather than the dataset from the original source.\n",
    "\n",
    "The original dataset is a csv. We have converted the csv to json as per the requirement of the lab.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2eed468-b4cd-481a-a18e-1851e2fc7a4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "!wget  https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DA0321EN-SkillsNetwork/labs/module%201/Accessing%20Data%20Using%20APIs/jobs.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21697c9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import flask\n",
    "from flask import request, jsonify\n",
    "import requests\n",
    "import re\n",
    "\n",
    "def get_data(key,value,current):\n",
    "    results = list()\n",
    "    pattern_dict = {\n",
    "        'C'      : '(C)',\n",
    "        'C++'    : '(C\\+\\+)',\n",
    "        'Java'   :'(Java)',\n",
    "        'C#'     : '(C\\#)',\n",
    "        'Python' :'(Python)',\n",
    "        'Scala' : '(Scala)',\n",
    "        'Oracle' : '(Oracle)',\n",
    "        'SQL Server': '(SQL Server)',\n",
    "        'MySQL Server' :'(MySQL Server)',\n",
    "        'PostgreSQL':'(PostgreSQL)',\n",
    "        'MongoDB'    : '(MongoDB)',\n",
    "        'JavaScript'    : '(JavaScript)',\n",
    "        'Los Angeles' :'(Los Angeles)',\n",
    "        'New York':'(New York)',\n",
    "        'San Francisco':'(San Francisco)',\n",
    "        'Washington DC':'(Washington DC)',\n",
    "        'Seattle':'(Seattle)',\n",
    "        'Austin':'(Austin)',\n",
    "        'Detroit':'(Detroit)',\n",
    "        \n",
    "        \n",
    "        \n",
    "        \n",
    "        \n",
    "    }\n",
    "    for rec in current:\n",
    "        print(rec[key])\n",
    "        print(type(rec[key]))\n",
    "        print(rec[key].find(value))\n",
    "        #if rec[key].find(value) != -1:\n",
    "        import re\n",
    "        #reex_str = \"\"\"(C)|(C\\+\\+)|(JavaScript)|(Java)|(C\\#)|(Python)|(Scala)|(Oracle)|(SQL Server)|(MySQL Server)|(PostgreSQL)|(MongoDB)\"\"\"\n",
    "        if re.search(pattern_dict[value],rec[key]) != None:\n",
    "            results.append(rec)\n",
    "    return results\n",
    "\n",
    "app = flask.Flask(__name__)\n",
    "\n",
    "import json\n",
    "data = None\n",
    "with open('jobs.json',encoding='utf-8') as f:\n",
    "    # returns JSON object as\n",
    "    # a dictionary\n",
    "    data = json.load(f)\n",
    "    \n",
    "    \n",
    "\n",
    "@app.route('/', methods=['GET'])\n",
    "def home():\n",
    "    \n",
    "    return '''<h1>Welcome to flask JOB search API</p>'''\n",
    "\n",
    "\n",
    "@app.route('/data/all', methods=['GET'])\n",
    "def api_all():\n",
    "    return jsonify(data)\n",
    "\n",
    "\n",
    "@app.route('/data', methods=['GET'])\n",
    "def api_id():\n",
    "    # Check if keys such as Job Title,KeySkills, Role Category and others  are provided as part of the URL.\n",
    "    #  Assign the keys to the corresponding variables..\n",
    "    # If no key is provided, display an error in the browser.\n",
    "    res = None\n",
    "    for req in request.args:\n",
    "        \n",
    "        if req == 'Job Title':\n",
    "            key = 'Job Title'\n",
    "        elif req == 'Job Experience Required' :\n",
    "            key='Job Experience Required'\n",
    "        elif req == 'Key Skills' :\n",
    "            key='Key Skills'\n",
    "            \n",
    "        elif req == 'Role Category' :\n",
    "            key='Role Category'\n",
    "        elif req == 'Location' :\n",
    "            key='Location'\n",
    "        \n",
    "        elif req == 'Functional Area' :\n",
    "            key='Functional Area'\n",
    "        \n",
    "        elif req == 'Industry' :\n",
    "            key='Industry'\n",
    "        elif req == 'Role' :\n",
    "            key='Role'\n",
    "        elif req==\"id\":\n",
    "             key=\"id\"\n",
    "        else:\n",
    "            pass\n",
    "    \n",
    "        value = request.args[key]\n",
    "        if (res==None):\n",
    "            res = get_data(key,value,data)\n",
    "        else:\n",
    "            res = get_data(key,value,res)\n",
    "\n",
    "    # Use the jsonify function from Flask to convert our list of\n",
    "    # Python dictionaries to the JSON format.\n",
    "    return jsonify(res)\n",
    "\n",
    "app.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3599fa2-05b9-4285-9699-08dc2f14c7f2",
   "metadata": {},
   "source": [
    "## Authors\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b47c3a9c-437a-4067-a532-a7765443fbe7",
   "metadata": {},
   "source": [
    "Lakshmi Holla  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f42fdbf-c7b7-409f-b435-28d64d1607e3",
   "metadata": {},
   "source": [
    "### Other Contributors\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c323fbe6-dc6a-4dbc-9569-74c9041ea88c",
   "metadata": {},
   "source": [
    "Malika Singla  \n",
    "Ayushi Jain\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ffa5fb5-70ab-4f23-aee6-09cb260c1475",
   "metadata": {},
   "source": [
    "## Changelog\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9348598-d30b-4359-b163-8177b50bcb3a",
   "metadata": {},
   "source": [
    "| Date (YYYY-MM-DD) | Version | Changed By        | Change Description                 |\n",
    "| ----------------- | ------- | ----------------- | ---------------------------------- |\n",
    "| 2022-01-03        | 0.1     | Lakshmi Holla  | Created Initial Version\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f233ece-5867-4880-a325-d198f567a5b0",
   "metadata": {},
   "source": [
    "Copyright © 2022 IBM Corporation. This notebook and its source code are released under the terms of the [MIT License](https://cognitiveclass.ai/mit-license?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDA0321ENSkillsNetwork21426264-2021-01-01&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBM-DA0321EN-SkillsNetwork-21426264&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ).\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
