-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadLinks.py
112 lines (98 loc) · 3.11 KB
/
loadLinks.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Created on Mon Aug 22 22:57:23 2011
@author: antoanne
"""
import httplib
from BeautifulSoup import BeautifulSoup
import re
#http://www.englishexperts.com.br/2008/01/03/as-palavras-mais-comuns-da-lingua-inglesa/
#http://www.e-chords.com/browse/a
#http://www.e-chords.com/site/text-version.htm?p3=15746
SITE = "www.cifraclub.com.br/cifras"
PATH = "/home/antoanne/Dropbox/Work-2011/Mestrado/Modelagem/musica/data/"
classListTop1000 = [{'tag' : 'li',
'classes': ['img li1',
'img li2',
'img li3',
'img li4',
'img ',
'',
'fix3 ',
'fix4 '],
},]
classListABC = [{'tag' : 'ul',
'classes': ['lis1'],
},]
classCifra = [{'tag':'h1',
'id':['ai_musica']},
{'tag':'h2',
'id':['ai_artista']},
{'tag':'pre',
'id':['ct_tom', 'ct_cifra']},
]
def req(url):
adress = url.replace("http://", "").split("/")
conn = httplib.HTTPConnection(adress[0])
conn.request("GET", "/"+"/".join(adress[1:])) # /cavaco
r1 = conn.getresponse()
if (r1.status == 301):
# redirected
print("redirect...")
return req(r1.msg.dict['location'])
elif (r1.status == 404):
# fora do ar
return None
elif (r1.status == 200):
# OK
return r1.read()
else:
print "erro..."
def loadCifra(local):
cifra = req(local)
soup = BeautifulSoup(cifra)
cifraData = {}
for item in classCifra:
tag = item['tag']
for cls in item['id']:
#print cls
tagData = soup.findAll(tag, attrs={'id':cls})
allTagData = []
for d in tagData:
allTagData.append(str(d))
cifraData[cls.split("_")[1]] = allTagData
return cifraData
def writeToDictFile(data, fileName):
f = open(PATH + re.findall('.*?([^/\'" >]+)', SITE)[0] + '.dictFile', 'a')
f.write('%s\r\n' % str(data))
f.close()
def readFromDictFile(fileName):
f = open(PATH+fileName+'.dictFile','r')
my_dict = eval(f.read())
f.close()
print "FROM FILE: ", my_dict['ai_musica']
def extractLink(soap):
for tag in soap.findAll('a', href=True):
cifraLink = re.findall('.*?([^/\'" >]+)', SITE)[0] + tag['href']
print "Cifra:", cifraLink
writeToDictFile(loadCifra(cifraLink), cifraLink.rstrip("/").replace("/","_"))
def loadData(local, tag, cls):
print tag, cls
soup = BeautifulSoup(local)
tagData = soup.findAll(tag, attrs={'class':cls})
for d in tagData:
extractLink(d)
def loadHomeMusicList(local, dic):
for item in dic:
tag = item['tag']
for cls in item['classes']:
try:
loadData(local, tag, cls)
except:
print "Erro: %s" % local
break
local = req(SITE)
loadHomeMusicList(local, classListTop1000)
for s in SITE:
local = req(s)
loadHomeMusicList(local, classListABC)
break