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