-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_text.py
executable file
·64 lines (57 loc) · 1.73 KB
/
get_text.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys, urllib, os
"""
get_text.py
Detta script hämtar en undertext's filen frånn undertexter.se
./get-text.py <sökterm>
ex:
./get-text.py johan
./get-text.py house md s05e17
"""
def filter_page():
"""
Filtrerar fram möjliga resultat från hemsidan,
och returnerar dessa som tupler i form: (id, url)
"""
ut = "http://undertexter.se/?p=arkiv&cat=" + '%20'.join(sys.argv[1:])
webtext = urllib.urlopen(ut).read()
first = webtext.find('<title>' + ' '.join(sys.argv[1:]) + '</title>')
last = webtext.rfind('<a href="javascript:history.go(-1)"><b>')
newtext = webtext[first:last]
textlist = []
rawtextlist = newtext.split('\n')
[textlist.append(line.strip()) for line in rawtextlist]
hits = []
hits.append(('Ingen av dessa.',0))
iter = len(textlist)
i = 0
while i < iter:
if 'Nedladdningar' in textlist[i]:
hits.append((textlist[i+1][44:-5], "http://www.undertexter.se/utext.php?" + textlist[i-5][48:textlist[i-5].rfind('alt="')-2]))
i +=1
return hits
def get_textfile_url(hits):
"""
Frågar användaren vilken rls som den vill ladda ner.
0 betyder att man vill avbryta.
"""
i = 0
for item in hits:
print "[" + str(i) + "] " + item[0]
i +=1
return hits[int(raw_input("Vilken rls? "))][1]
def down_load_sub(url):
"""
Tar en url och laddar ner filen.
"""
if url:
a = urllib.urlopen(url)
url = a.geturl()
a.close()
urllib.urlretrieve(url, url.rsplit('/')[-1])
print 'Laddade ner' ,url.rsplit('/')[-1]
if __name__ == "__main__":
result = filter_page()
choice = get_textfile_url(result)
down_load_sub(choice)