diff --git a/Eight_Puzzle_Solver/eight_puzzle.py b/Eight_Puzzle_Solver/eight_puzzle.py index a812a26d8f2..50ecef1d5ca 100644 --- a/Eight_Puzzle_Solver/eight_puzzle.py +++ b/Eight_Puzzle_Solver/eight_puzzle.py @@ -1,9 +1,9 @@ -import sys +# import sys from collections import deque from copy import deepcopy from queue import PriorityQueue -import time -from collections import Counter +# import time +# from collections import Counter class Node: def __init__(self,state,depth = 0,moves = None,optimizer=0): diff --git a/Face and eye Recognition/face_recofnation_first.py b/Face and eye Recognition/face_recofnation_first.py index 9a8c26c0ffd..8c61c3b0f07 100644 --- a/Face and eye Recognition/face_recofnation_first.py +++ b/Face and eye Recognition/face_recofnation_first.py @@ -1,34 +1,37 @@ -## Name - Soumyajit Chakraborty +## Name - Soumyajit Chakraborty ## place - kolkata ## date - 10 / 08 / 2020 import cv2 as cv -face_cascade = cv.CascadeClassifier('..\libs\haarcascade_frontalface_default.xml') -face_cascade_eye = cv.CascadeClassifier('..\libs\haarcascade_eye.xml') -#face_glass = cv.CascadeClassifier('..\libs\haarcascade_eye_tree_eyeglasses.xml') + +face_cascade = cv.CascadeClassifier("..\libs\haarcascade_frontalface_default.xml") +face_cascade_eye = cv.CascadeClassifier("..\libs\haarcascade_eye.xml") +# face_glass = cv.CascadeClassifier('..\libs\haarcascade_eye_tree_eyeglasses.xml') cap = cv.VideoCapture(0) -while(cap.isOpened()): +while cap.isOpened(): - falg ,img = cap.read() #start reading the camera output i mean frames + falg, img = cap.read() # start reading the camera output i mean frames # cap.read() returning a bool value and a frame onject type value - gray = cv.cvtColor(img , cv.COLOR_BGR2GRAY) # converting to grayscale image to perform smoother - faces = face_cascade.detectMultiScale(img , 1.1, 7) #we use detectMultiscale library function to detect the predefined structures of a face - eyes = face_cascade_eye.detectMultiScale(img , 1.1 , 7) + gray = cv.cvtColor( + img, cv.COLOR_BGR2GRAY + ) # converting to grayscale image to perform smoother + faces = face_cascade.detectMultiScale( + img, 1.1, 7 + ) # we use detectMultiscale library function to detect the predefined structures of a face + eyes = face_cascade_eye.detectMultiScale(img, 1.1, 7) # using for loops we are trying to read each and every frame and map - for(x , y ,w ,h ) in faces: - cv.rectangle(img , (x , y) , (x+w , y+h) , (0 , 255 , 0) , 1) + for (x, y, w, h) in faces: + cv.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1) - for(a , b , c , d) in eyes: - cv.rectangle(img, (a , b), (a+c, b+d), (255, 0, 0), 1) + for (a, b, c, d) in eyes: + cv.rectangle(img, (a, b), (a + c, b + d), (255, 0, 0), 1) - cv.imshow('img' , img ) + cv.imshow("img", img) c = cv.waitKey(1) - if c == ord('q'): + if c == ord("q"): break cv.release() cv.destroyAllWindows() - - diff --git a/Face and eye Recognition/gesture_control.py b/Face and eye Recognition/gesture_control.py index b5b3fbe6061..db15e1849f1 100644 --- a/Face and eye Recognition/gesture_control.py +++ b/Face and eye Recognition/gesture_control.py @@ -1,17 +1,18 @@ import cv2 as cv + # import numpy as np -img = cv.imread('..\img\hand1.jpg' , 0) -flag,frame = cv.threshold(img , 70 , 255 , cv.THRESH_BINARY) +img = cv.imread("..\img\hand1.jpg", 0) +flag, frame = cv.threshold(img, 70, 255, cv.THRESH_BINARY) -contor,_ = cv.findContours(frame.copy(),cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE) +contor, _ = cv.findContours(frame.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) hull = [cv.convexHull(c) for c in contor] -final = cv.drawContours(img , hull , -1 , (0 , 0 , 0) ) -cv.imshow('original_image' , img) -cv.imshow('thres' , frame) -cv.imshow('final_hsv' , final) +final = cv.drawContours(img, hull, -1, (0, 0, 0)) +cv.imshow("original_image", img) +cv.imshow("thres", frame) +cv.imshow("final_hsv", final) cv.waitKey(0) -cv.destroyAllWindows() \ No newline at end of file +cv.destroyAllWindows() diff --git a/Hotel-Management.py b/Hotel-Management.py index 874d1913543..b8e4cfcbfeb 100644 --- a/Hotel-Management.py +++ b/Hotel-Management.py @@ -1,5 +1,3 @@ - - def menu(): print("") print("") @@ -15,36 +13,37 @@ def menu(): print("6-Exit the program") print("") - user_input=int(input("Enter your choice(1-6): ")) + user_input = int(input("Enter your choice(1-6): ")) - if user_input==1: + if user_input == 1: add() - elif user_input==2: + elif user_input == 2: modify() - elif user_input==3: + elif user_input == 3: search() - elif user_input==4: + elif user_input == 4: view() - elif user_input==5: + elif user_input == 5: remove() - elif user_input==6: + elif user_input == 6: exit() + def add(): print("") - Name1=input("Enter your first name: ") + Name1 = input("Enter your first name: ") print("") - Name2=input("Enter your last name: ") + Name2 = input("Enter your last name: ") print("") - Phone_Num=input("Enter your phone number(without +91): ") + Phone_Num = input("Enter your phone number(without +91): ") print("") print("These are the rooms that are currently available") @@ -53,32 +52,31 @@ def add(): print("3-Super Deluxe (1500/Day)") print("4-Premium Deluxe (2000/Day)") print("") - Room_Type=int(input("Which type you want(1-4): ")) + Room_Type = int(input("Which type you want(1-4): ")) print("") - if Room_Type==1: - x=500 - Room_Type="Normal" - elif Room_Type==2: - x=1000 - Room_Type='Deluxe' - elif Room_Type==3: - x=1500 - Room_Type='Super Deluxe' - elif Room_Type==4: - x=2000 - Room_Type='Premium' - - Days=int(input("How many days you will stay: ")) - Money=x*Days - Money=str(Money) + if Room_Type == 1: + x = 500 + Room_Type = "Normal" + elif Room_Type == 2: + x = 1000 + Room_Type = "Deluxe" + elif Room_Type == 3: + x = 1500 + Room_Type = "Super Deluxe" + elif Room_Type == 4: + x = 2000 + Room_Type = "Premium" + + Days = int(input("How many days you will stay: ")) + Money = x * Days + Money = str(Money) print("") - print("You have to pay ",(Money)) + print("You have to pay ", (Money)) print("") - - Payment=input("Mode of payment(Card/Cash/Online): ").capitalize() + Payment = input("Mode of payment(Card/Cash/Online): ").capitalize() if Payment == "Card": print("Payment with card") elif Payment == "Cash": @@ -87,38 +85,36 @@ def add(): print("Online payment") print("") - - File=open('Management.txt','r') - string=File.read() - string = string.replace("\'", "\"") - dictionary=json.loads(string) + File = open("Management.txt", "r") + string = File.read() + string = string.replace("'", '"') + dictionary = json.loads(string) File.close() - - if len(dictionary.get('Room'))==0: - Room_num='501' + if len(dictionary.get("Room")) == 0: + Room_num = "501" else: - listt=dictionary.get('Room') - tempp=len(listt)-1 - temppp=int(listt[tempp]) - Room_num=(1+temppp) - Room_num=str(Room_num) + listt = dictionary.get("Room") + tempp = len(listt) - 1 + temppp = int(listt[tempp]) + Room_num = 1 + temppp + Room_num = str(Room_num) - print('You have been assigned Room Number',Room_num) + print("You have been assigned Room Number", Room_num) print(f"name : {Name1} {Name2}") print(f"phone number : +91{Phone_Num}") print(f"Room type : {Room_Type}") print(f"Stay (day) : {Days}") - dictionary['First_Name'].append(Name1) - dictionary['Last_Name'].append(Name2) - dictionary['Phone_num'].append(Phone_Num) - dictionary['Room_Type'].append(Room_Type) - dictionary['Days'].append(Days) - dictionary['Price'].append(Money) - dictionary['Room'].append(Room_num) + dictionary["First_Name"].append(Name1) + dictionary["Last_Name"].append(Name2) + dictionary["Phone_num"].append(Phone_Num) + dictionary["Room_Type"].append(Room_Type) + dictionary["Days"].append(Days) + dictionary["Price"].append(Money) + dictionary["Room"].append(Room_num) - File=open("Management.txt",'w',encoding="utf-8") + File = open("Management.txt", "w", encoding="utf-8") File.write(str(dictionary)) File.close() @@ -128,39 +124,46 @@ def add(): exit_menu() - import os import json -filecheck = os.path.isfile('Management.txt') -if filecheck == False : - File = open("Management.txt", 'a', encoding="utf-8") - temp1 = {'First_Name': [], 'Last_Name': [], 'Phone_num': [], 'Room_Type': [], 'Days': [], 'Price': [], 'Room':[]} + +filecheck = os.path.isfile("Management.txt") +if filecheck == False: + File = open("Management.txt", "a", encoding="utf-8") + temp1 = { + "First_Name": [], + "Last_Name": [], + "Phone_num": [], + "Room_Type": [], + "Days": [], + "Price": [], + "Room": [], + } File.write(str(temp1)) File.close() - def modify(): - File=open('Management.txt','r') - string=File.read() - string = string.replace("\'", "\"") - dictionary=json.loads(string) + File = open("Management.txt", "r") + string = File.read() + string = string.replace("'", '"') + dictionary = json.loads(string) File.close() - dict_num=dictionary.get("Room") - dict_len=len(dict_num) - if dict_len==0: + dict_num = dictionary.get("Room") + dict_len = len(dict_num) + if dict_len == 0: print("") print("There is no data in our database") print("") menu() else: print("") - Room=(input("Enter your Room Number: ")) + Room = input("Enter your Room Number: ") - listt=dictionary['Room'] - index=int(listt.index(Room)) + listt = dictionary["Room"] + index = int(listt.index(Room)) print("") print("1-Change your first name") @@ -168,35 +171,35 @@ def modify(): print("3-Change your phone number") print("") - choice=(input("Enter your choice: ")) + choice = input("Enter your choice: ") print("") - File=open("Management.txt",'w',encoding="utf-8") + File = open("Management.txt", "w", encoding="utf-8") if choice == str(1): - user_input=input('Enter New First Name: ') - listt1=dictionary['First_Name'] - listt1[index]=user_input - dictionary['First_Name']=None - dictionary['First_Name']=listt1 + user_input = input("Enter New First Name: ") + listt1 = dictionary["First_Name"] + listt1[index] = user_input + dictionary["First_Name"] = None + dictionary["First_Name"] = listt1 File.write(str(dictionary)) File.close() elif choice == str(2): - user_input = input('Enter New Last Name: ') - listt1 = dictionary['Last_Name'] + user_input = input("Enter New Last Name: ") + listt1 = dictionary["Last_Name"] listt1[index] = user_input - dictionary['Last_Name'] = None - dictionary['Last_Name'] = listt1 + dictionary["Last_Name"] = None + dictionary["Last_Name"] = listt1 File.write(str(dictionary)) File.close() elif choice == str(3): - user_input = input('Enter New Phone Number: ') - listt1 = dictionary['Phone_num'] + user_input = input("Enter New Phone Number: ") + listt1 = dictionary["Phone_num"] listt1[index] = user_input - dictionary['Phone_num'] = None - dictionary['Phone_num'] = listt1 + dictionary["Phone_num"] = None + dictionary["Phone_num"] = listt1 File.write(str(dictionary)) File.close() @@ -205,77 +208,79 @@ def modify(): exit_menu() + def search(): - File=open('Management.txt','r') - string=File.read() - string = string.replace("\'", "\"") - dictionary=json.loads(string) + File = open("Management.txt", "r") + string = File.read() + string = string.replace("'", '"') + dictionary = json.loads(string) File.close() - dict_num=dictionary.get("Room") - dict_len=len(dict_num) - if dict_len==0: + dict_num = dictionary.get("Room") + dict_len = len(dict_num) + if dict_len == 0: print("") print("There is no data in our database") print("") menu() else: print("") - Room = (input("Enter your Room Number: ")) + Room = input("Enter your Room Number: ") print("") - listt = dictionary['Room'] + listt = dictionary["Room"] index = int(listt.index(Room)) - listt_fname=dictionary.get('First_Name') - listt_lname=dictionary.get('Last_Name') - listt_phone=dictionary.get('Phone_num') - listt_type=dictionary.get('Room_Type') - listt_days=dictionary.get('Days') - listt_price=dictionary.get('Price') - listt_num=dictionary.get('Room') + listt_fname = dictionary.get("First_Name") + listt_lname = dictionary.get("Last_Name") + listt_phone = dictionary.get("Phone_num") + listt_type = dictionary.get("Room_Type") + listt_days = dictionary.get("Days") + listt_price = dictionary.get("Price") + listt_num = dictionary.get("Room") print("") - print("First Name:",listt_fname[index]) - print("Last Name:",listt_lname[index]) - print("Phone number:",listt_phone[index]) - print("Room Type:",listt_type[index]) - print('Days staying:',listt_days[index]) - print('Money paid:',listt_price[index]) - print('Room Number:',listt_num[index]) + print("First Name:", listt_fname[index]) + print("Last Name:", listt_lname[index]) + print("Phone number:", listt_phone[index]) + print("Room Type:", listt_type[index]) + print("Days staying:", listt_days[index]) + print("Money paid:", listt_price[index]) + print("Room Number:", listt_num[index]) exit_menu() + def remove(): - File=open('Management.txt','r') - string=File.read() - string = string.replace("\'", "\"") - dictionary=json.loads(string) + File = open("Management.txt", "r") + string = File.read() + string = string.replace("'", '"') + dictionary = json.loads(string) File.close() - dict_num=dictionary.get("Room") - dict_len=len(dict_num) - if dict_len==0: + dict_num = dictionary.get("Room") + dict_len = len(dict_num) + if dict_len == 0: print("") print("There is no data in our database") print("") menu() else: print("") - Room = (input("Enter your Room Number: ")) + Room = input("Enter your Room Number: ") print("") - listt = dictionary['Room'] + listt = dictionary["Room"] index = int(listt.index(Room)) - listt_fname = dictionary.get('First_Name') - listt_lname = dictionary.get('Last_Name') - listt_phone = dictionary.get('Phone_num') - listt_type = dictionary.get('Room_Type') - listt_days = dictionary.get('Days') - listt_price = dictionary.get('Price') - listt_num = dictionary.get('Room') + listt_fname = dictionary.get("First_Name") + listt_lname = dictionary.get("Last_Name") + listt_phone = dictionary.get("Phone_num") + listt_type = dictionary.get("Room_Type") + listt_days = dictionary.get("Days") + listt_price = dictionary.get("Price") + listt_num = dictionary.get("Room") del listt_fname[index] del listt_lname[index] @@ -285,28 +290,28 @@ def remove(): del listt_price[index] del listt_num[index] - dictionary['First_Name'] = None - dictionary['First_Name'] = listt_fname + dictionary["First_Name"] = None + dictionary["First_Name"] = listt_fname - dictionary['Last_Name']= None - dictionary['Last_Name']= listt_lname + dictionary["Last_Name"] = None + dictionary["Last_Name"] = listt_lname - dictionary['Phone_num']= None - dictionary['Phone_num']=listt_phone + dictionary["Phone_num"] = None + dictionary["Phone_num"] = listt_phone - dictionary['Room_Type']=None - dictionary['Room_Type']=listt_type + dictionary["Room_Type"] = None + dictionary["Room_Type"] = listt_type - dictionary['Days']=None - dictionary['Days']=listt_days + dictionary["Days"] = None + dictionary["Days"] = listt_days - dictionary['Price']=None - dictionary['Price']=listt_price + dictionary["Price"] = None + dictionary["Price"] = listt_price - dictionary['Room']=None - dictionary['Room']=listt_num + dictionary["Room"] = None + dictionary["Room"] = listt_num - file1=open('Management.txt','w',encoding="utf-8") + file1 = open("Management.txt", "w", encoding="utf-8") file1.write(str(dictionary)) file1.close() @@ -314,55 +319,58 @@ def remove(): exit_menu() + def view(): - File=open('Management.txt','r') - string=File.read() - string = string.replace("\'", "\"") - dictionary=json.loads(string) + File = open("Management.txt", "r") + string = File.read() + string = string.replace("'", '"') + dictionary = json.loads(string) File.close() - dict_num=dictionary.get("Room") - dict_len=len(dict_num) - if dict_len==0: + dict_num = dictionary.get("Room") + dict_len = len(dict_num) + if dict_len == 0: print("") print("There is no data in our database") print("") menu() else: - listt = dictionary['Room'] + listt = dictionary["Room"] a = len(listt) - index=0 - while index!=a: - listt_fname = dictionary.get('First_Name') - listt_lname = dictionary.get('Last_Name') - listt_phone = dictionary.get('Phone_num') - listt_type = dictionary.get('Room_Type') - listt_days = dictionary.get('Days') - listt_price = dictionary.get('Price') - listt_num = dictionary.get('Room') + index = 0 + while index != a: + listt_fname = dictionary.get("First_Name") + listt_lname = dictionary.get("Last_Name") + listt_phone = dictionary.get("Phone_num") + listt_type = dictionary.get("Room_Type") + listt_days = dictionary.get("Days") + listt_price = dictionary.get("Price") + listt_num = dictionary.get("Room") print("") print("First Name:", listt_fname[index]) print("Last Name:", listt_lname[index]) print("Phone number:", listt_phone[index]) print("Room Type:", listt_type[index]) - print('Days staying:', listt_days[index]) - print('Money paid:', listt_price[index]) - print('Room Number:', listt_num[index]) + print("Days staying:", listt_days[index]) + print("Money paid:", listt_price[index]) + print("Room Number:", listt_num[index]) print("") - index=index+1 + index = index + 1 exit_menu() + def exit(): print("") - print(' Thanks for visiting') + print(" Thanks for visiting") print(" Goodbye") + def exit_menu(): print("") print("Do you want to exit the program or return to main menu") @@ -370,14 +378,16 @@ def exit_menu(): print("2-Exit") print("") - user_input=int(input("Enter your choice: ")) - if user_input==2: + user_input = int(input("Enter your choice: ")) + if user_input == 2: exit() - elif user_input==1: + elif user_input == 1: menu() + + try: menu() except KeyboardInterrupt as exit: print("\nexiting...!") - -# menu() \ No newline at end of file + +# menu() diff --git a/JustDialScrapperGUI/Justdial Scrapper GUI.py b/JustDialScrapperGUI/Justdial Scrapper GUI.py index 6b0bd7cea70..2dd4803f0bb 100644 --- a/JustDialScrapperGUI/Justdial Scrapper GUI.py +++ b/JustDialScrapperGUI/Justdial Scrapper GUI.py @@ -1,10 +1,10 @@ -from tkinter import Tk, Entry, Label, Button, HORIZONTAL +import csv +import threading +import urllib.request +from tkinter import HORIZONTAL, Button, Entry, Label, Tk from tkinter.ttk import Progressbar -from bs4 import BeautifulSoup -import urllib.request -import threading -import csv +from bs4 import BeautifulSoup class ScrapperLogic: @@ -21,80 +21,81 @@ def inner_html(element): @staticmethod def get_name(body): - return body.find('span', {'class': 'jcn'}).a.string + return body.find("span", {"class": "jcn"}).a.string @staticmethod def which_digit(html): - mapping_dict = {'icon-ji': 9, - 'icon-dc': '+', - 'icon-fe': '(', - 'icon-hg': ')', - 'icon-ba': '-', - 'icon-lk': 8, - 'icon-nm': 7, - 'icon-po': 6, - 'icon-rq': 5, - 'icon-ts': 4, - 'icon-vu': 3, - 'icon-wx': 2, - 'icon-yz': 1, - 'icon-acb': 0, - } - return mapping_dict.get(html, '') + mapping_dict = { + "icon-ji": 9, + "icon-dc": "+", + "icon-fe": "(", + "icon-hg": ")", + "icon-ba": "-", + "icon-lk": 8, + "icon-nm": 7, + "icon-po": 6, + "icon-rq": 5, + "icon-ts": 4, + "icon-vu": 3, + "icon-wx": 2, + "icon-yz": 1, + "icon-acb": 0, + } + return mapping_dict.get(html, "") def get_phone_number(self, body): i = 0 phone_no = "No Number!" try: - for item in body.find('p', {'class': 'contact-info'}): + for item in body.find("p", {"class": "contact-info"}): i += 1 if i == 2: - phone_no = '' + phone_no = "" try: for element in item.find_all(class_=True): classes = [] classes.extend(element["class"]) phone_no += str((self.which_digit(classes[1]))) - except: + except Exception: pass - except: + except Exception: pass - body = body['data-href'] - soup = BeautifulSoup(body, 'html.parser') - for a in soup.find_all('a', {"id": "whatsapptriggeer"}): + body = body["data-href"] + soup = BeautifulSoup(body, "html.parser") + for a in soup.find_all("a", {"id": "whatsapptriggeer"}): # print (a) - phone_no = str(a['href'][-10:]) + phone_no = str(a["href"][-10:]) return phone_no @staticmethod def get_rating(body): rating = 0.0 - text = body.find('span', {'class': 'star_m'}) + text = body.find("span", {"class": "star_m"}) if text is not None: for item in text: - rating += float(item['class'][0][1:]) / 10 + rating += float(item["class"][0][1:]) / 10 return rating @staticmethod def get_rating_count(body): - text = body.find('span', {'class': 'rt_count'}).string + text = body.find("span", {"class": "rt_count"}).string # Get only digits - rating_count = ''.join(i for i in text if i.isdigit()) - return rating_count - + rating_count = "".join(i for i in text if i.isdigit()) + return rating_count + @staticmethod def get_address(body): - return body.find('span', {'class': 'mrehover'}).text.strip() + return body.find("span", {"class": "mrehover"}).text.strip() @staticmethod def get_location(body): - text = body.find('a', {'class': 'rsmap'}) + text = body.find("a", {"class": "rsmap"}) if not text: return - text_list = text['onclick'].split(",") + text_list = text["onclick"].split(",") latitude = text_list[3].strip().replace("'", "") longitude = text_list[4].strip().replace("'", "") @@ -107,44 +108,48 @@ def start_scrapping_logic(self): total_url = "https://www.justdial.com/%7B0%7D/%7B1%7D".format(self.location, self.query) - fields = ['Name', 'Phone', 'Rating', 'Rating Count', 'Address', 'Location'] - out_file = open('{0}.csv'.format(self.file_name), 'w') - csvwriter = csv.DictWriter(out_file, delimiter=',', fieldnames=fields) - csvwriter.writerow({ - 'Name': 'Name', #Shows the name - 'Phone': 'Phone',#shows the phone - 'Rating': 'Rating',#shows the ratings - 'Rating Count': 'Rating Count',#Shows the stars for ex: 4 stars - 'Address': 'Address',#Shows the address of the place - 'Location': 'Location'#shows the location - }) + fields = ["Name", "Phone", "Rating", "Rating Count", "Address", "Location"] + out_file = open("{0}.csv".format(self.file_name), "w") + csvwriter = csv.DictWriter(out_file, delimiter=",", fieldnames=fields) + csvwriter.writerow( + { + "Name": "Name", # Shows the name + "Phone": "Phone", # shows the phone + "Rating": "Rating", # shows the ratings + "Rating Count": "Rating Count", # Shows the stars for ex: 4 stars + "Address": "Address", # Shows the address of the place + "Location": "Location", # shows the location + } + ) progress_value = 0 while True: # Check if reached end of result if page_number > 50: progress_value = 100 - self.progressbar['value'] = progress_value + self.progressbar["value"] = progress_value break if progress_value != 0: progress_value += 1 - self.label_progress['text'] = "{0}{1}".format(progress_value, '%') - self.progressbar['value'] = progress_value + self.label_progress["text"] = "{0}{1}".format(progress_value, "%") + self.progressbar["value"] = progress_value url = total_url + "/page-%s" % page_number print("{0} {1}, {2}".format("Scrapping page number: ", page_number, url)) - req = urllib.request.Request(url, headers={'User-Agent': "Mozilla/5.0 (Windows NT 6.1; Win64; x64)"}) + req = urllib.request.Request( + url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64)"} + ) page = urllib.request.urlopen(req) soup = BeautifulSoup(page.read(), "html.parser") - services = soup.find_all('li', {'class': 'cntanr'}) + services = soup.find_all("li", {"class": "cntanr"}) # Iterate through the 10 results in the page progress_value += 1 - self.label_progress['text'] = "{0}{1}".format(progress_value, '%') - self.progressbar['value'] = progress_value + self.label_progress["text"] = "{0}{1}".format(progress_value, "%") + self.progressbar["value"] = progress_value for service_html in services: try: @@ -158,18 +163,18 @@ def start_scrapping_logic(self): address = self.get_address(service_html) location = self.get_location(service_html) if name is not None: - dict_service['Name'] = name + dict_service["Name"] = name if phone is not None: - print('getting phone number') - dict_service['Phone'] = phone + print("getting phone number") + dict_service["Phone"] = phone if rating is not None: - dict_service['Rating'] = rating + dict_service["Rating"] = rating if count is not None: - dict_service['Rating Count'] = count + dict_service["Rating Count"] = count if address is not None: - dict_service['Address'] = address + dict_service["Address"] = address if location is not None: - dict_service['Address'] = location + dict_service["Address"] = location # Write row to CSV csvwriter.writerow(dict_service) @@ -207,42 +212,50 @@ def start_scrapping(self): query = self.entry_query.get() location = self.entry_location.get() file_name = self.entry_file_name.get() - scrapper = ScrapperLogic(query, location, file_name, self.progress, self.label_progress) + scrapper = ScrapperLogic( + query, location, file_name, self.progress, self.label_progress + ) t1 = threading.Thread(target=scrapper.start_scrapping_logic, args=[]) t1.start() def start(self): - self.label_query = Label(self.master, text='Query') + self.label_query = Label(self.master, text="Query") self.label_query.grid(row=0, column=0) self.entry_query = Entry(self.master, width=23) self.entry_query.grid(row=0, column=1) - self.label_location = Label(self.master, text='Location') + self.label_location = Label(self.master, text="Location") self.label_location.grid(row=1, column=0) self.entry_location = Entry(self.master, width=23) self.entry_location.grid(row=1, column=1) - self.label_file_name = Label(self.master, text='File Name') + self.label_file_name = Label(self.master, text="File Name") self.label_file_name.grid(row=2, column=0) self.entry_file_name = Entry(self.master, width=23) self.entry_file_name.grid(row=2, column=1) - self.label_progress = Label(self.master, text='0%') + self.label_progress = Label(self.master, text="0%") self.label_progress.grid(row=3, column=0) - self.button_start = Button(self.master, text="Start", command=self.start_scrapping) + self.button_start = Button( + self.master, text="Start", command=self.start_scrapping + ) self.button_start.grid(row=3, column=1) - self.progress = Progressbar(self.master, orient=HORIZONTAL, length=350, mode='determinate') + self.progress = Progressbar( + self.master, orient=HORIZONTAL, length=350, mode="determinate" + ) self.progress.grid(row=4, columnspan=2) - #Above is the progress bar -if __name__ == '__main__': + # Above is the progress bar + + +if __name__ == "__main__": root = Tk() - root.geometry('350x130+600+100') + root.geometry("350x130+600+100") root.title("Just Dial Scrapper - Cool") JDScrapperGUI(root).start() root.mainloop() diff --git a/Python_chatting_application/client.py b/Python_chatting_application/client.py index 75f18b046a8..447e7b6c448 100644 --- a/Python_chatting_application/client.py +++ b/Python_chatting_application/client.py @@ -2,40 +2,44 @@ import threading flag = 0 -s = socket.socket(socket.AF_INET , socket.SOCK_STREAM) +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) hostname = input("Enter your host :: ") -s.connect((hostname , 1023)) +s.connect((hostname, 1023)) nickname = input("Enter your Name :: ") + def recieve(): while True: try: msg = s.recv(1024).decode("utf-8") - if msg == 'NICK': - print("Welcome to Chat room :: " , nickname) - s.send(bytes(nickname , "utf-8")) + if msg == "NICK": + print("Welcome to Chat room :: ", nickname) + s.send(bytes(nickname, "utf-8")) else: print(msg) - except: - print("An Erro occured ") + except Exception as error: + print(f"An Erro occured {error}") s.close() flag = 1 break + def Write(): while True: try: reply_msg = f"{nickname} :: {input()}" - s.send(bytes(reply_msg , "utf-8")) - except: - print("An Error Occured while sending message !!!") + s.send(bytes(reply_msg, "utf-8")) + except Exception as error: + print(f"An Error Occured while sending message !!!\n error : {error}") s.close() flag = 1 break + + if flag == 1: exit() recieve_thrd = threading.Thread(target=recieve) recieve_thrd.start() write_thrd = threading.Thread(target=Write) -write_thrd.start() \ No newline at end of file +write_thrd.start() diff --git a/Python_chatting_application/server.py b/Python_chatting_application/server.py index 4c2e417d63e..1ff9141e9dd 100644 --- a/Python_chatting_application/server.py +++ b/Python_chatting_application/server.py @@ -23,7 +23,7 @@ def Client_Handler(cli): print(f"Disconnected with f{nick}") break BroadCasating(reply) - except: + except Exception: index_of_cli = clients.index(cli) print(index_of_cli) nick = nickename[index_of_cli] @@ -38,6 +38,7 @@ def BroadCasating(msg): for client in clients: client.send(bytes(msg, "utf-8")) + def recieve(): while True: client_sckt, addr = s.accept() @@ -48,7 +49,8 @@ def recieve(): clients.append(client_sckt) print(f"{nick} has joined the chat room ") BroadCasating(f"{nick} has joined the chat room say hi !!!") - threading._start_new_thread(Client_Handler , (client_sckt , )) + threading._start_new_thread(Client_Handler, (client_sckt,)) + recieve() -s.close() \ No newline at end of file +s.close() diff --git a/flappyBird_pygame/flappy_bird.py b/flappyBird_pygame/flappy_bird.py index 633fa482eb5..34b3206b7e2 100644 --- a/flappyBird_pygame/flappy_bird.py +++ b/flappyBird_pygame/flappy_bird.py @@ -9,26 +9,25 @@ import math import os -import pygame -from pygame.locals import * -from random import randint from collections import deque +from random import randint - +import pygame +from pygame.locals import * FPS = 60 ANI_SPEED = 0.18 # pixels per millisecond -W_WIDTH = 284 * 2 # BG image size: 284x512 px; tiled twice +W_WIDTH = 284 * 2 # BG image size: 284x512 px; tiled twice W_HEIGHT = 512 class Bird(pygame.sprite.Sprite): - WIDTH = 32 # bird image width - HEIGHT = 32 # bird image height - DOWN_SPEED = 0.18 # pix per ms -y - UP_SPEED = 0.3 # pix per ms +y - UP_DURATION = 150 # time for which bird go up + WIDTH = 32 # bird image width + HEIGHT = 32 # bird image height + DOWN_SPEED = 0.18 # pix per ms -y + UP_SPEED = 0.3 # pix per ms +y + UP_DURATION = 150 # time for which bird go up def __init__(self, x, y, ms_to_up, images): @@ -42,9 +41,12 @@ def __init__(self, x, y, ms_to_up, images): def update(self, delta_frames=1): if self.ms_to_up > 0: - frac_climb_done = 1 - self.ms_to_up/Bird.UP_DURATION - self.y -= (Bird.UP_SPEED * frames_to_msec(delta_frames) * - (1 - math.cos(frac_climb_done * math.pi))) + frac_climb_done = 1 - self.ms_to_up / Bird.UP_DURATION + self.y -= ( + Bird.UP_SPEED + * frames_to_msec(delta_frames) + * (1 - math.cos(frac_climb_done * math.pi)) + ) self.ms_to_up -= frames_to_msec(delta_frames) else: self.y += Bird.DOWN_SPEED * frames_to_msec(delta_frames) @@ -73,7 +75,7 @@ def rect(self): class PipePair(pygame.sprite.Sprite): - WIDTH = 80 # width of pipe + WIDTH = 80 # width of pipe PIECE_HEIGHT = 32 ADD_INTERVAL = 3000 @@ -83,20 +85,22 @@ def __init__(self, pipe_end_img, pipe_body_img): self.score_counted = False self.image = pygame.Surface((PipePair.WIDTH, W_HEIGHT), SRCALPHA) - self.image.convert() # speeds up blitting + self.image.convert() # speeds up blitting self.image.fill((0, 0, 0, 0)) total_pipe_body_pieces = int( - (W_HEIGHT - # fill window from top to bottom - 3 * Bird.HEIGHT - # make room for bird to fit through - 3 * PipePair.PIECE_HEIGHT) / # 2 end pieces + 1 body piece - PipePair.PIECE_HEIGHT # to get number of pipe pieces + ( + W_HEIGHT + - 3 * Bird.HEIGHT # fill window from top to bottom + - 3 * PipePair.PIECE_HEIGHT # make room for bird to fit through + ) + / PipePair.PIECE_HEIGHT # 2 end pieces + 1 body piece # to get number of pipe pieces ) self.bottom_pieces = randint(1, total_pipe_body_pieces) self.top_pieces = total_pipe_body_pieces - self.bottom_pieces # bottom pipe for i in range(1, self.bottom_pieces + 1): - piece_pos = (0, W_HEIGHT - i*PipePair.PIECE_HEIGHT) + piece_pos = (0, W_HEIGHT - i * PipePair.PIECE_HEIGHT) self.image.blit(pipe_body_img, piece_pos) bottom_pipe_end_y = W_HEIGHT - self.bottom_height_px bottom_end_piece_pos = (0, bottom_pipe_end_y - PipePair.PIECE_HEIGHT) @@ -145,22 +149,22 @@ def collides_with(self, bird): def load_images(): - - def load_image(img_file_name): - file_name = os.path.join('.', 'images', img_file_name) + file_name = os.path.join(".", "images", img_file_name) img = pygame.image.load(file_name) img.convert() return img - return {'background': load_image('background.png'), - 'pipe-end': load_image('pipe_end.png'), - 'pipe-body': load_image('pipe_body.png'), - # images for animating the flapping bird -- animated GIFs are - # not supported in pygame - 'bird-wingup': load_image('bird_wing_up.png'), - 'bird-wingdown': load_image('bird_wing_down.png')} + return { + "background": load_image("background.png"), + "pipe-end": load_image("pipe_end.png"), + "pipe-body": load_image("pipe_body.png"), + # images for animating the flapping bird -- animated GIFs are + # not supported in pygame + "bird-wingup": load_image("bird_wing_up.png"), + "bird-wingdown": load_image("bird_wing_down.png"), + } def frames_to_msec(frames, fps=FPS): @@ -171,11 +175,13 @@ def frames_to_msec(frames, fps=FPS): def msec_to_frames(milliseconds, fps=FPS): return fps * milliseconds / 1000.0 -''' + + +""" def gameover(display, score): font = pygame.font.SysFont(None,55) text = font.render("Game Over! Score: {}".format(score),True,(255,0,0)) - display.blit(text, [150,250])''' + display.blit(text, [150,250])""" def main(): @@ -183,7 +189,7 @@ def main(): pygame.init() display_surface = pygame.display.set_mode((W_WIDTH, W_HEIGHT)) - pygame.display.set_caption('Flappy Bird by PMN') + pygame.display.set_caption("Flappy Bird by PMN") clock = pygame.time.Clock() score_font = pygame.font.SysFont(None, 32, bold=True) # default font @@ -191,8 +197,12 @@ def main(): # the bird stays in the same x position, so bird.x is a constant # center bird on screen - bird = Bird(50, int(W_HEIGHT/2 - Bird.HEIGHT/2), 2, - (images['bird-wingup'], images['bird-wingdown'])) + bird = Bird( + 50, + int(W_HEIGHT / 2 - Bird.HEIGHT / 2), + 2, + (images["bird-wingup"], images["bird-wingdown"]), + ) pipes = deque() @@ -205,7 +215,7 @@ def main(): # Handle this 'manually'. If we used pygame.time.set_timer(), # pipe addition would be messed up when paused. if not (paused or frame_clock % msec_to_frames(PipePair.ADD_INTERVAL)): - pp = PipePair(images['pipe-end'], images['pipe-body']) + pp = PipePair(images["pipe-end"], images["pipe-body"]) pipes.append(pp) for e in pygame.event.get(): @@ -214,8 +224,9 @@ def main(): break elif e.type == KEYUP and e.key in (K_PAUSE, K_p): paused = not paused - elif e.type == MOUSEBUTTONUP or (e.type == KEYUP and - e.key in (K_UP, K_RETURN, K_SPACE)): + elif e.type == MOUSEBUTTONUP or ( + e.type == KEYUP and e.key in (K_UP, K_RETURN, K_SPACE) + ): bird.ms_to_up = Bird.UP_DURATION if paused: @@ -227,7 +238,7 @@ def main(): done = True for x in (0, W_WIDTH / 2): - display_surface.blit(images['background'], (x, 0)) + display_surface.blit(images["background"], (x, 0)) while pipes and not pipes[0].visible: pipes.popleft() @@ -246,19 +257,19 @@ def main(): p.score_counted = True score_surface = score_font.render(str(score), True, (255, 255, 255)) - score_x = W_WIDTH/2 - score_surface.get_width()/2 + score_x = W_WIDTH / 2 - score_surface.get_width() / 2 display_surface.blit(score_surface, (score_x, PipePair.PIECE_HEIGHT)) pygame.display.flip() frame_clock += 1 - #gameover(display_surface, score) + # gameover(display_surface, score) - print('Game over! Score: %i' % score) + print("Game over! Score: %i" % score) pygame.quit() -if __name__ == '__main__': +if __name__ == "__main__": # If this module had been imported, __name__ would be 'flappybird'. # It was executed (e.g. by double-clicking the file), so call main. - main() \ No newline at end of file + main() diff --git a/news_articles__scraper.py b/news_articles__scraper.py index d8ced15e9d5..6c6360460cf 100644 --- a/news_articles__scraper.py +++ b/news_articles__scraper.py @@ -11,42 +11,44 @@ # ! pip install newspaper3k -# importing necessary libraries -from bs4 import BeautifulSoup -import requests -import urllib -import pandas as pd -from newspaper import Article import pickle import re import sys +import urllib + +import pandas as pd +import requests + +# importing necessary libraries +from bs4 import BeautifulSoup +from newspaper import Article # Extracting links for all the pages (1 to 158) of boomlive fake news section fakearticle_links = [] for i in range(1, 159): - url = 'https://www.boomlive.in/fake-news/' + str(i) - try: - # this might throw an exception if something goes wrong. - page=requests.get(url) - - # send requests - page = requests.get(url) - soup = BeautifulSoup(page.text, 'html.parser') - - # Collecting all the links in a list - for content in soup.find_all('h2', attrs={'class':'entry-title'}): - link = content.find('a') - fakearticle_links.append(link.get('href')) - - # this describes what to do if an exception is thrown - except Exception as e: - # get the exception information - error_type, error_obj, error_info = sys.exc_info() - #print the link that cause the problem - print ('ERROR FOR LINK:',url) - #print error info and line that threw the exception - print (error_type, 'Line:', error_info.tb_lineno) - continue + url = "https://www.boomlive.in/fake-news/" + str(i) + try: + # this might throw an exception if something goes wrong. + page = requests.get(url) + + # send requests + page = requests.get(url) + soup = BeautifulSoup(page.text, "html.parser") + + # Collecting all the links in a list + for content in soup.find_all("h2", attrs={"class": "entry-title"}): + link = content.find("a") + fakearticle_links.append(link.get("href")) + + # this describes what to do if an exception is thrown + except Exception as e: + # get the exception information + error_type, error_obj, error_info = sys.exc_info() + # print the link that cause the problem + print("ERROR FOR LINK:", url) + # print error info and line that threw the exception + print(error_type, "Line:", error_info.tb_lineno) + continue fakearticle_links[:5] @@ -54,9 +56,10 @@ fakearticle_links[1888:] +import matplotlib.pyplot as plt import pandas as pd + import numpy as np -import matplotlib.pyplot as plt """We have to modify the links so that the links actually work as we can see that the string extracted is the last part of the url! @@ -64,8 +67,8 @@ """ # Modify the links so that it takes us to the particular website -str1 = 'https://www.boomlive.in/fake-news' -fakearticle_links = [str1+lnk for lnk in fakearticle_links] +str1 = "https://www.boomlive.in/fake-news" +fakearticle_links = [str1 + lnk for lnk in fakearticle_links] fakearticle_links[6:10] @@ -75,30 +78,37 @@ """ # Create a dataset for storing the news articles -news_dataset = pd.DataFrame(fakearticle_links, columns=['URL']) +news_dataset = pd.DataFrame(fakearticle_links, columns=["URL"]) news_dataset.head() -title, text, summary, keywords, published_on, author = [], [], [], [], [], [] # Creating empty lists to store the data +title, text, summary, keywords, published_on, author = ( + [], + [], + [], + [], + [], + [], +) # Creating empty lists to store the data for Url in fakearticle_links: - article = Article(Url) - - #Call the download and parse methods to download information - try: - article.download() - article.parse() - article.nlp() - except Exception as error: - print(f"exception : {error}") - pass - - # Scrape the contents of article - title.append(article.title) # extracts the title of the article - text.append(article.text) # extracts the whole text of article - summary.append(article.summary) # gives us a summary abou the article - keywords.append(', '.join(article.keywords)) # the main keywords used in it - published_on.append(article.publish_date) # the date on which it was published - author.append(article.authors) # the authors of the article + article = Article(Url) + + # Call the download and parse methods to download information + try: + article.download() + article.parse() + article.nlp() + except Exception as error: + print(f"exception : {error}") + pass + + # Scrape the contents of article + title.append(article.title) # extracts the title of the article + text.append(article.text) # extracts the whole text of article + summary.append(article.summary) # gives us a summary abou the article + keywords.append(", ".join(article.keywords)) # the main keywords used in it + published_on.append(article.publish_date) # the date on which it was published + author.append(article.authors) # the authors of the article """**Checking the lists created**""" @@ -111,22 +121,22 @@ author[6] # Adding the columns in the fake news dataset -news_dataset['title'] = title -news_dataset['text'] = text -news_dataset['keywords'] = keywords -news_dataset['published date'] = published_on -news_dataset['author'] = author +news_dataset["title"] = title +news_dataset["text"] = text +news_dataset["keywords"] = keywords +news_dataset["published date"] = published_on +news_dataset["author"] = author # Check the first five columns of dataset created news_dataset.head() """**Converting the dataset to a csv file**""" -news_dataset.to_csv('Fake_news.csv') +news_dataset.to_csv("Fake_news.csv") """**Reading the csv file**""" -df = pd.read_csv('Fake_news.csv') +df = pd.read_csv("Fake_news.csv") # Checking the last 5 rows of the csv file df.tail(5) @@ -134,84 +144,95 @@ """**Download the csv file in local machine**""" from google.colab import files -files.download('Fake_news.csv') + +files.download("Fake_news.csv") """**Scraping news from Times of India**""" -TOIarticle_links = [] # Creating an empty list of all the urls of news from Times of India site +TOIarticle_links = ( + [] +) # Creating an empty list of all the urls of news from Times of India site # Extracting links for all the pages (2 to 125) of boomlive fake news section for i in range(2, 126): - url = 'https://timesofindia.indiatimes.com/news/' + str(i) - - try: - # send requests - page = requests.get(url) - soup = BeautifulSoup(page.text, 'html.parser') - - # Collecting all the links in a list - for content in soup.find_all('span', attrs={'class':'w_tle'}): - link = content.find('a') - TOIarticle_links.append(link.get('href')) - - # this describes what to do if an exception is thrown - except Exception as e: - # get the exception information - error_type, error_obj, error_info = sys.exc_info() - #print the link that cause the problem - print ('ERROR FOR LINK:',url) - #print error info and line that threw the exception - print (error_type, 'Line:', error_info.tb_lineno) - continue + url = "https://timesofindia.indiatimes.com/news/" + str(i) + + try: + # send requests + page = requests.get(url) + soup = BeautifulSoup(page.text, "html.parser") + + # Collecting all the links in a list + for content in soup.find_all("span", attrs={"class": "w_tle"}): + link = content.find("a") + TOIarticle_links.append(link.get("href")) + + # this describes what to do if an exception is thrown + except Exception as e: + # get the exception information + error_type, error_obj, error_info = sys.exc_info() + # print the link that cause the problem + print("ERROR FOR LINK:", url) + # print error info and line that threw the exception + print(error_type, "Line:", error_info.tb_lineno) + continue TOIarticle_links[6:15] len(TOIarticle_links) -str2 = 'https://timesofindia.indiatimes.com/' -TOIarticle_links = [str2+lnk for lnk in TOIarticle_links if lnk[0]=='/'] +str2 = "https://timesofindia.indiatimes.com/" +TOIarticle_links = [str2 + lnk for lnk in TOIarticle_links if lnk[0] == "/"] TOIarticle_links[5:8] len(TOIarticle_links) -title, text, summary, keywords, published_on, author = [], [], [], [], [], [] # Creating empty lists to store the data +title, text, summary, keywords, published_on, author = ( + [], + [], + [], + [], + [], + [], +) # Creating empty lists to store the data for Url in TOIarticle_links: - article = Article(Url) - - #Call the download and parse methods to download information - try: - article.download() - article.parse() - article.nlp() - except: - pass - - # Scrape the contents of article - title.append(article.title) # extracts the title of the article - text.append(article.text) # extracts the whole text of article - summary.append(article.summary) # gives us a summary abou the article - keywords.append(', '.join(article.keywords)) # the main keywords used in it - published_on.append(article.publish_date) # the date on which it was published - author.append(article.authors) # the authors of the article + article = Article(Url) + + # Call the download and parse methods to download information + try: + article.download() + article.parse() + article.nlp() + except Exception: + pass + + # Scrape the contents of article + title.append(article.title) # extracts the title of the article + text.append(article.text) # extracts the whole text of article + summary.append(article.summary) # gives us a summary abou the article + keywords.append(", ".join(article.keywords)) # the main keywords used in it + published_on.append(article.publish_date) # the date on which it was published + author.append(article.authors) # the authors of the article title[5] -TOI_dataset = pd.DataFrame(TOIarticle_links, columns=['URL']) +TOI_dataset = pd.DataFrame(TOIarticle_links, columns=["URL"]) # Adding the columns in the TOI news dataset -TOI_dataset['title'] = title -TOI_dataset['text'] = text -TOI_dataset['keywords'] = keywords -TOI_dataset['published date'] = published_on -TOI_dataset['author'] = author +TOI_dataset["title"] = title +TOI_dataset["text"] = text +TOI_dataset["keywords"] = keywords +TOI_dataset["published date"] = published_on +TOI_dataset["author"] = author TOI_dataset.head() -TOI_dataset.to_csv('TOI_news_dataset.csv') +TOI_dataset.to_csv("TOI_news_dataset.csv") -dt = pd.read_csv('TOI_news_dataset.csv') +dt = pd.read_csv("TOI_news_dataset.csv") dt.tail(3) from google.colab import files -files.download('TOI_news_dataset.csv') \ No newline at end of file + +files.download("TOI_news_dataset.csv")