beaf8162ac3cc40f886302fe3aa111155de3bbdf
[vuplus_dvbapp-plugin] / rsdownloader / src / RSSearch.py
1 ##
2 ## RS Downloader
3 ## by AliAbdul
4 ##
5 from Components.ActionMap import ActionMap
6 from Components.MenuList import MenuList
7 from RSConfig import config
8 from Screens.MessageBox import MessageBox
9 from Screens.Screen import Screen
10 from twisted.web.client import getPage
11
12 ##############################################################################
13
14 class RSSearch(Screen):
15         skin = """
16                 <screen position="75,75" size="570,425" title="Searching... please wait!">
17                         <widget name="list" position="0,0" size="570,425" scrollbarMode="showOnDemand" />
18                 </screen>"""
19
20         def __init__(self, session, searchFor):
21                 Screen.__init__(self, session)
22                 self.session = session
23                 
24                 self.searchFor = searchFor.replace(" ", "%2B")
25                 self.maxPage = 1
26                 self.curPage = 1
27                 self.files = []
28                 
29                 self["list"] = MenuList([])
30                 
31                 self["actions"] = ActionMap(["OkCancelActions", "InfobarChannelSelection"],
32                         {
33                                 "historyBack": self.previousPage,
34                                 "historyNext": self.nextPage,
35                                 "ok": self.okClicked,
36                                 "cancel": self.close
37                         }, -1)
38                 
39                 self.onLayoutFinish.append(self.search)
40
41         def okClicked(self):
42                 if len(self.files) > 0:
43                         idx = self["list"].getSelectedIndex()
44                         url = self.files[idx]
45                         list = ("%s/search.txt" % config.plugins.RSDownloader.lists_directory.value).replace("//", "/")
46                         
47                         try:
48                                 f = open(list, "r")
49                                 content = f.read()
50                                 f.close()
51                                 
52                                 if not content.endswith("\n"):
53                                         content += "\n"
54                         except:
55                                 content = ""
56                         
57                         try:
58                                 f = open(list, "w")
59                                 f.write("%s%s\n" % (content, url))
60                                 f.close()
61                                 self.session.open(MessageBox, (_("Added %s to the download-list.") % url), MessageBox.TYPE_INFO)
62                         except:
63                                 self.session.open(MessageBox, (_("Error while adding %s to the download-list!") % url), MessageBox.TYPE_ERROR)
64
65         def search(self):
66                 getPage("http://rapidshare-search-engine.com/index-s_submit=Search&sformval=1&s_type=0&what=1&s=%s&start=%d.html" % (self.searchFor, self.curPage)).addCallback(self.searchCallback).addErrback(self.searchError)
67
68         def searchCallback(self, html=""):
69                 list = []
70                 files = []
71                 
72                 if html.__contains__("Nothing found, sorry."):
73                         self.session.open(MessageBox, (_("Error while searching http://rapidshare-search-engine.com!\n\nError: Nothing found, sorry.")), MessageBox.TYPE_ERROR)
74                         self.instance.setTitle(_("Nothing found, sorry."))
75                 else:
76                         tmp = html
77                         while tmp.__contains__("goPg('"):
78                                 idx = tmp.index("goPg('")
79                                 tmp = tmp[idx+6:]
80                                 idx = tmp.index("'")
81                                 pageNumber = tmp[:idx]
82                                 \r
83                                 try:
84                                         pageNumber = int(pageNumber)
85                                         if pageNumber > self.maxPage:
86                                                 self.maxPage = pageNumber
87                                 except:
88                                         pass
89                                 
90                                 self.instance.setTitle("Page %d / %d. Push < > to switch the page..." % (self.curPage, self.maxPage))
91                         
92                         while html.__contains__('title="Download"'):
93                                 idx = html.index('title="Download"')
94                                 html = html[idx:]
95                                 idx = html.index('value="')
96                                 html = html[idx+7:]
97                                 idx = html.index('"')
98                                 size = html[:idx]
99                                 idx = html.index('http://rapidshare.com/')
100                                 html = html[idx:]
101                                 idx = html.index('"')
102                                 url = html[:idx]
103                                 
104                                 files.append(url) 
105                                 try:
106                                         urllist = url.split("/")
107                                         idx = len(urllist) - 1
108                                         name = urllist[idx]
109                                         list.append("%s - %s" % (size, name))
110                                 except:
111                                         list.append("%s - %s" % (size, url))
112                 
113                 self.files = files
114                 self["list"].setList(list)
115
116         def searchError(self, error=""):
117                 self.session.open(MessageBox, (_("Error while searching http://rapidshare-search-engine.com!\n\nError: %s") % str(error)), MessageBox.TYPE_ERROR)
118
119         def previousPage(self):
120                 if self.curPage > 1:
121                         self.curPage -= 1
122                         self.instance.setTitle("Loading previous page... please wait!")
123                         self.search()
124
125         def nextPage(self):
126                 if self.curPage < self.maxPage:
127                         self.curPage += 1
128                         self.instance.setTitle("Loading next page... please wait!")
129                         self.search()
130