Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Easy Level Tasks #2

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions N.Sunil-Easy/Task 1/web_scrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Web Scraping Tool with BeautifulSoup
# Required Modules
import requests
from bs4 import BeautifulSoup
import csv
import json

# Fetching URL
url = 'https://www.imdb.com/chart/top/'

# Using headers to prevent boting
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}

# Converting Json to HTML
def js_to_html(html_content):
soup = BeautifulSoup(html_content, 'html.parser')
scripts = soup.find_all('script')

for script in scripts:
if script.get('type') == 'application/ld+json':
return script.string
return None

# Extracting the top 250 iMDB movie details from the url
def extract_movie_data(json_data):
movies = []

for item in json_data.get('itemListElement', []):
movie = item.get('item', {})
name = movie.get('name')
description = movie.get('description')
rating_value = movie.get('aggregateRating', {}).get('ratingValue')
content_rating = movie.get('contentRating')
genre = movie.get('genre')
movies.append({
'name': name,
'description': description,
'ratingValue': rating_value,
'contentRating': content_rating,
'genre': genre
})
return movies

# Fetching url
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
json_string = js_to_html(response.content)

#Checking the presense of json in the HTML content
if not json_string:
print("JSON data not found in the HTML content.")

else:
json_data = json.loads(json_string)
movies = extract_movie_data(json_data)
print(f"Extracted {len(movies)} movies.")
csv_file_name = 'imdb_top_250.csv'

# Saving fetchd data into .csv file
with open(csv_file_name, 'w', newline='', encoding='utf-8') as csvfile:
fieldnames = ['name', 'description', 'ratingValue', 'contentRating', 'genre']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(movies)
print(f"Data saved to '{csv_file_name}'.")
json_file_name = 'imdb_top_250.json'

# Saving fetchd data into .json file
with open(json_file_name, 'w', encoding='utf-8') as jsonfile:
json.dump(movies, jsonfile, ensure_ascii=False, indent=4)
print(f"Data saved to '{json_file_name}'.")

# Exception catching in the request
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except Exception as err:
print(f"Other error occurred: {err}")
51 changes: 51 additions & 0 deletions N.Sunil-Easy/Task 2/nltk_chatbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Basic Chatbot with NLTK
# Required Modules
import nltk
from nltk.chat.util import Chat, reflections

# Pairs:Reflections
pairs = [
[
r"hi|hello|hey",
["Hello!", "Hi there!", "Hey!"]
],
[
r"how are you?",
["I'm good, thank you! How about you?", "Doing well, how are you?"]
],
[
r"what is your name?",
["I'm a chatbot created using NLTK.", "You can call me NLTKBot."]
],
[
r"what can you do?",
["I can chat with you and answer some basic questions.", "I'm here to talk and help you with basic queries."]
],
[
r"how does natural language processing work?",
["Natural Language Processing (NLP) is a field of artificial intelligence that focuses on the interaction between computers and humans through natural language.",
"NLP combines computational linguistics with statistical, machine learning, and deep learning models to process and understand human language."]
],
[
r"(.*) your favorite (.*)?",
["I'm just a bot, but I think everything is interesting!", "I don't have preferences, but I enjoy learning new things!"]
],
[
r"quit",
["Bye for now. See you soon!", "Goodbye! It was nice talking to you."]
],
[
r"(.*)",
["I'm sorry, I don't understand that. Can you please rephrase?", "Can you please clarify your question?"]
]
]

# Converse ChatBot
def chatbot():
print("Hi! I'm a chatbot created using NLTK. Type 'quit' to exit.")
chat = Chat(pairs, reflections)
chat.converse()

# Start ChatBot
if __name__ == "__main__":
chatbot()
Binary file added N.Sunil-Easy/Task 3/db.sqlite
Binary file not shown.
160 changes: 160 additions & 0 deletions N.Sunil-Easy/Task 3/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/* General Styles */
body {
font-family: 'Roboto', sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}

.container {
max-width: 800px;
margin: 50px auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
border-radius: 8px;
transition: transform 0.2s ease-in-out;
}

.container:hover {
transform: scale(1.02);
}

h1 {
text-align: center;
color: #cc0000;
font-size: 2.5em;
margin-bottom: 20px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

/* Form Styles */
.form-container {
display: flex;
justify-content: center;
margin-bottom: 20px;
}

.form-group {
display: flex;
align-items: center;
}

.thick {
font-weight: bold;
font-size: 1.2em;
margin-right: 10px;
}

.input-task {
width: 60%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
margin-right: 10px;
transition: border-color 0.3s;
}

.input-task:focus {
border-color: #007bff;
box-shadow: 0 0 8px rgba(0, 123, 255, 0.3);
}

/* Button Styles */
.button1, .button2, .button3 {
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
text-decoration: none;
color: #fff;
transition: background-color 0.3s, box-shadow 0.3s;
}

.button1 {
background-color: #28a745;
font-weight: bold;
font-size: 16px;
}

.button1:hover {
background-color: #218838;
box-shadow: 0 4px 8px rgba(40, 167, 69, 0.3);
}

.button2 {
background-color: #007bff;
font-weight: bold;
line-height: 25px;
}

.button2:hover {
background-color: #0056b3;
box-shadow: 0 4px 8px rgba(0, 123, 255, 0.3);
}

.button3 {
background-color: #dc3545;
font-weight: bold;
line-height: 25px;
}

.button3:hover {
background-color: #c82333;
box-shadow: 0 4px 8px rgba(220, 53, 69, 0.3);
}

/* Label Styles */
.label {
color: white;
padding: 8px;
font-family: Arial, sans-serif;
border-radius: 4px;
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
}

.done {
background-color: #28a745;
}

.pending {
background-color: #ffc107;
}

/* Table Styles */
.task-table {
width: 100%;
border-collapse: collapse;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}

.task-table th, .task-table td {
padding: 15px;
text-align: left;
border-bottom: 1px solid #ddd;
}

.task-table th {
background-color: #f8f9fa;
font-size: 1.2em;
font-weight: bold;
}

.task-table tr:hover {
background-color: #f1f1f1;
}

/* Additional Styles */
a:link {
text-decoration: none;
color: inherit;
}

div {
text-align: center;
}

#first {
margin-top: 50px;
}
44 changes: 44 additions & 0 deletions N.Sunil-Easy/Task 3/task_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

class Todo(db.Model):
task_id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
done = db.Column(db.Boolean)

@app.route('/')
def home():
todo_list = Todo.query.all()
return render_template('base.html', todo_list=todo_list)

@app.route('/add', methods=['POST'])
def add():
name = request.form.get("name")
new_task = Todo(name=name, done=False)
db.session.add(new_task)
db.session.commit()
return redirect(url_for("home"))

@app.route('/update/<int:todo_id>')
def update(todo_id):
todo = Todo.query.get(todo_id)
todo.done = not todo.done
db.session.commit()
return redirect(url_for("home"))

@app.route('/delete/<int:todo_id>')
def delete(todo_id):
todo = Todo.query.get(todo_id)
db.session.delete(todo)
db.session.commit()
return redirect(url_for("home"))

if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run()
47 changes: 47 additions & 0 deletions N.Sunil-Easy/Task 3/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Todo App</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css')}}">
</head>
<body>
<div id="first" class="container">
<h1>To Do List</h1>
<form action="/add" method="post" class="form-container">
<div class="form-group">
<label for="task" class="thick">Enter the task</label>
<input type="text" id="task" name="name" class="input-task">
<button class="button1" type="submit">Add</button>
</div>
</form>
<br><br>
<table class="task-table">
<thead>
<tr>
<th>Task ID</th>
<th>Task Name</th>
<th>Status</th>
<th>Update</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{% for todo in todo_list %}
<tr>
<td class="thick">{{ todo.task_id }}</td>
<td class="thick">{{ todo.name }}</td>
{% if todo.done == False %}
<td><label class="label pending">Not done</label></td>
{% else %}
<td><label class="label done">Done</label></td>
{% endif %}
<td><a href="/update/{{ todo.task_id }}" class="button2">Update</a></td>
<td><a href="/delete/{{ todo.task_id }}" class="button3">Delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
Loading