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.

Tuesday, 9 July 2019

India in cricket world cup 2019

Now that we have got our 4 semi finalists for this year's cricket world cup it's a good time to analyse and talk about the performance of team India and also to go deep down into questions like what we expected what happened and what to expect in the semi finals.



India entered this world cup as favorites and started its campaign against south africa which they won quite easily thanks to Rohit's brilliant 122* and then kept on winning every match until they encountered the hosts and their new away jersey with one match being washed off against New Zealand but still their run was good enough to keep them at the top of table at the end of league matches.
The only set back for India occurred when Shikhar got injured after scoring a match winning century against the most dominated team in world cups- Australia, though Rahul is doing well in the opening with Rohit  to fill the void created therefore India got rid of their first problem .
Now coming to middle order Virat is doing what he is best known for by playing some important innings through the middle overs just the only thing he is struggling with is he is not able to convert his scores to a big number and our new no. 4 batsman another Delhi boy Rishabh Pant is doing just fine and then comes the most interesting part of this team, the finishers first lets talk about Hardik Pandya. He definitely is playing some good knocks but still if you observe his shot selection you can see that he is not mature enough for that role that's the reason we have Dhoni in our team, but the thing is people always find a way to criticize him even though at the end of the innings his S.R. is
always more than or near to 100 this is the thing that sometimes bother me. If we look at Dhoni's playing style he always started slow initially and hits the boundaries in few last overs i definitely agree that he is not playing the innings he is known for but the fact is he is 38 now it's not easy for him to be in same form as he was in his peak time even the Sachin took a lot of innings to score his 100th century against Bangladesh and we even lose the match and his strike rate was even below 80. And if at the end of the day in a pressure game like world cup if we are winning the match than it doesn't matter if Dhoni played with the sr of 70 or of 120 because at the end of the day in a world cup match what matter the most is who won that particular match.
Now talking about our our bowling which is one of the best bowling lineup because in every other team either they have good pacers or they have good spinners but india is the only team with good pace attack with bowlers like Bumrah and a nice pair of kul-cha for the middle overs and the best part is our all rounder Pandya can now bowl all his 10 overs anyhow for which he was struggling before that's the reason that bowling is not concern anymore and so is the fielding, India has the lowest catch drop percentage in the world cup so far.



Now in the semis India is going against New zealand which has shown some good play so far in the tournament too although they found themselves little stuttering in their last 3 games with but still their excellent bowling performance by Boult and Fergueson & a few match winning knocks by their captain Kane Williamson made sure that they reach the semi final. So the way to tackle them is to
make sure that Williamson and Taylor doesn't remain at the crease for long and it will be easy for India to break their opening stand because none of their opener has performed so far in the league stages but still if their opening partnership goes good then they have a deep batting lineup that can capitalize to that and can put India under pressure easily with a big total.We have to keep on getting 1-2 wickets every 10 overs in order keep the momentum with us and we definitely got good bowlers to do that and as far as batting is concerned we can expect some good knocks from our inform batsmen Rohit and Kohli just like every match we just need to make sure that if we are chasing we need atleast one of them to play till  the 35-40 over otherwise it can put pressure on our middle order batsman and New zealand bowlers are good enough to take advantage of the situation from there.