Beispiel #1
0
 def set_general_setting(self):
     # global
     data_home = get_data_home() if not self.__config.has_option("global", "data_location") else \
         self.__config.get("global", "data_location")
     accessible_pivot = get_accessible_pivot() if not self.__config.has_option("global", "network_accessible_pivot") else \
         self.__config.get("global", "network_accessible_pivot")
     set_data_home(data_home)
     set_accessible_pivot(accessible_pivot)
     # search
     api_key = get_api_key() if not self.__config.has_option("search", "api_key") else \
         self.__config.get("search", "api_key")
     cx = get_cx() if not self.__config.has_option("search", "cx") else \
         self.__config.get("search", "cx")
     latency = get_latency() if not self.__config.has_option("search", "latency") else \
         float(self.__config.get("search", "latency"))
     search_size = get_search_size() if not self.__config.has_option("search", "search_size") else \
         int(self.__config.get("search", "search_size"))
     if self.__config.has_option("search", "img_size"):
         img_size = list(
             set(self.__config.get(
                 "search", "img_size").split('|')))  # 'set' to uniquify
     else:
         img_size = get_img_size()
     set_api_key(api_key)
     set_cx(cx)
     set_latency(latency)
     set_search_size(search_size)
     set_img_size(img_size)
     # image
     slideshow_rate = get_slideshow_rate() if not self.__config.has_option("image", "slideshow_rate") else \
         float(self.__config.get("image", "slideshow_rate"))
     fullscreen_mode2 = get_fullscreen_mode2() if not self.__config.has_option("image", "fullscreen_mode2") else \
         "True" == self.__config.get("image", "fullscreen_mode2")
     set_slideshow_rate(slideshow_rate)
     set_fullscreen_mode2(fullscreen_mode2)
     # phrase
     attach_rate = get_attach_rate() if not self.__config.has_option("phrase", "attach_rate") else \
         float(self.__config.get("phrase", "attach_rate"))
     font_size = get_font_size() if not self.__config.has_option("phrase", "font_size") else \
         float(self.__config.get("phrase", "font_size"))
     set_attach_rate(attach_rate)
     set_font_size(font_size)
     print("=======  iReminder setting  ============="
           )  # currently, we only show selected entries
     print("data home:      ", data_home)
     print("slideshow rate: ", slideshow_rate)
     print("attach rate:    ", attach_rate)
     print("latency:        ", latency)
     print("search size:    ", search_size)
     print("========================================")
Beispiel #2
0
 def crawl_by_asking_google_search(pattern, start, size, option=""):
     assert type(pattern) in [str, unicode]
     from util.global_def import get_api_key, get_cx
     api_key = get_api_key()
     cx = get_cx()
     if not api_key or not cx:
         if not Crawler._HAS_SHOW_NO_SEARCH_MSG:
             Crawler._HAS_SHOW_NO_SEARCH_MSG = True
             info("as api_key and cx for Google Custom Search is not available, no image search will be issued")
         return [], False
     size_option = "&imgSize=" + size if size else ""
     full_option = size_option + (option if option else "")
     base_url = 'https://www.googleapis.com/customsearch/v1?key=%s&cx=%s&searchType=image&num=%d' \
                '&q=' + pattern + '&start=%d' + full_option
     request_str = base_url % (api_key, cx, G_SEARCH_PER_REQ_SIZE, start)
     urls = []
     success = True
     try:
         r = requests.get(request_str)
         res = json.loads(r.text)
         if "error" in res:
             Crawler.print_error(res["error"])
             if "This API requires billing to be enabled on the project" in res["error"]["message"]:
                 # this is the 'out of quota' message
                 Crawler.__STOP_SEARCH = True
             return urls, False
         if 'items' not in res:
             info("cannot fetch newer image url list: empty query")
             return urls, True  # return 'True' is okay?
         for image_info in res['items']:
             assert 'link' in image_info
             url = image_info['link']
             urls.append(url)
     except TypeError as e:  # for unhandled error...
         info("cannot fetch newer image url list: %s" % str(e))
         success = False
     except requests.ConnectionError as e:
         info("cannot fetch newer image url list: %s" % str(e))
         success = False
     return urls, success
Beispiel #3
0
 def crawl_by_asking_google_search(pattern, start, size, option=""):
     assert type(pattern) in [str, unicode]
     from util.global_def import get_api_key, get_cx
     api_key = get_api_key()
     cx = get_cx()
     if not api_key or not cx:
         if not Crawler._HAS_SHOW_NO_SEARCH_MSG:
             Crawler._HAS_SHOW_NO_SEARCH_MSG = True
             info(get_msg(Msg.no_search_due_to_no_api_key_and_cx))
         return [], False
     size_option = "&imgSize=" + size if size else ""
     full_option = size_option + (option if option else "")
     base_url = 'https://www.googleapis.com/customsearch/v1?key=%s&cx=%s&searchType=image&num=%d' \
                '&q=' + pattern + '&start=%d' + full_option
     request_str = base_url % (api_key, cx, G_SEARCH_PER_REQ_SIZE, start)
     urls = []
     success = True
     try:
         r = requests.get(request_str)
         res = json.loads(r.text)
         if "error" in res:
             Crawler.print_error(res["error"])
             if "This API requires billing to be enabled on the project" in res["error"]["message"]:
                 # this is the 'out of quota' message
                 Crawler.__STOP_SEARCH = True
             return urls, False
         if 'items' not in res:
             info(get_msg(Msg.cannot_fetch_image_url), "empty query")
             return urls, True  # return 'True' is okay?
         for image_info in res['items']:
             assert 'link' in image_info
             url = image_info['link']
             urls.append(url)
     except TypeError as e:  # for unhandled error...
         info(get_msg(Msg.cannot_fetch_image_url), str(e))
         success = False
     except requests.ConnectionError as e:
         info(get_msg(Msg.cannot_fetch_image_url), str(e))
         success = False
     return urls, success
Beispiel #4
0
 def set_general_setting(self):
     lang = get_lang() if not self.__config.has_option("reminder", "lang") else \
         self.__config.get("reminder", "lang")
     if type(lang) is str:
         lang = EN if "EN" == lang else CHT if "CHT" == lang else None
     assert lang is not None
     data_home = get_data_home() if not self.__config.has_option("reminder", "data_location") else \
         self.__config.get("reminder", "data_location")
     slideshow_frequency = get_slideshow_frequency() if not self.__config.has_option("image", "slideshow_frequency") else \
         float(self.__config.get("image", "slideshow_frequency"))
     phrase_appear_ratio = get_phrase_appear_ratio() if not self.__config.has_option("phrase", "ratio") else \
         float(self.__config.get("phrase", "ratio"))
     api_key = get_api_key() if not self.__config.has_option("search", "api_key") else \
         self.__config.get("search", "api_key")
     cx = get_cx() if not self.__config.has_option("search", "cx") else \
         self.__config.get("search", "cx")
     search_latency = get_search_latency() if not self.__config.has_option("search", "search_latency") else \
         float(self.__config.get("search", "search_latency"))
     fullscreen_mode2 = get_fullscreen_mode2() if not self.__config.has_option("reminder", "fullscreen_mode2") else \
         "True" == self.__config.get("reminder", "fullscreen_mode2")
     verbose = get_verbose() if not self.__config.has_option("reminder", "verbose") else \
         "True" == self.__config.get("reminder", "verbose")
     set_lang(lang)
     set_data_home(data_home)
     set_slideshow_frequency(slideshow_frequency)
     set_phrase_appear_ratio(phrase_appear_ratio)
     set_search_latency(search_latency)
     set_api_key(api_key)
     set_cx(cx)
     set_fullscreen_mode2(fullscreen_mode2)
     set_verbose(verbose)
     print("=======  reminder setting  =============")
     print("data home:       ", data_home)
     print("slideshow:       ", slideshow_frequency)
     print("phrase ratio:    ", phrase_appear_ratio)
     print("search latency:  ", search_latency)
     print("api key:         ", api_key if "" != api_key else "None")
     print("cx:              ", cx if "" != cx else "None")
     print("========================================")