-2

There is this site https://colinhume.com/music.aspx which takes a certain input and converts it to an output file.

Let's say the input is :

X: 7
T:As Luck Will Have It
% Nottingham Music Database
S:John Lagden, via EF
M:6/8
K:D
E|"D"F3 "A7/c+"A2G|"Bm"FGF "A7"E3|"D"F2A d2c|"G"BcB "D"A3|"G"G2B "A7"A2G|\
"D"F2A d2f|
"G"efe "E7"dcB|"A7"ABA GFE|"D"F3 "A7/c+"A2G|"Bm"FGF "A7"E3|"D"F2A d2c|
"G"BcB "D"A3|"G"G2B "A7"A2G|"D"F2E "Bm"D2F|"Em"EFG "A7"F2E|"D"D3 -D2||
A|"D"d3 fed|"A"c2d e2A|"Bm"Bcd c2B|"F#m"A3 A2A|"G"B3 dcB|
"D"A2d f2a|"G"agf "E7"fed|"A7"e3 e2A|"D"d3 fed|"A"c2B A2A|
"Bm"B3 dcB|"F#m"A3 A2F|"G"G3 BAG|"D"F2A d2D|"Em"EFG "A7"F2E|"D"D3 -D2||

When you click on "Convert" button, there is an output page which has a "Play" button on the top. When you click the button, a midi file gets downloaded.

Now, is it possible for this whole process to be done by a python code automatically, which stores the midi file to my PC?

I need this code as a part of a project which should automatically convert that type of input to midi format. I have tried many offline codes, none of them seem to work properly.

So I hope I could do this with web scraping.

Note: Input needs to be in certain format, so paste the above text as it is in the site.

0

1 Answer 1

1

You'll need to import selenium module to run this. Also you need to have the chromedriver in your path. Maybe you already do since you mentioned web scraping.

EDIT: the code now doesn't show the GUI and downloads the file to the same folder of the py script.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os

options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=options)
download_path = os.path.dirname(__file__)
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd':'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_path}}
driver.execute("send_command", params)    

song = """X: 7
T:As Luck Will Have It
% Nottingham Music Database
S:John Lagden, via EF
M:6/8
K:D
E|"D"F3 "A7/c+"A2G|"Bm"FGF "A7"E3|"D"F2A d2c|"G"BcB "D"A3|"G"G2B "A7"A2G|\
"D"F2A d2f|
"G"efe "E7"dcB|"A7"ABA GFE|"D"F3 "A7/c+"A2G|"Bm"FGF "A7"E3|"D"F2A d2c|
"G"BcB "D"A3|"G"G2B "A7"A2G|"D"F2E "Bm"D2F|"Em"EFG "A7"F2E|"D"D3 -D2||
A|"D"d3 fed|"A"c2d e2A|"Bm"Bcd c2B|"F#m"A3 A2A|"G"B3 dcB|
"D"A2d f2a|"G"agf "E7"fed|"A7"e3 e2A|"D"d3 fed|"A"c2B A2A|
"Bm"B3 dcB|"F#m"A3 A2F|"G"G3 BAG|"D"F2A d2D|"Em"EFG "A7"F2E|"D"D3 -D2||"""

driver.get("https://colinhume.com/music.aspx")
input_element = driver.find_element_by_id("InBox")
input_element.send_keys(song)

button_element = driver.find_element_by_id("ConvBtn")
button_element.click()

button_elements = driver.find_elements_by_class_name("Button")
for button_element in button_elements:
    if button_element.text == "Play":
            driver.get(button_element.find_element_by_tag_name("a").get_attribute("href"))
            break
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.