Article 51Q42 Useful scrips to work on in quarantine.

Useful scrips to work on in quarantine.

by
teckk
from LinuxQuestions.org on (#51Q42)
Since we are all stuck at home...good time to practice our bash, python, perl, php, C++ etc.

I'll start with one. Here is a very basic text only web browser that uses python3, tkinter, urllib, beautifulsoup, html2text, youtube_dl.
Everything is put into functions so that it's easy to find/understand. tkinter is about as easy as it gets to make a GUI with python3.

Go ahead and pick it apart, put some forward/back buttons on it, make the utube functionality better, use tkinter.ttk instead of tkinter to colorize it, use a grid instead of frames...

It shows how to make elements with tkinter, get input from them, display output in them, save the element content to file.

Code:#! /usr/bin/python

from tkinter import *
from urllib import request
from bs4 import BeautifulSoup
from html2text import html2text, HTML2Text
from youtube_dl import YoutubeDL

#User agent for requests
'''
agent = ('Mozilla/5.0 (Windows NT 10.1; x86_64; rv:75.0)'
' Gecko/20100101 Firefox/75.0')
'''
agent = ('Mozilla/5.0')

#Make request header
user_agent = {'User-Agent': agent,
'Accept': 'text/html,application/xhtml+xml,'
'application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}

#Home page
#homePage = 'file:///path/to/Bookmarks.html'
homePage = 'https://en.wikipedia.org/wiki/Carbon'
#homePage = 'https://www.youtube.com/watch?v=5Q5g-krgDnk'

def TkBrowser():
#Load the page with urllib
def loadPage():
req = request.Request(entry.get(),
data=None, headers=user_agent)
page = request.urlopen(req)
return page

#Get page source
def viewSource():
text.delete(1.0, END)
pSource = loadPage()
html = pSource.read()
text.insert(1.0, html)
return html

#Write source code to file
def saveSource():
getHtml = viewSource().splitlines()
sSource = '\n'.join(line.decode('utf-8') for line in getHtml)
with open('TkBSource.html', 'a') as f:
f.write(sSource)

#Get text of page with soup
def textBrowse():
text.delete(1.0, END)
get = loadPage()
soup = BeautifulSoup(get, features='lxml')
#Kill all script and style elements
for s in soup(["script", "style"]):
s.extract()
txt = '\n'.join(soup.get_text().splitlines())
text.insert(1.0, txt)
return txt

#Get text of page with html2text
def textBrowse2():
text.delete(1.0, END)
pSource = loadPage()
html = pSource.read()
noLinks = HTML2Text()
noLinks.ignore_links = True
txt = noLinks.handle(html.decode('utf-8'))
text.insert(1.0, txt)

#Save text of page to file
def savePage():
getPage = textBrowse()
with open('TkBText.txt', 'w') as f:
f.write(getPage)

#Get links on page with soup
def pageLinks():
text.delete(1.0, END)
getP = loadPage()
soup = BeautifulSoup(getP, 'lxml')
hrefTags = soup.find_all(href=True)
links = [tag.get('href') for tag in hrefTags]
linkList = '\n'.join(l for l in links if l)
text.insert(1.0, linkList)
return linkList

#Save links to file
def saveLinks():
getLinks = pageLinks()
with open('TkBLinks.txt', 'w') as f:
f.write(getLinks)

#Get Utube video urls
def getUtube():
text.delete(1.0, END)
ulist = []
yt = YoutubeDL()
info = yt.extract_info(entry.get(), download=False)
ulist.append(info['title'])
ulist.append('')
ulist.append(info['description'])
ulist.append('')
try:
for i in info['formats']:
print(i)
ulist.append(i['format_id'])
ulist.append(i['url'])
ulist.append('')
utList = '\n'.join(ulist)
except:
pass
text.insert(1.0, utList)
return utList

#Save Utube url's to file
def saveUtube():
getTube = getUtube()
with open('TkBUtube.txt', 'w') as f:
f.write(getTube)

#Make a window
browser = Tk()
browser.title('PyTk Browser')
#Window size
browser.geometry('1200x900')
fontSize = ('Arial', 18)
bg='grey20'
fg='white'
bw=3

#Make Frames
top = Frame(browser)
bottom = Frame(browser)
top.pack(side=TOP)
bottom.pack(side=BOTTOM, fill=BOTH, expand=True)

#Make menubar
menubar = Menu(browser)
browser.config(menu=menubar, bg=bg)
menubar.config(font=fontSize, bd=bw, bg=bg, fg=fg)

#Make menu entries
#Load menu
loadmenu = Menu(menubar, tearoff=0)
loadmenu.config(
font=fontSize, bd=bw, bg=bg, fg=fg)
loadmenu.add_command(
label="Get page text with Beautiful Soup", command=textBrowse)
loadmenu.add_command(
label="Get page text with Html2text", command=textBrowse2)
loadmenu.add_command(
label="Save page text to file", command=savePage)

#Source menu
sourcemenu = Menu(menubar, tearoff=0)
sourcemenu.config(
font=fontSize, bd=bw, bg=bg, fg=fg)
sourcemenu.add_command(
label="View page source code", command=viewSource)
sourcemenu.add_command(
label="Save page source code to file", command=saveSource)

#Links menu
linksmenu = Menu(menubar, tearoff=0)
linksmenu.config(
font=fontSize, bd=bw, bg=bg, fg=fg)
linksmenu.add_command(
label="View page links", command=pageLinks)
linksmenu.add_command(
label="Save page links to file", command=saveLinks)

#UTube menu
utubemenu = Menu(menubar, tearoff=0)
utubemenu.config(
font=fontSize, bd=bw, bg=bg, fg=fg)
utubemenu.add_command(
label="View utube url's", command=getUtube)
utubemenu.add_command(
label="Save utube url's to file", command=saveUtube)

#Make a url entry widget
entry = Entry(browser)
label = Label(text='url')
label.config(font=fontSize, bg=bg, fg=fg, highlightthickness=3)
entry.config(font=fontSize, width=80, bd=bw,
highlightcolor='blue', highlightthickness=3)
#Bind enter key to textBrowse()
entry.bind('<Return>', lambda event=None: textBrowse())
entry.insert(END, homePage)

#Make a scroll bar
scroll = Scrollbar(browser)
scroll.config(width=25, bd=bw, bg=bg, troughcolor=fg)

#Make text widget
text = Text(browser, yscrollcommand=scroll.set)
text.config(font=fontSize, bd=bw, bg='white', fg='black',
highlightcolor='blue', highlightthickness=3)
scroll.config(command=text.yview)

#Place widgets
menubar.add_cascade(label="Load", menu=loadmenu)
menubar.add_cascade(label="Source", menu=sourcemenu)
menubar.add_cascade(label="Links", menu=linksmenu)
menubar.add_cascade(label="UTube", menu=utubemenu)
entry.pack(in_=top, side=LEFT)
label.pack(in_=top, side=RIGHT)
scroll.pack(in_=bottom, side=RIGHT, fill=Y)
text.pack(in_=bottom, side=TOP, fill="both", expand=True)

browser.mainloop()

if __name__ == "__main__":
TkBrowser()https://docs.python.org/3/
https://docs.python.org/3/library/tk.html
https://docs.python.org/3/library/urllib.html
https://www.crummy.com/software/BeautifulSoup/bs4/doc/
https://pypi.org/project/html2text/

Go ahead and post yours if you want. Something profitable to do instead of watching vids.latest?d=yIl2AUoC8zA latest?i=yYeVO4RMXQ8:em2X5vAYZO0:F7zBnMy latest?i=yYeVO4RMXQ8:em2X5vAYZO0:V_sGLiP latest?d=qj6IDK7rITs latest?i=yYeVO4RMXQ8:em2X5vAYZO0:gIN9vFwyYeVO4RMXQ8
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments