Esempio n. 1
0
async def wikilang(ctx, language=None):
    if language == None:
        deflang = str(
            wikipedia_language.setdefault(
                str(ctx.message.author.id),
                guild_language.get(str(ctx.guild.id), "en")))
        await ctx.send("Language is {short} - {long}".format(
            short=deflang, long=wikipedia.languages()[deflang]))
        return
    query = language.lower()
    if query in wikipedia.languages():
        wikipedia_language.update({str(ctx.message.author.id): str(query)})
        lg = wikipedia.languages()[query]
        await ctx.send(languages[guild_language.setdefault(
            str(ctx.guild.id), "en")]["wikilang_success"].format(query=query,
                                                                 lg=lg))
    else:
        defaultlanguage = wikipedia_language.setdefault(
            str(ctx.message.author.id),
            guild_language.setdefault(str(ctx.guild.id), "en"))
        await ctx.send(languages[guild_language.setdefault(
            str(ctx.guild.id), "en")]["wikilang_error"].format(
                defaultlang=defaultlanguage,
                defaultlg=wikipedia.languages()[defaultlanguage]))
    with open('wikipedia_language.json', 'w') as json_file:
        json.dump(wikipedia_language, json_file)
    if use_postgres: backup()
Esempio n. 2
0
    def _is_parameters_ok(self):
        """
        Check if received parameters are ok to perform operations in the neuron
        :return: true if parameters are ok, raise an exception otherwise

        .. raises:: InvalidParameterException
        """

        if self.query is None or self.query == "":
            raise InvalidParameterException("Wikipedia needs a query")
        if self.language is None:
            raise InvalidParameterException("Wikipedia needs a language")

        valid_language = wikipedia.languages().keys()
        if self.language not in valid_language:
            raise InvalidParameterException(
                "Wikipedia needs a valid language: %s" % valid_language)

        if self.sentences is not None:
            try:
                self.sentences = int(self.sentences)
            except ValueError:
                # Handle the exception
                raise InvalidParameterException(
                    "Number of sentences must be an integer")
        return True
def wiki_extraction(n, languages):
    for lan in languages:
        if lan not in w.languages():
            raise Exception('Language wrongly specified')
        if isinstance(lan, str) is False:
            raise Exception(
                'Wrong format. The language list takes only strings')
    rows_list = []
    for lan in languages:
        w.set_lang(lan)
        page = w.random(pages=n)
        for i in page:
            try:
                dict_ = {}
                summary = w.WikipediaPage(title=i).summary
                if len(summary) > 0:
                    input_dict = {'Title': i, 'Text': summary, 'Language': lan}
                    dict_.update(input_dict)
                    rows_list.append(dict_)
                else:
                    page.append(w.random(pages=1))
            except:
                page.append(w.random(pages=1))
                pass
    return pd.DataFrame(rows_list)
Esempio n. 4
0
def choose_language():
    user_input = input("[1]. Wpisz kod WP swojego języka(en, pl, de, etc.)\n"
                       "[2]. Wybierz język z listy\n")
    language_choosen = False
    languages = wikipedia.languages()

    if user_input == '1':
        while not language_choosen:
            wp_code = input("Wpisz kod: ")
            if wp_code in languages:
                wikipedia.set_lang(wp_code)
                language_choosen = True
            else:
                print("Nie istnieje taki kod!")
    else:
        show_languages(languages)
        while not language_choosen:
            search_language = input("Podaj język: ")
            if search_language in languages.values():
                for wp, lang in languages.items():
                    if lang == search_language:
                        wikipedia.set_lang(wp)
                        language_choosen = True
            else:
                print("Nie ma takiego języka")
    os.system('clear')
Esempio n. 5
0
def busca_idioma(i):
    try:
        while not i in wikipedia.languages():
            i = input("Input no válido: ")
        return i
    except:
        print("MALA CONEXIÓN.")
Esempio n. 6
0
def busca_idioma(i):
    try:
        while not i in wikipedia.languages():
            i = input("Input no válido: ")
        return i
    except:
        print("FALLO AL ESTABLECER EL IDIOMA")
Esempio n. 7
0
 def activate(self):
     super().activate()
     if self.config['LANGUAGE'] in wikipedia.languages():
         wikipedia.set_lang(self.config['LANGUAGE'])
     else:
         self.log.warning('{0} is not a valid language code.'.format(
             self.config['LANGUAGE']))
     return
Esempio n. 8
0
def busca_idioma():
    i = input(
        "Introduce iniciales del idioma deseado (ej: \'es\',\'en\',\'fr\'...): "
    )
    try:
        while not i in wikipedia.languages():
            i = input("Input no válido: ")
        return i
    except:
        print("FALLO AL ESTABLECER EL IDIOMA")
Esempio n. 9
0
def wiki_lang(lang, set_id):
    langs_dict = wikipedia.languages()
    if lang in langs_dict.keys():
        wiki_settings[set_id] = lang
        return ("Language has been changed to {} successfully.".format(
            langs_dict[lang]))
    return ("{} not available!\n"
            "See meta.wikimedia.org/wiki/List_of_Wikipedias for "
            "a list of available languages, and use the prefix "
            "in the Wiki column to set the language.".format(lang))
Esempio n. 10
0
def setLanguage():
    print('Languages availables: ')
    languages = wikipedia.languages()
    for lang in languages:
        print(f'{lang} : {languages[lang]}')

    selected_lang = input('(*) Option (default = en): ')
    if (selected_lang):
        global language
        language = selected_lang
Esempio n. 11
0
def home():
    lang = wikipedia.languages()
    languages = []
    for i in lang:
        languages.append(wikipedia.languages()[i])

    if request.method == "POST":
        try:
            query = request.form.get('query')
            try:
                result = wikipedia.summary(query)
            except wikipedia.exceptions.DisambiguationError:
                stra = "There May Be Multiple Articles About The Topic "
                strb = ". Please Be More Specific!"
                result = stra + query + strb
            return render_template("index.html", result=result)
        except:
            pass

    return render_template("index.html", languages=languages)
Esempio n. 12
0
 async def wikipedia(self, ctx, language: str):
     """Wikipedia based command. Do !help wp."""
     global wiki_lang
     if language in wikipedia.languages():
         wiki_lang = language
         wikipedia.set_lang(language)
     else:
         error_d = f'"{language}" isn\'t in the list of languages.'
         await ctx.send(embed=error_embed(error_d))
     if ctx.invoked_subcommand is None:
         error_d = 'this is a f****n subcommand, idiot. can\'t you just do !help wp or something?'
         await ctx.send(embed=error_embed(error_d))
Esempio n. 13
0
async def wiki(msg: types.Message):
    _lang = [msg.from_user.language_code if msg.from_user.language_code else 'en-us']
    regex_groups = re.findall(r'(\w{2})\.wikipedia.org\/wiki\/([\S]{0,})', msg.text)

    if not regex_groups[0][0] in wikipedia.languages():
        return await msg.reply(_lang['lang_not_found']) 
    wikipedia.set_lang(regex_groups[0][0])
    try:
        page = wikipedia.page(regex_groups[0][1])
        await msg.reply(f"<b>{page.title}</b>\n{page.summary}")
    except:
        return await msg.reply(_lang["page_not_found"])
Esempio n. 14
0
	def __init__(self, query=None, lang="en"):
		self.name 		= ""
		self.text		= ""
		self.dates 		= []
		self.images 	= []
		self.picture 	= ""
		self.hits 		= []

		if not query:
			return

		self.query = query

		if lang in wikipedia.languages():
			wikipedia.set_lang(lang)
Esempio n. 15
0
	def check_wiki_link_command(self, comment):
		'''
		Функция проверки на команду краткого содержания по ссылке на статью википедии 
		comment - комментарий для проверки
		return True - найдена команда в нужном формате и успешно отправлен ответ 
		return False - не найден либо ошибка при ответе
		'''
		history_filename = os.path.join(self.bot_profile, self.WIKI_LOG_PATH)
		if not os.path.exists(history_filename):
			open(history_filename, 'w').close()
		history_file = open(history_filename, 'r')
		if comment.id not in history_file.read().splitlines():
			match = re.findall('.*\[\[(http[s]?:\/\/([a-z]{1,3})\.wikipedia\.org\/wiki\/.+)\]\].*', comment.body)
			if match:
				self.write_to_log('Вики-ссылка найдена в комментарии с id: ' + comment.id)
				url = match[0][0] # ссылка на статью
				lang = match[0][1] # языковой поддомен
				if lang not in wikipedia.languages():
					self.write_to_log('Дан неверный языковой поддомен "' + lang + '" в ссылке ' + url)
					return False
				try:
					page_title = self.get_wiki_page_title(url)
					wikipedia.set_lang(lang)
					summary = self.WIKI_HEADER + wikipedia.summary(page_title) + self.WIKI_FOOTER
				except wikipedia.exceptions.DisambiguationError as e:
					summary = self.WIKI_DISAMB_REPLY
					for possible_title in e.options:
						summary+= possible_title + '\n\n'
				except Exception as e:
					print('Возникло исключение: ' + str(e) + '\n')
					history_file.close()
					return False
				comment.reply(summary)
				history_file.close()
				history_file = open(history_filename, 'a+')
				history_file.write(comment.id + '\n')
				history_file.close()
				self.write_to_log('Успешно отправил ответ на вики-команду и записал в историю, таймаут ' + str(self.RESPONCE_TIMEOUT) + ' сек.')
				time.sleep(self.RESPONCE_TIMEOUT)
				return True
			else:
				history_file.close()
				return False
		else:
			history_file.close()
			self.write_to_log('Повтор комментария, можно не отвечать')
			return True
Esempio n. 16
0
 def do_activate(self, args, argv):
     lang = args[0]
     langTrue = str(lang) in wikipedia.languages()
     if langTrue == True:
         wikipedia.set_lang(str(lang))
         print(wikipedia.summary(' '.join(args[1:]), sentences=3))
     else:
         if not re.search(lang, "[a-z][a-z]+"):
             print(
                 "'" + str(lang) +
                 "' not found as language, trying to find a summary in the english language"
             )
             wikipedia.set_lang("en")
             print(wikipedia.summary(' '.join(args[1:]), sentences=3))
         else:
             wikipedia.set_lang("en")
             print(wikipedia.summary(' '.join(args), sentences=3))
def wikiSearch(lang="indonesia", seq="Test", *args):
    """Search for a specific subject on wikipedia and returns its title, summary, and link in a list.
    
    If your search query return multiple search results, this function will return a tuple of the error name and a list of top 5 search results."""
    if type(lang) is not str and type(seq) is not str: raise TypeError('language and search query must be a string.')

    for _, ls in enumerate(list(wapi.languages().items())):
        if lang in ls[1].lower():
            lang = ls[0]
            break
    try:
        wapi.set_lang(lang)
        page = wapi.page(seq)
    except wapi.exceptions.DisambiguationError as err:
        return ('error', err.args[1][:5]) 

    return [page.title, wapi.summary(seq).split('\n')[0][:1000]+'..', page.url]
Esempio n. 18
0
async def lang(ctx):

    global current_language

    msg = ctx.message.content.split(" ")
    command = msg[2]
    langcodes = wikipedia.languages().keys()

    #Check which command to run
    if command == "list" or command == "List":

        #List of most used languages on wikipedia
        languagelist = "English / English = en\nCebuano / Sinugboanong = ceb\nSwedish / Svenska = sv\nGerman / Deutsch = de\nFrench / Français = fr\nDutch / Nederlands = nl\nRussian / Русский = ru\nItalian / Italiano = it\nSpanish / Español = es\nWaray-Waray / Winaray = war\nPolish / Polski = pl\nVietnamese / Tiếng Việt = vi\n Japanese / 日本語 = ja\n"
        languagelistwiki = "https://meta.wikimedia.org/wiki/List_of_Wikipedias"

        embed = discord.Embed(
            title="Wikipedia language list:",
            color=0,
            description=languagelist +
            "\n\nAll supported languages can be found [here]({})".format(
                languagelistwiki))
        embed.set_thumbnail(
            url=
            "https://www.wikipedia.org/static/images/project-logos/{}wiki.png".
            format(current_language))
        await bot.say(embed=embed)

    elif command == "set" or command == "Set":

        #Check if the given language(langcode) is valid
        if msg[3] in langcodes:
            current_language = msg[3]
            wikipedia.set_lang(msg[3])

        else:
            embed = discord.Embed(
                title="Languages not found:",
                color=0xe74c3c,
                description=
                "Sorry, the language '{}' was not found. Please run '/w lang list' to see all language codes."
                .format(msg[3]))
            embed.set_thumbnail(
                url=
                "https://www.wikipedia.org/static/images/project-logos/{}wiki.png"
                .format(current_language))
            await bot.say(embed=embed)
Esempio n. 19
0
def send_welcome(message):
    title = ''.join(message.text.split(" ")[1:])
    if title in wikipedia.languages():
        wikipedia.set_lang(title)
        bot.send_message(
            chat_id=message.chat.id,
            text='set up %s language'%title
        )
    else:
#         bot.send_message(
#             chat_id=message.chat.id,
#             text=str('1' + lan_str)
#         )
        bot.send_message(
            chat_id=message.chat.id,
#             text=str('wrong language flag "%s" use on of %s'%(str(title)),lan_str)
#             text=str('wrong language flag "%s" use on of %s'%('1','lan_str'))
            text=str('wrong language flag')
        )
Esempio n. 20
0
    def _is_parameters_ok(self):
        """
        Check if received parameters are ok to perform operations in the neuron
        :return: true if parameters are ok, raise an exception otherwise

        .. raises:: InvalidParameterException
        """

        if self.query is None:
            raise InvalidParameterException("Wikipedia needs a query")
        if self.language is None:
            raise InvalidParameterException("Wikipedia needs a language")

        valid_language = wikipedia.languages().keys()
        if self.language not in valid_language:
            raise InvalidParameterException(
                "Wikipedia needs a valid language: %s" % valid_language)

        return True
Esempio n. 21
0
def execute_wiki(query, client, message):
    if "/comune" in query:
        try:
            return comune(client, message)
        except:
            id_messaggio = utils.get_config.get_id_msg(message)
            client.edit_message_text(utils.get_config.get_chat(message),
                                     id_messaggio + 1, "Operazione fallita")
            return
    lingua = get_lang(query)
    if not (lingua in wikipedia.languages()) or lingua == "all":
        return exec_wiki_ita(query, client, message)
    word = get_keyword(query)
    if " all " in query:
        return wikiall(word, client, message, lingua)
    if "random" in query:
        return wikirandom(1, False, client, message, lingua)
    else:
        return wiki(word, client, message, lingua)
Esempio n. 22
0
 def __init__(self, parent=None):
     QWidget.__init__(self, parent)
     self.setupUi(self)
     self.about_action.setIcon(QIcon('images/about.png'))
     self.preferences_action.setIcon(QIcon('images/preferences.png'))
     self.quit_action.setIcon(QIcon('images/quit.png'))
     self.quit_action.triggered.connect(lambda : exit(0))
     self.preferences_action.triggered.connect(self.handle_preferences_menu_action)
     self.title_line_edit.returnPressed.connect(self._extract_from_wiki)
     self.content_text_browser.anchorClicked.connect(self.handle_anchor_clicked)
     self.run_push_button.clicked.connect(self.handle_run_button)
     self.run_push_button.setIcon(QIcon('images/run.png'))
     self.run_push_button.setText('Download')
     self.setWindowIcon(QIcon('images/copas-logo.png'))
     self.page_combo_box.addItems(
         ['Content', 'Images', 'Summary', 'Images Links', 'References Links'])
     self.about_action.triggered.connect(self.handle_about_menu_action)
     for lang in sorted(wikipedia.languages()):
         self.lang_combo_box.addItem(lang)
Esempio n. 23
0
 async def wikipedia(self, context, lang: str = None, query: str = None):
     ''' Get a page from wikipedia and reply with an embed '''
     query = self.bot.get_text(context)
     if lang is not None:
         if lang.startswith('(') and lang.endswith(')'):
             query = query[len(lang) + 1:]
             lang = lang[1:-1]
         else:
             lang = None
     if query in [None, '', ' ']:
         await self.bot.doubt(context)
         return
     try:
         import wikipedia
         if lang is not None and lang in wikipedia.languages().keys():
             wikipedia.set_lang(lang)
         else:
             wikipedia.set_lang('en')
         page = wikipedia.page(query)
         summary = page.summary
         if len(summary) > 1222:  # totally arbitrary chosen number
             summary = summary[:1220] + '...'
         embed = discord.Embed(title=page.title,
                               description=summary,
                               url=page.url)
         embed.set_footer(text=page.url)
         if self.bot.config['bleeding']:
             if len(page.images) > 0:
                 embed.set_image(url=page.images[0])
         await self.bot.say(embed=embed)
         await self.bot.replied(context)
     except wikipedia.PageError as e:
         await self.bot.reply(
             '{}\nMake sure you search for page titles in the language that you have set.'
             .format(e))
         await self.bot.doubt(context)
     except KeyError:
         pass
     except wikipedia.DisambiguationError as e:
         msg = '```\n{}\n```'.format(e)
         await self.bot.doubt(context)
         await self.bot.say(msg)
Esempio n. 24
0
 def dsply_lang(self):
     try:
         self.tex_name.setText("Supported languages")
         content="Wikipedia provides content in the following languages\n"
         list_of_languages=wiki.languages().values()
         for lenguiz in list_of_languages:
             content+=str(str(lenguiz)+"\n")
         self.box_cont.setPlainText(content)
         self.box_smry.clear()
         self.box_sgst.clear()
         self.val_paid.setText("UNDEFINED")
         self.val_reid.setText("UNDEFINED")
         self.head_cont.setText("Wisdom currently supports only English language")
         self.art_url_disp.setText("An language page has no URL")
     except Exception as e:
         self.tex_name.setText("Supported languages")
         self.head_cont.setText("Failed to retrieve the list of supported languages")
         self.box_smry.clear()
         self.box_cont.clear()
         self.box_sgst.clear()
         self.val_paid.setText("UNDEFINED")
         self.val_reid.setText("UNDEFINED")
         self.art_url_disp.setText("An errored page has no URL")
Esempio n. 25
0
def wikispam():
    while True:
        selectedlang = input(
            "Choose the language you want to spam using the UTF-8 Language Codes ex.English - en:\n"
        )

        try:
            if selectedlang not in wikipedia.languages():
                if selectedlang != wikipedia.languages():
                    print(
                        f"Your selected Language of {selectedlang} is not found. Try again!\n"
                    )
                continue
            break
        except:
            print("This is not a language code")
    print(
        f"You selected the language {selectedlang} for your Wikipedia article!"
    )

    while True:
        timer = input(
            "\nHow long do you want the time in between messages to be (sec)?\nChoose a number: "
        )

        try:
            float(timer)
            if float(timer) < -0.1:
                print("This is not a valid number!")
                continue
            break

        except ValueError:
            print("This is not a valid number!\n")

    print(
        f"\nYou opened a random wiki article with a timer of {timer} seconds!\nBe sure to focus the window of your "
        f"choice.\nStarting in:")

    try:
        wikipedia.set_lang(selectedlang)
        wikipage = wikipedia.random(1)
        wikiload = wikipedia.page(wikipage).content

        page = split_into_sentences(wikiload)

        countdown(5)

        print("\r", end='')
        print(f"Started!\nSpamming {wikipedia.page(wikipage).title}\n",
              end='',
              flush=True)
        float(timer)
        for word in page:
            pyautogui.typewrite(word)
            pyautogui.press('enter')
            time.sleep(float(timer))

        print("\r", end='')
        print("Done!", end='', flush=True)

    except wikipedia.DisambiguationError as e:
        s = random.choice(e.options)
        p = wikipedia.pages(s)
Esempio n. 26
0
        print(e.options)
    except wikipedia.exceptions.PageError:
        print("Page not found. Try again with different query.")

    try:
        wpg = wikipedia.page(
            "virat kohli"
        )  # Wiki page with title = "title" or the pageid = numpageid
        print(wpg.content)
        print(wpg.categories)
        print(wpg.images)
        print(wpg.links)
        print(wpg.references)
        print(wpg.sections)
        print(wpg.summary)
    except wikipedia.exceptions.DisambiguationError as e:
        print(e.options)
    except wikipedia.exceptions.PageError:
        print("Page not found. Try again with different query.")

    print(
        wikipedia.languages()
    )  # List all the currently supported language prefixes (usually ISO language code).

    # wikipedia.set_lang(prefix)        # set language, but query must be made in the same lang

    # requests rate limits
    # wikipedia.set_rate_limiting(rate_limit, min_wait=datetime.timedelta(0, 0, 50000))

except Exception as e:
    print(str(e))
Esempio n. 27
0
import wikipedia

# 取り込む記事のディレクトリとタイトル
dirPass = '******'
print('English' in wikipedia.languages())
titles = wikipedia.random(pages=10)
print(titles)
'''
# Wikipediaのテキストデータを取り込む
texts = [wikipedia.page(t, auto_suggest=False).content for t in titles]

for (title, text) in zip(titles, texts):
    fName = dirPass + '/' + title.replace(' ', '_').lower() + '.txt'

    with open(fName, 'w') as f:
        f.write(text)
'''
Esempio n. 28
0
def busca_idioma(i):
    while not i in wikipedia.languages():
        i = input("Input no válido: ")
    return i
Esempio n. 29
0
import wikipedia
print(wikipedia.search("Bill"))
print(wikipedia.suggest("Bill cliton"))
#print(wikipedia.summary("Ubuntu"))
# print(wikipedia.summary("key"))
# print(wikipedia.summary("Key (cryptography)"))
print(wikipedia.page("Ubuntu"))
print(wikipedia.page("Python").content)
print(wikipedia.page("Python").url)
print(wikipedia.page("Python").references)
print(wikipedia.page("Python").title)
print(wikipedia.page("Python").categories)
print(wikipedia.page("Ubuntu").links)
print(wikipedia.geosearch(37.787, -122.4))
print(wikipedia.page(37.787, -122.4))
wikipedia.set_lang("de")
print(wikipedia.summary("ubuntu", sentences=2))

print(wikipedia.languages())
print(wikipedia.page("ubuntu").images[0])
print(wikipedia.page("Ubuntu").html())
Esempio n. 30
0
def checklang(language):
    if (arr[-1] in wikipedia.languages()):
        return 1
    else:
        return 0
Esempio n. 31
0
def main(argv):
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description="fetch snippets of language-specific text from random pages "
        "of one or many wikipedias",
    )
    parser.add_argument(
        "--langs",
        type=str,
        nargs="+",
        required=False,
        help="one or more languages for which to fetch wiki snippets "
        "as ISO 639-1 language codes",
    )
    parser.add_argument(
        "--langs_file",
        type=str,
        required=False,
        help="path to text file on disk containing languages for which to fetch "
        "wiki snippets as ISO 639-1 language codes, one per line",
    )
    parser.add_argument(
        "--skip_langs",
        type=str,
        nargs="+",
        required=False,
        default=[],
        help="one or more languages for which to *skip* fetching wiki snippets "
        "as ISO 639-1 language codes, as a convenience",
    )
    parser.add_argument(
        "--outdir",
        type=str,
        required=False,
        help="path to directory on disk to which wiki snippets will be saved",
    )
    parser.add_argument(
        "--n_snippets",
        "-n",
        type=int,
        default=25,
        help="number of text snippets to save per language",
    )
    parser.add_argument(
        "--timeout",
        type=int,
        default=3600,
        help="maximum number of seconds to spend fetching snippets, per language",
    )
    parser.add_argument(
        "--mode",
        type=str,
        choices=["skip", "append", "overwrite"],
        default="skip",
        help="",
    )
    parser.add_argument(
        "--featured",
        action="store_true",
        default=False,
        help="if set, will attempt to get 'featured articles' for each language "
        "before falling back to random pages",
    )
    parser.add_argument(
        "--good",
        action="store_true",
        default=False,
        help="if set, will attempt to get 'good articles' for each language "
        "before falling back to random pages",
    )
    args = parser.parse_args(argv)

    if not bool(args.langs) ^ bool(args.langs_file):
        raise ValueError("either `langs` or `langs_file` must be specified")
    if args.langs_file:
        with io.open(args.langs_file, mode="rt", encoding="utf-8") as f:
            langs = [line.strip() for line in f]
    else:
        langs = args.langs

    outdir = os.path.realpath(os.path.expanduser(args.outdir)) if args.outdir else None
    featured = (
        get_all_lang_links("Category:Featured_articles", "en") if args.featured else None
    )
    good = get_all_lang_links("Category:Good_articles", "en") if args.good else None

    wikipedia.set_rate_limiting(True, min_wait=datetime.timedelta(milliseconds=10))
    wiki_langs = wikipedia.languages()

    for i, lang in enumerate(langs):
        if lang not in wiki_langs:
            raise ValueError(
                "lang='{}' is invalid; available langs are\n{}".format(
                    lang, sorted(wiki_langs.keys())
                )
            )
        wikilang = wiki_langs[lang]
        if lang in args.skip_langs:
            logging.info(
                "skipping lang %s '%s' (%s/%s) ...", wikilang, lang, i + 1, len(langs),
            )
            continue
        n_snippets = args.n_snippets
        mode = "wt"
        if outdir:
            fname = os.path.join(outdir, lang + ".txt")
            if os.path.isfile(fname):
                if args.mode == "skip":
                    logging.info(
                        "snippets for lang %s '%s' (%s/%s) already fetched, skipping ...",
                        wikilang,
                        lang,
                        i + 1,
                        len(langs),
                    )
                    continue
                else:
                    with io.open(fname, mode="rt", encoding="utf-8") as f:
                        n_snippets_existing = sum(1 for _ in f)
                    if args.mode == "append":
                        mode = "at"
                        n_snippets = max(n_snippets - n_snippets_existing, 0)
                        if n_snippets == 0:
                            logging.info(
                                "snippets for lang %s '%s' (%s/%s) already fetched, skipping ...",
                                wikilang,
                                lang,
                                i + 1,
                                len(langs),
                            )
                            continue
                    logging.info(
                        "fetching snippets for lang %s '%s' (%s/%s) "
                        "to %s %s existing snippets ...",
                        wikilang,
                        lang,
                        i + 1,
                        len(langs),
                        args.mode,
                        n_snippets_existing,
                    )
            else:
                logging.info(
                    "fetching snippets for lang %s '%s' (%s/%s) ...",
                    wiki_langs[lang],
                    lang,
                    i + 1,
                    len(langs),
                )
        else:
            logging.info(
                "fetching snippets for lang %s '%s' (%s/%s) ...",
                wiki_langs[lang],
                lang,
                i + 1,
                len(langs),
            )
        snippets = get_snippets(lang, n_snippets, featured, good, args.timeout)
        if outdir:
            with io.open(fname, mode=mode, encoding="utf-8") as f:
                for snippet in snippets:
                    f.write(snippet + "\n")
            logging.info("saved %s %s snippets to %s", len(snippets), lang, fname)
        else:
            logging.info(
                "fetched %s %s snippets but did not save to disk:\n%s\n...",
                len(snippets),
                lang,
                snippets[:3],
            )
Esempio n. 32
0
 def lang_select(self,lng):
     "set language for the request"
     print "Language set to:",wikipedia.languages()[lng]
     wikipedia.set_lang(lng)
Esempio n. 33
0
def is_right_lang(lang):
    return lang in wikipedia.languages()