code: 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],
              )
  
file_path: textacy/scripts/fetch_wiki_lang_snippets.py
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import argparse
  import datetime
  import io
  import logging
  import os
  import random
  import re
  import sys
  import time
  import warnings
  import wikipedia
  from cld2 import detect
  from tqdm import tqdm
label_desc: Use image node_id 1 for calling a global function with example usage: main(argv) without return types