-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
83 lines (77 loc) · 2.74 KB
/
convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from bs4 import BeautifulSoup
import sys
import os
import re
import math
import shutil
#Take input
try:
inputfile=sys.argv[1]
except IndexError:
print("Provide a filename with", sys.argv[0], "example.html")
sys.exit(1)
#create temp file to fix
tempfile=inputfile+"_temp"
shutil.copy(inputfile, tempfile)
#fix the temp file since it has a bunch of opened <div> tags that are never closed and breaks everything
with open(tempfile, 'r') as file:
filedata=file.read()
filedata=filedata.replace('<div align="left"><br>', '</div><div align="left"><br>')
with open(tempfile, 'w') as file:
file.write(filedata)
#Open and start finding information
url=tempfile
page=open(url)
soup=BeautifulSoup(page.read(), 'html.parser')
buttons=soup.find_all('font', size='+1')
#create output file in ir Flipper format
outputfile=os.path.splitext(inputfile)[0]+'.ir'
outfile=open(outputfile, 'w+')
outfile.write("Filetype: IR signals file\n")
outfile.write("Version: 1\n")
outfile.close
outfile=open(outputfile, 'a')
for button in buttons:
#find all of the information, output it and also put it into the ir file
fulldiv=button.find_previous('div', align="left")
print("Button:", button.text.strip())
outfile.write("#\n")
outfile.write("name: "+button.text.strip()+"\n")
osc=button.find_next('font', color="#666666")
print("Oscillate:", osc.text.strip())
outfile.write("type: raw\n")
frequencycode=button.find_next('font', color="#800080")
onetimebyte=button.find_next('font', color='#009900')
repeatbyte=button.find_next('font', color='#993300')
print("One Time Byte:", onetimebyte.text.strip())
print("Repeat Byte:", repeatbyte.text.strip())
print("Frequency Code:", frequencycode.text)
ir01s=fulldiv.find_all('span', class_=['IR1', 'IR0'])
hirs=[]
for ir01 in ir01s:
hirs.append(ir01.text.strip())
#conversion math from http://www.remotecentral.com/features/irdisp1.htm
frequency=round(1000000/(int(frequencycode.text.strip(), 16)*.241246))
onetimecode=int(onetimebyte.text.strip(), 16)
repeatcode=int(repeatbyte.text.strip(), 16)
#I could be wrong, but from MY understanding, the dutycycle can be anywhere between 25 and 50, but it seemed like all of the existing files I saw was at 0.330000, so I went with that
dutycycle="0.330000"
print("One time Code:", onetimecode)
print("Repeat Code:", repeatcode)
print("Frequency:", frequency)
outfile.write("frequency: "+str(frequency)+"\n")
print("Duty Cycle:", dutycycle)
outfile.write("duty_cycle: "+str(dutycycle)+"\n")
print("Converted:")
outfile.write("data:")
decoded=[]
for hir in hirs:
#Decoded IR math
decoded.append(round(1000000*int(hir, 16)/frequency))
for decodedd in decoded:
print(decodedd, "", end="")
outfile.write(" "+str(decodedd))
print("\n")
outfile.write("\n")
#delete the tempfile
os.remove(tempfile)