1 | import locale |
---|
2 | import re |
---|
3 | class infomanager: |
---|
4 | def __init__(self): |
---|
5 | self.dbg=False |
---|
6 | self.plugin_actions={'get_info':'*'} |
---|
7 | self.result={} |
---|
8 | self.result['status']={'status':-1,'msg':''} |
---|
9 | self.result['data']=[] |
---|
10 | self.progress=0 |
---|
11 | self.inc=0 |
---|
12 | self._set_locale() |
---|
13 | #def __init__ |
---|
14 | |
---|
15 | def set_debug(self,dbg=True): |
---|
16 | self.dbg=dbg |
---|
17 | self._debug ("Debug enabled") |
---|
18 | #def set__debug |
---|
19 | |
---|
20 | def _debug(self,msg=''): |
---|
21 | if self.dbg: |
---|
22 | print ('DEBUG Info: '+str(msg)) |
---|
23 | #def _debug |
---|
24 | |
---|
25 | def register(self): |
---|
26 | return(self.plugin_actions) |
---|
27 | #def register |
---|
28 | |
---|
29 | def execute_action(self,appstream,action,applist,match=False): |
---|
30 | self.progress=0 |
---|
31 | self.appstream=appstream |
---|
32 | count=len(applist) |
---|
33 | if (action=='get_info' or action=='info') and count>0: |
---|
34 | inc=100.0/count |
---|
35 | self.result['data']=self._get_info(applist) |
---|
36 | self.progress=100 |
---|
37 | return(self.result) |
---|
38 | #def execute_action |
---|
39 | |
---|
40 | def _set_status(self,status,msg=''): |
---|
41 | self.result['status']={'status':status,'msg':msg} |
---|
42 | |
---|
43 | def _callback_progress(self): |
---|
44 | self.progress=self.progress+self.inc |
---|
45 | #def _callback_progress |
---|
46 | |
---|
47 | def _set_locale(self): |
---|
48 | if locale.getdefaultlocale()[0]=="ca_ES": |
---|
49 | self.locale=['ca_ES@valencia','ca@valencia','qcv','ca','ca_ES','es_ES','es','en_US','en_GB','en','C'] |
---|
50 | else: |
---|
51 | if locale.getdefaultlocale()[0]=="es_ES": |
---|
52 | self.locale=['es_ES','es','ca_ES@valencia','ca@valencia','qcv','ca','ca_ES','en_US','en_GB','en','C'] |
---|
53 | else: |
---|
54 | self.locale=[locale.getlocale()[0],'en_US','en_GB','en','ca_ES@valencia','ca@valencia','qcv','ca','es_ES','es','C'] |
---|
55 | #def _set_locale |
---|
56 | |
---|
57 | def _get_info(self,applist): |
---|
58 | applistInfo=[] |
---|
59 | for app in applist: |
---|
60 | appInfo=self._init_appInfo() |
---|
61 | # self._debug("Gathering package info for "+app.get_id()) |
---|
62 | appInfo['appstream_id']=app |
---|
63 | if app.get_id(): |
---|
64 | appInfo['id']=app.get_id() |
---|
65 | for localeItem in self.locale: |
---|
66 | if app.get_name(localeItem): |
---|
67 | appInfo['name']=app.get_name(localeItem) |
---|
68 | break |
---|
69 | if app.get_release_default(): |
---|
70 | appInfo['version']=app.get_release_default().get_version() |
---|
71 | else: |
---|
72 | for release in app.get_releases(): |
---|
73 | appinfo['version']=release.get_version() |
---|
74 | break |
---|
75 | if app.get_pkgname_default(): |
---|
76 | appInfo['package']=app.get_pkgname_default() |
---|
77 | if app.get_project_license(): |
---|
78 | appInfo['license']=app.get_project_license() |
---|
79 | else: |
---|
80 | appInfo['license']='other/restricted' |
---|
81 | orig=app.get_origin() |
---|
82 | if orig: |
---|
83 | if '-main' in orig or '-universe' in orig: |
---|
84 | appInfo['license']='open source' |
---|
85 | for localeItem in self.locale: |
---|
86 | if app.get_comment(localeItem): |
---|
87 | appInfo['summary']=app.get_comment(localeItem) |
---|
88 | break |
---|
89 | for localeItem in self.locale: |
---|
90 | if app.get_description(localeItem): |
---|
91 | appInfo['description']=app.get_description(localeItem) |
---|
92 | break |
---|
93 | if app.get_categories(): |
---|
94 | appInfo['categories']=app.get_categories() |
---|
95 | if app.get_icon_default(): |
---|
96 | appInfo['icon']=app.get_icon_default().get_name() |
---|
97 | if appInfo['icon']==None: |
---|
98 | icons=app.get_icons() |
---|
99 | if icons: |
---|
100 | for icon in icons: |
---|
101 | appInfo['icon']=icon.get_name() |
---|
102 | break |
---|
103 | else: |
---|
104 | appInfo['icon']='' |
---|
105 | |
---|
106 | if app.get_screenshots(): |
---|
107 | thumbnails_list=[] |
---|
108 | default_screenshot='' |
---|
109 | screenshots_list=[] |
---|
110 | for screenshot in app.get_screenshots(): |
---|
111 | for img in screenshot.get_images(): |
---|
112 | #The values are the values of appstream.ImageKind. 1=Source, 2=Thumbnail, 0=UNKNOWN |
---|
113 | #yml currently doen's support unkown images so we assign videos depending on file extension |
---|
114 | if not re.search(r'\.....?$',img.get_url()): |
---|
115 | appInfo['video']=img.get_url() |
---|
116 | continue |
---|
117 | if img.get_kind()==0: |
---|
118 | appInfo['video']=img.get_url() |
---|
119 | continue |
---|
120 | if img.get_kind()==1: #2=Default;1=normal;0=unknown |
---|
121 | default_screenshot=img.get_url() |
---|
122 | screenshots_list.append(img.get_url()) |
---|
123 | continue |
---|
124 | if img.get_kind()==2: |
---|
125 | thumbnails_list.append(img.get_url()) |
---|
126 | continue |
---|
127 | |
---|
128 | appInfo['thumbnails']=thumbnails_list |
---|
129 | appInfo['screenshot']=default_screenshot |
---|
130 | appInfo["screenshots"]=screenshots_list |
---|
131 | #The values are the values of appstream.UrlKind. 1=HOMEPAGE, 0=UNKNOWN |
---|
132 | if app.get_url_item(1): |
---|
133 | appInfo['homepage']=app.get_url_item(1).strip() |
---|
134 | if app.get_url_item(0): |
---|
135 | appInfo['installerUrl']=app.get_url_item(0).strip() |
---|
136 | if app.get_state()==1: #1=Installed |
---|
137 | appInfo['state']='installed' |
---|
138 | else: |
---|
139 | appInfo['state']='available' |
---|
140 | if app.get_kudos(): |
---|
141 | appInfo['kudos']=app.get_kudos() |
---|
142 | if app.get_metadata_item('x-zomando'): |
---|
143 | appInfo['installerUrl']=app.get_metadata_item('x-zomando') |
---|
144 | if app.get_origin(): |
---|
145 | appInfo['component']=app.get_origin() |
---|
146 | if app.get_metadata_item('x-video'): |
---|
147 | appInfo['video']=app.get_metadata_item('x-video') |
---|
148 | #Modify the url and create the url embed code |
---|
149 | if 'embed' not in appInfo['video']: |
---|
150 | appInfo['video']=appInfo['video'].replace('watch?v=','embed/') |
---|
151 | #F***g appstream returns unknown for all the possible kinds |
---|
152 | # if app.get_bundle_default(): |
---|
153 | # appInfo['bundle']=app.get_bundle_default().get_kind() |
---|
154 | #Fix F***g appstream returns unknown for all the possible kinds |
---|
155 | #ID must contain bundle type as last field |
---|
156 | for bundle in app.get_bundles(): |
---|
157 | if bundle.get_kind()==0: |
---|
158 | kind=bundle.get_id().split('.')[-1] |
---|
159 | appInfo['bundle']=kind.lower() |
---|
160 | if kind.lower=='sh': |
---|
161 | appInfo['installerUrl']=bundle.get_id() |
---|
162 | applistInfo.append(appInfo) |
---|
163 | self._callback_progress() |
---|
164 | self._set_status(0) |
---|
165 | return(applistInfo) |
---|
166 | #def _get_info |
---|
167 | |
---|
168 | def _init_appInfo(self): |
---|
169 | appInfo={'appstream_id':'',\ |
---|
170 | 'id':'',\ |
---|
171 | 'name':'',\ |
---|
172 | 'version':'',\ |
---|
173 | 'package':'',\ |
---|
174 | 'license':'',\ |
---|
175 | 'summary':'',\ |
---|
176 | 'description':'',\ |
---|
177 | 'categories':[],\ |
---|
178 | 'icon':'',\ |
---|
179 | 'screenshot':'',\ |
---|
180 | 'thumbnails':[],\ |
---|
181 | 'video':'',\ |
---|
182 | 'homepage':'',\ |
---|
183 | 'installerUrl':'',\ |
---|
184 | 'state':'',\ |
---|
185 | 'depends':'',\ |
---|
186 | 'kudos':'',\ |
---|
187 | 'suggests':'',\ |
---|
188 | 'extraInfo':'',\ |
---|
189 | 'size':'',\ |
---|
190 | 'bundle':'',\ |
---|
191 | 'updatable':'',\ |
---|
192 | } |
---|
193 | return(appInfo) |
---|
194 | #def _init_appInfo |
---|
195 | |
---|
196 | #class infomanager |
---|