1

I read a tutorial about how to compile and install a Wikipedia lens, but it didn't work.

The tutorial sounds easy - i just copied and pasted to the file that was suppose to edit.

I have tried some times and here are 2 edits

edit 1:

import logging
import optparse

import gettext
from gettext import gettext as _
gettext.textdomain('wikipedia')

from singlet.lens import SingleScopeLens, IconViewCategory, ListViewCategory

from wikipedia import wikipediaconfig
import urllib2
import simplejson

class WikipediaLens(SingleScopeLens):

wiki = "http://en.wikipedia.org"
def wikipedia_query(self,search):
    try:
        search = search.replace(" ", "|")
        url = ("%s/w/api.php?action=opensearch&limit=25&format=json&search=%s" % (self.wiki, search))
        results = simplejson.loads(urllib2.urlopen(url).read())
        print "Searching Wikipedia"
        return results[1]
    except (IOError, KeyError, urllib2.URLError, urllib2.HTTPError, simplejson.JSONDecodeError):
        print "Error : Unable to search Wikipedia"
        return []

   class Meta:
    name = 'Wikipedia'
    description = 'Wikipedia Lens'
    search_hint = 'Search Wikipedia'
    icon = 'wikipedia.svg'
    search_on_blank=True

    # TODO: Add your categories
   articles_category = ListViewCategory("Articles", "dialog-information-symbolic")

  def search(self, search, results):
    for article in self.wikipedia_query(search):
        results.append("%s/wiki/%s" % (self.wiki, article),
                    "http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png",
                    self.articles_category,
                    "text/html",
                    article,
                    "Wikipedia Article",
                    "%s/wiki/%s" % (self.wiki, article))
    pass

edit 2:

import urllib2
import simplejson


import logging
import optparse

import gettext
from gettext import gettext as _
gettext.textdomain('wikipediaa')

from singlet.lens import SingleScopeLens, IconViewCategory, ListViewCategory

from wikipediaa import wikipediaaconfig

class WikipediaaLens(SingleScopeLens):

wiki = "http://en.wikipedia.org"

def wikipedia_query(self,search):
    try:
        search = search.replace(" ", "|")
        url = ("%s/w/api.php?action=opensearch&limit=25&format=json&search=%s" % (self.wiki, search))
        results = simplejson.loads(urllib2.urlopen(url).read())
        print "Searching Wikipedia"
        return results[1]

    except (IOError, KeyError, urllib2.URLError, urllib2.HTTPError, simplejson.JSONDecodeError):
        print "Error : Unable to search Wikipedia"
        return []
def search(self, search, results):
    for article in self.wikipedia_query(search):
        results.append("%s/wiki/%s" % (self.wiki, article),
                    "http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png",
                    self.articles_category,
                    "text/html",
                    article,
                    "Wikipedia Article",
                    "%s/wiki/%s" % (self.wiki, article))
    pass

    class Meta:
    name = 'Wikipedia'
    description = 'Wikipedia Lens'
    search_hint = 'Search Wikipedia'
    icon = 'wikipedia.svg'
    search_on_blank=True

    # TODO: Add your categories
   articles_category = ListViewCategory("Articles", "dialog-information-symbolic")

    def search(self, search, results):
    # TODO: Add your search results
    results.append('https://wiki.ubuntu.com/Unity/Lenses/Singlet',
                'ubuntu-logo',
                self.example_category,
                "text/html",
                'Learn More',
                'Find out how to write your Unity Lens',
                'https://wiki.ubuntu.com/Unity/Lenses/Singlet')
    pass

so .. what can i change in the edit ? (if anybody give me the entire edit file edited i will appreciate)

1
  • For questions like this, it's useful to have your working code. I would suggest dumping the whole working set on github and adding a link. Apr 14, 2012 at 22:39

1 Answer 1

4

Your issue is a case of formatting and indentation issues.

The first edit got the import items placement wrong. And the indentation wasn't correct. Python is very specific about indentation and if it isn't accurate, you won't get anywhere.

The second edit has an extra a after Wikipedia. It is wikipediaa everywhere in the code which messes things up. It also has some indentation and formatting problems.

Here is a very basic working version of the file from the tutorial. It works for me. You can go on and add other features like the customized images and the locales.

The best practise to learn how to program is not to ask for a completed file but to ask where you did it wrong. I am giving a leeway in that by giving you a sample file.


I guess you are not very much interested in developing a lens on your own. For your sake, I have added the lens to my PPA so that you can install it and use it without much of a chore.

Enter the following commands in a terminal:

sudo apt-add-repository ppa:jokerdino/ppa
sudo apt-get update
sudo apt-get install wikipedia

After installing the lens, log out of Unity and then back in. You should now be ready to use the Wikipedia lens.

Note: The lens in my PPA is just functional. Don't expect it to have stylish images, error messages or the localized versions. If you want all of those, I recommend you to try the tutorial yourself and add them for your delight.

4
  • thanks for you answer and for the input how to ask a question about learning programing.by the way i can say that i do not know anything about programming, although i would like to know how.
    – user49523
    Apr 15, 2012 at 21:17
  • i have tried several times and it didn't work not sure what is the error i making or is ubuntu 12.04 beta,not sure
    – user49523
    Apr 15, 2012 at 23:00
  • is there a more easy way of adding lens avaiable at this time ?
    – user49523
    Apr 16, 2012 at 21:19
  • @user49523 I have added instructions on how to install the lens from my PPA. Hope it works for you.
    – jokerdino
    Apr 17, 2012 at 9:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .