OlehOnyshchcak created this task.
OlehOnyshchcak added a project: Pywikibot.
Restricted Application added subscribers: pywikibot-bugs-list, Aklapper.

TASK DESCRIPTION
  It's impossible to get image description from Commons via pywikibot API.
  
  Image is represented as FilePage 
(https://doc.wikimedia.org/pywikibot/master/api_ref/pywikibot.html#pywikibot.FilePage).
 And while it allows to access Commons HTML page or e.g. revision history, it's 
impossible to get some useful descriptive properties such as description.
  
  Below is provided workaround how to do that, although it's errorsome and also 
inefficient (need to first transfer and them parse entire HTML page). Thus it 
would be great to have that in API (such as, get_description or 
get_property(name="description") of some kind):
  
    import pywikibot
    from html.parser import HTMLParser
    from html.entities import name2codepoint
    
    class _MyHTMLParser(HTMLParser):
        _description = ""
        _tag_counter = 0
        
        def handle_starttag(self, tag, attrs):
            if self._tag_counter > 0:
                self._tag_counter += 1
            
            for attr in attrs:
                if attr == ('class', 'description'):
                    self._tag_counter = 1
                    
    
        def handle_endtag(self, tag):
            if self._tag_counter > 0:
                self._tag_counter -= 1
    
        def handle_data(self, data):
            if self._tag_counter > 0:
                self._description += data
            
        def get_description(self):
            return self._description
    
    def get_description(img):
        html = img.getImagePageHtml()
        
        parser = _MyHTMLParser()
        parser.feed(img.getImagePageHtml())
        return parser.get_description().replace("\n", "")
    
    site = pywikibot.Site('en', 'wikipedia')
    page = pywikibot.Page(site, "Mary_Shelley")
    img = list(page.imagelinks())[0]
    
    print(get_description(img))

TASK DETAIL
  https://phabricator.wikimedia.org/T238992

EMAIL PREFERENCES
  https://phabricator.wikimedia.org/settings/panel/emailpreferences/

To: OlehOnyshchcak
Cc: Aklapper, pywikibot-bugs-list, OlehOnyshchcak, Zkhalido, Viztor, DannyS712, 
Wenyi, Tbscho, MayS, Mdupont, JJMC89, Dvorapa, Altostratus, Avicennasis, 
mys_721tx, jayvdb, Dalba, Masti, Alchimista, Rxy
_______________________________________________
pywikibot-bugs mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-bugs

Reply via email to