diff --git a/README b/README deleted file mode 100644 index f26ef74..0000000 --- a/README +++ /dev/null @@ -1,13 +0,0 @@ -This is quick weekend hack made to access Wikipedia from command line. - - -Its written in python.To compile it just run 'python cliwiki.py'(without quotes) from your terminal and then enter the search title to get Wikipedia page on your terminal! - - -It can also be given one of following 3 command line arguments. - -1)potd -2)featured -3)onthisday - -this is just a quick hack and there's lot of crappy code in it.So please feel free to fork and send pull request if u want to improve it. diff --git a/README.md b/README.md new file mode 100644 index 0000000..266d4d9 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +Wikipedia CLI +========= + +Command line interface to access wikipedia pages. + +This version is a fork of [AnirudhBhat](https://github.com/AnirudhBhat)'s and now run on python 3. + + +## Usage +~~~bash +$ ./cliwiki.py -h +usage: cliwiki.py [-h] [-d | -f | -p] search + +Access Wikipedia from Command Line + +positional arguments: + search Page to search for on Wikipedia + +optional arguments: + -h, --help show this help message and exit + -d, --today Display URLs for the "On this day" pages + -f, --featured Display the featured articles URLs + -p, --picture Display URLs for the "Picture of the day" pages +~~~ \ No newline at end of file diff --git a/climovie.py b/climovie.py deleted file mode 100755 index 6aca2aa..0000000 --- a/climovie.py +++ /dev/null @@ -1,45 +0,0 @@ -#! /usr/bin/env python - -from __future__ import print_function -import urllib2 -import json - -API_KEY = "c5b633xj3ats73tmf9cez333" - -base_url = "http://api.rottentomatoes.com/api/public/v1.0" - - -type = "/lists/movies/box_office.lson?apikey=" - - -url = base_url + type + API_KEY - -result = json.load(urllib2.urlopen(url)) - -k=0 -z=0 - -for i in result["movies"]: - print(i["title"]) - print("SYNOPSIS",end="\n") - print(i["synopsis"],end="\n") - print("TRAILER",end="\n") - print(i["links"]["clips"],end="\n") - print("CASTS",end="\n") - for j in result["movies"][k]["abridged_cast"]: - print(j["name"]) - print(j["characters"],end="\n\n\n") - #for a in result["movies"]: - print("CRITICS_RATING:",end=" ") - print(result["movies"][z]["ratings"]["critics_rating"]) - print("CRITICS_SCORE:",end=" ") - print(result["movies"][z]["ratings"]["critics_score"]) - print("AUDIENCE_RATING:",end=" ") - print(result["movies"][z]["ratings"]["audience_rating"]) - print("AUDIENCE_SCORE:",end=" ") - print(result["movies"][z]["ratings"]["audience_score"],end="\n\n\n") - print("\t-----------------------------------------------------------") - #print(i["abridged_cast"][j]["name"]) - #print(i["abridged_cast"][j]["characters"],end="\n\n\n") - z=z+1 - k=k+1 diff --git a/cliwiki.py b/cliwiki.py old mode 100644 new mode 100755 index b87b854..f67645b --- a/cliwiki.py +++ b/cliwiki.py @@ -1,180 +1,220 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 + +""" CLI to access wikipedia informations """ -from __future__ import print_function import json -import urllib2 -import sys +import urllib.request import re +import argparse +# **** Global Variables **** +BASE_URL = "http://en.wikipedia.org/w/api.php?" +ACTION = "action=query" +FORMAT = "&format=json" +TITLES = "&titles=" -KEY = 0 - -base_url = "http://en.wikipedia.org/w/api.php?" -action = "action=query" +# **** Functions **** -Format = "&format=json" +def wiki_search(): + """ Search function """ + prop = "&prop=extracts" + plaintext = "&explaintext" + section_format = "&exsectionformat=plain" -titles="&titles=" + try: + url = (BASE_URL + ACTION + TITLES + + prop + plaintext + section_format + FORMAT) + # open url, read content (bytes), convert in string via decode() + result = json.loads(urllib.request.urlopen(url).read().decode('utf-8')) + key = list(result['query']['pages'].keys())[0][0:] + print(result['query']['pages'][key]['extract']) -def get_title(): - title = raw_input('enter the title you want to search\n') - title = title.replace(' ','_') - global titles - titles = titles + title + except KeyError: + print('No wikipedia page for that title. ' + 'Wikipedia search titles are case sensitive.') def url_and_displaytitle(): - print('\ntitle and url for this wikipedia site',end="\n") - global base_url - global action - global titles - global Format - prop = "&prop=info" - inprop = "&inprop=url|displaytitle" - url = base_url + action + titles + prop + inprop + Format - result = json.load(urllib2.urlopen(url)) - key = result['query']['pages'].keys() - global KEY - KEY = (key[0][:]) - print(result['query']['pages'][str(KEY)]['title']) - print(result['query']['pages'][str(KEY)]['fullurl']) - print('\t-------------------\t') - + """ Display URL and Title for the page """ -def interesting_links(): - print('\nyou may also be interested in the following links',end="\n") - global base_url - global Format - global action - global titles - prop = "&prop=extlinks" - try: - url = base_url + action + titles + prop + Format - result =json.load(urllib2.urlopen(url)) - key = result['query']['pages'].keys() - key = key[0][0:] - j = 0 - offset = result['query-continue']['extlinks']['eloffset'] - while j < offset: - print(result['query']['pages'][str(key)]['extlinks'][j]) - j=j+1 - except: - print('sorry,couldn\'t find any links') + print('\n\nTitle and url for this Wikipedia page: \n') + prop_inprop = "&prop=info&inprop=url|displaytitle" + url = BASE_URL + ACTION + TITLES + prop_inprop + FORMAT + # open url, read content (bytes), convert in string via decode() + result = json.loads(urllib.request.urlopen(url).read().decode('utf-8')) -#def interwiki_links(): - # print('inter wiki links found for this search',end="\n") - # base_url - # action - # titles - # prop = "&prop=iwlinks" - # url = base_url + action + titles + prop - # print(url) - # result = urllib2.urlopen(url) - # for i in result: - # print(i) + # In python 3 dict_keys are not indexable, so we need to use list() + key = list(result['query']['pages'].keys())[0][:] + print('\t'+result['query']['pages'][key]['title']) + print('\t'+result['query']['pages'][key]['fullurl']) + print('\n\t-------------------\t') -def wiki_search(): - global base_url - global action - global titles - global Format - prop = "&prop=extracts" - plaintext = "&explaintext" - section_format = "&exsectionformat=plain" +def interesting_links(): + + """Fonction displaying related links => Interest on the CLI ?""" + + print('\nYou may also be interested in the following links: \n') + + prop = "&prop=extlinks" + try: - url = base_url + action + titles + prop + plaintext + section_format + Format - result = json.load(urllib2.urlopen(url)) - key = result['query']['pages'].keys() - key = key[0][0:] - print(result['query']['pages'][str(key)]['extract'],end="\n") - except: - print('oops!,no wikipedia page for that title.Wikipedia search titles are case Sensitive...') - + url = BASE_URL + ACTION + TITLES + prop + FORMAT + # open url, read content (bytes), convert in string via decode() + result = json.loads(urllib.request.urlopen(url).read().decode('utf-8')) + + key = list(result['query']['pages'].keys())[0][0:] + + offset = result['query-continue']['extlinks']['eloffset'] + + for j in range(0, offset): + + # ['*'] => elements of ....[j] are dict, and their keys are '*' + print('\t'+result['query']['pages'][key]['extlinks'][j]['*']) + + except KeyError: + print("Sorry, we couldn't find any links.") def images(): - print('\nall images related to this search',end="\n") + """ Get images urls """ + image_url = "http://en.wikipedia.org/wiki/" - global base_url - global Format - global action - global titles prop = "&prop=images" - url = base_url + action + titles + prop + Format - result = json.load(urllib2.urlopen(url)) - key = result['query']['pages'].keys() - key = key[0][0:] + url = BASE_URL + ACTION + TITLES + prop + FORMAT + + print('\nAll images related to this search : \n') + + # open url, read content (bytes), convert in string via decode() + result = json.loads(urllib.request.urlopen(url).read().decode('utf-8')) + + key = list(result['query']['pages'].keys())[0][0:] + try: - i = 1 - while(i): - Image = str(result['query']['pages'][str(key)]['images'][i]['title']) - image = image_url + Image.replace(' ','_') - print(image) - i=i+1 - except: - print('\t------------------\t',end="\n") - pass - + for i in range(1, len(result['query']['pages'][key]['images'])): + image = result['query']['pages'][key]['images'][i]['title'] + image = image_url + image.replace(' ', '_') + print('\t'+image) + print('\n\t------------------\t') + except KeyError: + print('\n\t------------------\t') -def featured_feed(): - global base_url - Format = "&format=json" - action = "&action=featuredfeed" - try: - feed = "&feed=" + str(sys.argv[1]) - url = base_url + action + feed + Format - print(url) - result = urllib2.urlopen(url).read() - res1 = re.compile('(.*)') - res2 = re.compile('(.*)en') - Result1 = re.findall(res1,result) - Result2 = re.findall(res2,result) - for i in enumerate(zip(Result1,Result2)): - print(i) - except: - print('error!') - - +def featured_feed(feed): + """Featured Feed""" + + url = BASE_URL + "&action=featuredfeed" + "&feed=" + feed + FORMAT + + result = urllib.request.urlopen(url).read().decode('utf-8') + + re_title = re.compile('(.*)') + re_links = re.compile('(.*)en') + + result1 = re.findall(re_title, result) + result2 = re.findall(re_links, result) + + print('\n') + + for desc, url in zip(result1, result2): + print(desc + ':\t ' + url) + + + + +def interwiki_links(): + """ Inter wiki links """ + + print('Inter wiki links found for this search: ') + + url = BASE_URL + ACTION + TITLES + "&prop=iwlinks"+ FORMAT + print(url) + # TODO: parse the json, match it with a dict containing + # url to append depending on the key returned in the url, + # and then only show the resulting urls + + # result = urllib.request.urlopen(url).read().decode('utf-8') + + # for i in reslut: + # print(i) + + + +def main(): + """ Main function """ + + # Gestion des paramètres + parser = argparse.ArgumentParser(description = + "Access Wikipedia from Command Line") + + parser.add_argument('search', help = "Page to search for on Wikipedia") + + group = parser.add_mutually_exclusive_group() + + group.add_argument('-d', '--today', + action = 'store_const', + const = 'onthisday', + help='Display URLs for the "On this day" pages') + + group.add_argument('-f', '--featured', + action = 'store_const', + const = 'featured', + help = 'Display the featured articles URLs') + + group.add_argument('-p', '--picture', + action = 'store_const', + const = 'potd', + help='Display URLs for the "Picture of the day" pages') + + args = parser.parse_args() + + try: + if args.search : + global TITLES + TITLES += args.search.replace(' ','_') + wiki_search() + url_and_displaytitle() + images() + interesting_links() + # interwiki_links() -if len(sys.argv) < 2: - get_title() - wiki_search() - url_and_displaytitle() - images() - #interwiki_links() - interesting_links() -else: - featured_feed() + elif args.featured: + featured_feed(args.featured) + elif args.picture: + featured_feed(args.picture) + elif args.today: + featured_feed(args.today) + except KeyboardInterrupt: + print('\n\n Program interrupted') +if __name__ == "__main__": + main()