Wednesday 24 July 2019

Magical Python


Python nowadays has been considered as one of the most prominent and powerful language that's the reason many programmer are using it as their primary language and also many recruit are also stating their programming in python. So now let's talk about what python can really do that most of the language can't or if they can why people still prefer python over those languages.
Firstly python was introduced in 1991 hence it's a pretty new language as compared to most of the languages we have with us like C,C++ and Java. It has a very user friendly syntax too so that for beginners programming can become an easy start moreover it is popular because of it's scalability, productivity,  and usage in many data science and machine learning problems.
I myself had some beautiful experience with this particular language because you can make some fun scripts which can at times comes out to be useful. So let's discuss about those scripts one by one:
(I have used Jupyter Notebook for all my scripts and in case you don't have any particular library installed you can use 'pip install' command)


1.Playing random Youtube video


I made a script with GUI which can open up the random youtube video in your default browser you just have to put the name of the channel in the input label and BAM! your youtuber's video is right in front of you.

#importing dependencies
from tkinter import *
import random
import webbrowser
import feedparser

#defining the function

def play():
    str1=key.get()
    try:
        url=feedparser.parse("https://www.youtube.com/feeds/videos.xml?user="+''.join(str1))
        video=url.entries[random.randint(1,50)]
        webbrowser.open(video.link)
    except:
        print()

#GUI

window = Tk() 

#Window    
window.title("Youtube Random Play")
window.geometry('400x100')
window.configure(background="#a1dbcd")         

#Label 

msgLabel= Label(window, text="Channel Name: ", font=("Serif", 16), bg="#a1dbcd")
msgLabel.grid(column=1,row=2) 

key=StringVar() 


#Input Entry    
input_msg = Entry(window,textvariable = key, width=20, font=("Serif", 12))
input_msg.grid(column=2, row=2)
input_msg.focus()  

#Button

finalStr = Button(window, text="Open", font=("Serif", 16),command = play) 
finalStr.grid(column=2, row=27)  

window.mainloop() 

P.S. you have to make sure that the channel name you type is the actual channel name that appears in the URL of the youtube channel because most of the youtubers change their channel name but the name youtube provides doesn't change even after that and appears in the URL. For example Youtuber Ninja has his channel name as Ninja but actual/youtube name is NinjasHyper.



2.Check if website is up or down at any point 


Sometimes you may need to check if a website is not working or it is really down for everyone. For this you can use the below script where we scrap the website 'isitup.org' using beautifulsoup library.

from bs4 import BeautifulSoup
import requests

url=input('Enter URL:')

res=requests.get('https://isitup.org/'+url)

soup=BeautifulSoup(res.text,'lxml')

scrap=soup.find('div',{'id':'container'})#finding all div with id=container

output=scrap.find('p').text #getting the result from paragraph tag

print(output)


3.Audio to text converter


This is one of the best thing i believe python can do, if you want to convert the audio file to text then python provides 7 methods for recognising speech from audio source using various APIs.(here i have used a '.wav' file for conversion)

import speech_recognition as sr

r=sr.Recognizer()

audio=sr.AudioFile('harvard.wav')

with audio as source:

    text=r.record(source)
r.recognize_google(text)



If you compare the output with the actual input you will see that it is not 100% accurate but still is pretty nice thing for a programming language to do.


4.Forming a Word Cloud


Word cloud is an image formed from the input words with the size of the word depends upon the frequency of that word in the input.

from wordcloud import WordCloud
import matplotlib.pyplot as plt #for making a plot

text=input("ENTER THE TEXT:")

cloud=WordCloud(background_color="white").generate(text)

plt.imshow(cloud)

plt.axis('off')
plt.show()


5.Spamming on WhatsApp with GUI


Sometimes you want to just want to have fun by spamming a particular messages to your particular contact you can do so by using the below python application the only thing is you have to scan the QR code everytime you run the script and also you need to have the chrome driver downloaded and have to provide it's path in webdriver.chrome() function.
I am only writing the function here for GUI i have used same Tkinter library that i used in 'random youtube play'.

from selenium import webdriver
from tkinter import *

def whatsapp():


        name=key1.get()
        msg=key2.get()
        count=int(key3.get())

        driver=webdriver.Chrome(executable_path='C:/users/tyagi/Downloads/chromedriver.exe')

        driver.get('https://web.whatsapp.com/')


        user = driver.find_element_by_xpath('//span[@title = "{}"]'.format(name))

        user.click()

        msg_box = driver.find_element_by_class_name('_3u328')


        for i in range(count):

            msg_box.send_keys(msg)
            driver.find_element_by_class_name('_3M-N-').click()
        #print('Done...')



I have uploaded all of these code along with all necessary explanation and outputs on my Github:https://github.com/ApoorvTyagi/Magical-Python. Feel free to Pull/fork/contribute to it.

No comments:

Post a Comment