26
26
BUS_NAME = "net.launchpad.lens.minecraft"
27
27
CACHE = "%s/unity-lens-minecraft/" % GLib.get_user_cache_dir()
32
self._lens = Unity.Lens.new ("/net/launchpad/lens/minecraft", "minecraft")
33
self._scope = Unity.Scope.new ("/net/launchpad/lens/minecraft/main")
33
self._lens = Unity.Lens.new("/net/launchpad/lens/minecraft",
35
self._scope = Unity.Scope.new("/net/launchpad/lens/minecraft/main")
34
36
self._lens.props.search_hint = "Search Minecraft recipes"
35
self._lens.props.visible = True;
37
self._lens.props.visible = True
37
self._lens.add_local_scope (self._scope);
39
self._lens.add_local_scope(self._scope)
38
40
self._scope.connect("search-changed", self.on_search_changed)
39
41
self._scope.connect('preview-uri', self.on_preview_uri)
40
42
svg_dir = "/usr/share/icons/unity-icon-theme/places/svg/"
42
cats.append (Unity.Category.new ("Recipes",
43
Gio.ThemedIcon.new(svg_dir+"group-installed.svg"),
44
Unity.CategoryRenderer.VERTICAL_TILE))
44
cats.append(Unity.Category.new("Recipes",
45
Gio.ThemedIcon.new(svg_dir+"group-installed.svg"),
46
Unity.CategoryRenderer.VERTICAL_TILE))
45
47
self._lens.props.categories = cats
46
48
self.wiki = ["http://minecraftwiki.net"]
47
self.title_fixes = {"Torches":"Torch","Boots":"Leather Boots","Helmet":"Leather Cap","Leggings":"Leather Pants","Chestplate":"Leather Tunic", "Gold Ingot":"Gold (Ingot)", "Iron Ingot":"Iron (Ingot)", "Sword":"Wooden Sword","Doors":"Wooden Door", "Axe":"Wooden Axe", "Shovel":"Wooden Shovel", "Pickaxe":"Wooden Pickaxe", "Hoe":"Wooden Hoe","Wool":"White Wool","Redstone Torch":"Redstone (Torch)","Minecart with Furnace":"Powered Minecart","Minecart with Chest":"Storage Minecart","Jack 'o' Lantern":"Jack-O-Lantern", "Ore":"Gold (Ore)", "Button":"Stone Button","Rail":"Rails", "Map (item)":"Empty Map","Slabs":"Stone Slab", "Stairs":"Cobblestone Stairs","Pressure Plates":"Wooden Pressure Plate", "Wooden Planks":"Wooden Plank", "Bow#Arrows":"Arrow", "Snow Block":"Snow", "Bricks":"Brick (Block)","Glowstone Block":"Glowstone (Block)"}
48
self.recipes = self.parseDB ()
49
self.title_fixes = {"Torches": "Torch", "Boots": "Leather Boots", "Helmet": "Leather Cap", "Leggings": "Leather Pants", "Chestplate": "Leather Tunic", "Gold Ingot": "Gold(Ingot)", "Iron Ingot": "Iron(Ingot)", "Sword": "Wooden Sword", "Doors": "Wooden Door", "Axe": "Wooden Axe", "Shovel": "Wooden Shovel", "Pickaxe": "Wooden Pickaxe", "Hoe": "Wooden Hoe", "Wool": "White Wool", "Redstone Torch": "Redstone(Torch)", "Minecart with Furnace": "Powered Minecart", "Minecart with Chest": "Storage Minecart", "Jack 'o' Lantern": "Jack-O-Lantern", "Ore": "Gold(Ore)", "Button": "Stone Button", "Rail": "Rails", "Map(item)": "Empty Map", "Slabs": "Stone Slab", "Stairs": "Cobblestone Stairs", "Pressure Plates": "Wooden Pressure Plate", "Wooden Planks": "Wooden Plank", "Bow#Arrows": "Arrow", "Snow Block": "Snow", "Bricks": "Brick(Block)", "Glowstone Block": "Glowstone(Block)"}
50
self.recipes = self.parseDB()
49
51
self.icons_retrieved = []
50
52
self.icon_runs = {}
53
55
self.preferences = Unity.PreferencesManager.get_default()
54
56
self.preferences.connect("notify::remote-content-search", self._on_preference_changed)
56
self._scope.export ();
58
def _on_preference_changed (self, *_):
60
def _on_preference_changed(self, *_):
59
61
self._scope.queue_search_changed(Unity.SearchType.DEFAULT)
61
def on_search_changed (self, scope, search, search_type, *_):
63
def on_search_changed(self, scope, search, search_type, *_):
62
64
model = search.props.results_model
65
67
# only perform the request if the user has not disabled
66
68
# online/commercial suggestions. That will hide the category as well.
71
73
if search_type is Unity.SearchType.DEFAULT:
72
search_string = search.props.search_string.strip ()
74
search_string = search.props.search_string.strip()
73
75
print "Search changed to \"%s\"" % search_string
74
76
self.update_results_model(model, search_string)
75
search.set_reply_hint ("no-results-hint", GLib.Variant.new_string("Sorry, there are no articles that match your search."))
77
search.set_reply_hint("no-results-hint", GLib.Variant.new_string("Sorry, there are no articles that match your search."))
78
def getRecipeData (self):
80
def getRecipeData(self):
80
url = ("http://www.minecraftwiki.net/api.php?action=query&format=json&titles=Crafting/Blocks|Crafting/Items|Template:Wool_Crafting|Template:Tool_Crafting|Template:Armor_Crafting|Template:Weapon_Crafting|Glowstone_Block&prop=revisions&rvprop=content")
82
url =("http://www.minecraftwiki.net/api.php?action=query&format=json&titles=Crafting/Blocks|Crafting/Items|Template:Wool_Crafting|Template:Tool_Crafting|Template:Armor_Crafting|Template:Weapon_Crafting|Glowstone_Block&prop=revisions&rvprop=content")
81
83
results = simplejson.load(urllib2.urlopen(url))
86
def getIcon (self, title):
89
def getIcon(self, title):
108
111
self.icon_runs[content['title']] = 1
109
112
if 'imageinfo' in content:
110
113
for c in content['imageinfo']:
111
icon_list.append (c['url'])
114
icon_list.append(c['url'])
113
116
# print "no icon found"
114
icon_list.append ('')
116
title_list.append (content['title'].replace("File:Grid ", "").replace('.png', ''))
119
title_list.append(content['title'].replace("File:Grid ", "").replace('.png', ''))
117
120
# print "ICONS " + str(icon_list)
118
121
# print "TITLES " + str(title_list)
119
122
return icon_list, title_list
187
model.append (str(r[0]),icon,cat, "text/html", r[0].replace('|',' or '), '', '')
190
model.append(str(r[0]),icon,cat, "text/html", r[0].replace('|',' or '), '', '')
193
196
recipes_list = []
195
recipes = self.getRecipeData ()
198
recipes = self.getRecipeData()
196
199
for r in recipes['query']['pages']:
200
if not 'revisions' in recipes['query']['pages'][r]:
197
202
content = recipes['query']['pages'][r]['revisions']
198
203
cat = recipes['query']['pages'][r]['title']
199
204
for c in content:
239
242
None, None, None]
240
243
A1 = re.search("\|A1=(.*)\|.*B1", a,flags=re.MULTILINE | re.S)
242
recipe_map[0] = str(A1.group(1).strip ())
245
recipe_map[0] = str(A1.group(1).strip())
243
246
B1 = re.search("\|B1=(.*)\|.*C1", a,re.MULTILINE | re.S)
245
recipe_map[1] = str(B1.group(1).strip ())
248
recipe_map[1] = str(B1.group(1).strip())
246
249
C1 = re.search("\|C1=(.*)\|.*A2", a,re.MULTILINE | re.S)
248
recipe_map[2] = str(C1.group(1).strip ())
251
recipe_map[2] = str(C1.group(1).strip())
249
252
A2 = re.search("\|A2=(.*)\|.*B2", a,re.MULTILINE | re.S)
251
recipe_map[3] = str(A2.group(1).strip ())
254
recipe_map[3] = str(A2.group(1).strip())
252
255
B2 = re.search("\|B2=(.*)\|.*C2", a,re.MULTILINE | re.S)
254
recipe_map[4] = str(B2.group(1).strip ())
257
recipe_map[4] = str(B2.group(1).strip())
255
258
C2 = re.search("\|C2=(.*)\|.*A3", a,re.MULTILINE | re.S)
257
recipe_map[5] = str(C2.group(1).strip ())
260
recipe_map[5] = str(C2.group(1).strip())
258
261
A3 = re.search("\|A3=(.*)\|.*B3", a,re.MULTILINE | re.S)
260
recipe_map[6] = str(A3.group(1).strip ())
263
recipe_map[6] = str(A3.group(1).strip())
261
264
B3 = re.search("\|B3=(.*)\|.*C3", a,re.MULTILINE | re.S)
263
recipe_map[7] = str(B3.group(1).strip ())
266
recipe_map[7] = str(B3.group(1).strip())
264
267
C3 = re.search("\|C3=(.*)\n?\|.*Output", a,re.MULTILINE | re.S)
266
recipe_map[8] = str(C3.group(1).strip ())
269
recipe_map[8] = str(C3.group(1).strip())
268
271
for m in recipe_map:
276
279
elif m and "Planks" in m:
277
280
rmap.append("Wooden Plank")
278
281
elif m and "Diamond" in m:
279
rmap.append("Diamond (Gem)")
282
rmap.append("Diamond(Gem)")
280
283
elif m and "Milk" in m:
281
284
rmap.append("Milk Bucket")
282
285
elif m and "Egg" in m:
283
286
rmap.append("Egg")
284
287
elif m and "Dust" in m:
285
m = m.replace("Dust","(Dust)")
288
m = m.replace("Dust", "(Dust)")
287
290
elif m and "Ingot" in m:
288
m = m.replace("Ingot","(Ingot)")
291
m = m.replace("Ingot", "(Ingot)")
290
293
elif m and "Coal" in m:
291
rmap.append("Coal (Item)")
294
rmap.append("Coal(Item)")
292
295
elif m and "Redstone Torch" in m:
293
rmap.append("Redstone (Torch)")
296
rmap.append("Redstone(Torch)")
294
297
elif m and "Melon Slice" in m:
295
rmap.append("Melon (Slice)")
298
rmap.append("Melon(Slice)")
296
299
elif m and "Output" in m:
298
301
elif m and "Block of Gold" in m:
299
rmap.append("Gold (Block)")
302
rmap.append("Gold(Block)")
300
303
elif m and "Oak Wood" in m:
301
304
rmap.append("Wood")
302
305
elif m and "Clay Balls" in m:
303
rmap.append("Clay (Item)")
304
elif m and "Brick (Item)" in m:
305
rmap.append("Clay (Brick)")
306
rmap.append("Clay(Item)")
307
elif m and "Brick(Item)" in m:
308
rmap.append("Clay(Brick)")
311
311
item.append(rmap)
313
output = re.search("Output= (.*)\n", a)
312
output = re.search("Output=(.*)\n", a)
315
314
if item[0] == '':
316
if output.group(1).strip () in self.title_fixes:
317
item[0] = (self.title_fixes[output.group(1).strip ()])
315
if output.group(1).strip() in self.title_fixes:
316
item[0] =(self.title_fixes[output.group(1).strip()])
319
item[0] = output.group(1).strip ()
320
item.append(output.group(1).strip ())
318
item[0] = output.group(1).strip()
319
item.append(output.group(1).strip())
323
322
description = re.search("Output=.*\n.*\n\|\s(.*)", a, re.MULTILINE)
325
description = description.group(1).strip ()
326
description = re.sub("\[\[.*\|","", description)
327
description = re.sub("\]\]","", description)
328
description = re.sub("\[\[","", description)
324
description = description.group(1).strip()
325
description = re.sub("\[\[.*\|", "", description)
326
description = re.sub("\]\]", "", description)
327
description = re.sub("\[\[", "", description)
329
328
description = re.sub(r"\{\{hungerbar\|(\d+)\}\}",r"\1 hunger points", description)
330
329
item.append(description)
353
352
amount = r[3].split(',')[-1]
354
353
description = r[4]
356
image = self.createRecipeImage (title)
355
image = self.createRecipeImage(title)
358
357
gicon_icon = Gio.FileIcon.new(Gio.file_new_for_uri(model.get_value(iter, 1)))
359
if self.is_file (CACHE+image):
358
if self.is_file(CACHE+image):
360
359
gicon_recipe = Gio.FileIcon.new(Gio.file_new_for_path(CACHE+image))
362
361
gicon_recipe = ''
363
preview = Unity.ApplicationPreview.new(title.strip (),'' ,description,gicon_icon, gicon_recipe)
362
preview = Unity.ApplicationPreview.new(title.strip(),'' ,description,gicon_icon, gicon_recipe)
365
364
preview.add_info(Unity.InfoHint.new("Production", "Production", None, amount))
366
365
# gfile_icon = Gio.file_new_for_path("/usr/share/icons/unity-icon-theme/places/svg/service-picasa.svg")
367
# gicon = Gio.FileIcon.new (gfile_icon)
366
# gicon = Gio.FileIcon.new(gfile_icon)
368
367
# view_action = Unity.PreviewAction.new("view", _("View"), gicon)
369
368
# view_action.connect('activated', self.view_action)
370
369
# preview.add_action(view_action)
372
371
iter = model.next(iter)
373
372
if preview == None:
374
print ("Couldn't find model row for requested preview uri: '%s'", uri)
373
print("Couldn't find model row for requested preview uri: '%s'", uri)
377
def createRecipeImage (self, title):
376
def createRecipeImage(self, title):
378
377
final_image = 'recipe_'+title+'.png'
379
if self.is_file (CACHE+final_image):
378
if self.is_file(CACHE+final_image):
380
379
return final_image
382
381
for r in self.recipes:
383
382
if title == r[0]:
385
icons, titles = self.getIcon (recipe)
384
icons, titles = self.getIcon(recipe)
386
385
ordered_icons = []
387
386
for item in recipe:
389
ordered_icons.append ('')
388
ordered_icons.append('')
391
390
position = titles.index(item)
392
391
ordered_icons.append(icons[position])
416
415
return final_image
419
def view_action (self, scope, uri):
418
def view_action(self, scope, uri):
420
419
"""On item clicked, close the Dash and display the photo"""
424
423
if __name__ == "__main__":
425
session_bus_connection = Gio.bus_get_sync (Gio.BusType.SESSION, None)
426
session_bus = Gio.DBusProxy.new_sync (session_bus_connection, 0, None,
424
session_bus_connection = Gio.bus_get_sync(Gio.BusType.SESSION, None)
425
session_bus = Gio.DBusProxy.new_sync(session_bus_connection, 0, None,
427
426
'org.freedesktop.DBus',
428
427
'/org/freedesktop/DBus',
429
428
'org.freedesktop.DBus', None)
430
429
result = session_bus.call_sync('RequestName',
431
GLib.Variant ("(su)", (BUS_NAME, 0x4)),
430
GLib.Variant("(su)",(BUS_NAME, 0x4)),
434
433
result = result.unpack()[0]
437
436
print >> sys.stderr, "Failed to own name %s. Bailing out." % BUS_NAME
440
439
daemon = Daemon()
441
440
GObject.MainLoop().run()