Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
from googletrans import Translator
from bs4 import BeautifulSoup
import requests
"""
Libraries:
- requests (http://docs.python-requests.org/en/master/) by Kenneth Reitz for web requests
- bs4 (https://www.crummy.com/software/BeautifulSoup/) by Leonard Richardson for parsing HTML pages
- googletrans (https://pypi.org/project/googletrans/) by SuHun Han for translating
"""
"""
I've learned how to use Beautiful Soup by following its documentation:
https://www.crummy.com/software/BeautifulSoup/bs4/doc/
and this tutorial:
https://www.digitalocean.com/community/tutorials/how-to-scrape-web-pages-with-beautiful-soup-and-python-3
by Lisa Tagliaferri
"""
async def translate(client, message):
def parsePage(url):
"""take url as a string and parse it"""
######################################################
# Based on Lisa Tagliaferri's tutorial
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
#####################################################
return soup
def getLanguage(lang):
"""take language name e.g. english and change it to en"""
soup = parsePage('http://www.lingoes.net/en/translator/langcode.htm')
languages = []
for i in soup.find_all('td'):
languages.append(i.text.lower())
lang.lower()
num = languages.index(lang)
goal = languages[num - 1]
if goal == 'zh':
goal = goal + '-CN'
return goal
fullQuery = message.content[11:]
queryList = fullQuery.lower().split(' ')
toLan = getLanguage(queryList[0])
queryList.remove(queryList[0])
query = " ".join(queryList)
####################################################
# Based on example from: https://pypi.org/project/googletrans/
translator = Translator()
result = translator.translate(query, dest=toLan)
####################################################
await client.send_message(message.channel, 'Translation: ' + result.text)