示例#1
0
 def query(self, query :Query):
     (data, success) = (None, False)
     if self.is_cached(query):
         (data, success) = self.read(query)
         if not success:
             Log.warning("CacheConnector.query(%s): Unreadable cache" % query)
     if not success:
         data = self.child.query(query)
     if query.action == ACTION_READ and self.is_cachable(query, data):
         self.write(query, data)
     return self.answer(query, data)
示例#2
0
def country_code_to_name(country_code :str) -> str:
    ret = None

    try:
        country = pycountry.countries.get(alpha_2 = country_code.upper())
        ret = country.name
    except KeyError as e:
        if "%s" % e == "alpha_2":
            ret = _country_code_to_name(country_code)
        else:
            Log.warning("Unknown country %r" % country_code)

    return ret
示例#3
0
def _country_code_to_name(country_code :str) -> str:
    # For obsolete version of pycountry
    Log.warning("Please update python3-pycountry; apt-get update && apt-get upgrade")

    ret = None

    try:
        country = pycountry.countries.get(alpha_2 = country_code.upper())
        ret = country.name
    except KeyError as e:
        Log.warning("Unknown country %r" % country_code)

    return ret
示例#4
0
def tweet_to_dict(tweet):
    # Fetch tweet
    content = None
    try:
        content = tweet._json["full_text"]
    except:
        content = tweet._json["text"]
    if content:
        content = content.split(" https://t.co/")[0]

    # Get tweet image
    image = None
    try:
        image = tweet._json["entities"]["media"][0]["media_url"]
    except:
        # Try to get image from youtube, e.g: https://img.youtube.com/vi/iZCyv0e2RLM/1.jpg
        try:
            if "youtu" in tweet._json["entities"]["urls"][0]["display_url"]:
                image = "https://img.youtube.com/vi/%s/1.jpg" % tweet._json[
                    "entities"]["urls"][0]["display_url"].split("/")[-1]
        except:
            Log.warning("No image found for tweet %s" % content)

    # Get author name
    author_name = None
    try:
        author_name = tweet._json["user"]["name"]
    except:
        Log.warning("No author name found for tweet %s" % content)

    # Get author image
    author_image = None
    try:
        author_image = tweet._json["user"]["profile_image_url"]
    except:
        Log.warning("No author image found for tweet %s" % content)

    # Get tweet date
    date_list = tweet._json["created_at"].split()
    display_date = "%(m)s %(d)s, %(y)s" % {
        "d": date_list[2],
        "m": date_list[1],
        "y": date_list[5]
    }
    date = datetime.datetime.strptime(display_date, "%b %d, %Y")
    display_date = date.strftime("%d/%m/%Y")

    # Return entry
    return {
        "text": content,
        "image": image,
        "date": date,
        "display_date": display_date,
        "author_name": author_name,
        "author_image": author_image,
    }
示例#5
0
#
# This file is part of the minifold project.
# https://github.com/nokia/minifold

__author__     = "Marc-Olivier Buob"
__maintainer__ = "Marc-Olivier Buob"
__email__      = "*****@*****.**"
__copyright__  = "Copyright (C) 2018, Nokia"
__license__    = "BSD-3"

from minifold.log import Log

try:
    import pycountry
except ImportError:
    Log.warning("Please install pycountry: apt-get install python3-pycountry")

def _country_code_to_name(country_code :str) -> str:
    # For obsolete version of pycountry
    Log.warning("Please update python3-pycountry; apt-get update && apt-get upgrade")

    ret = None

    try:
        country = pycountry.countries.get(alpha_2 = country_code.upper())
        ret = country.name
    except KeyError as e:
        Log.warning("Unknown country %r" % country_code)

    return ret
示例#6
0
__author__     = "Marc-Olivier Buob"
__maintainer__ = "Marc-Olivier Buob"
__email__      = "*****@*****.**"
__copyright__  = "Copyright (C) 2018, Nokia"
__license__    = "BSD-3"

from minifold.log import Log

try:
    import pycountry
except ImportError as e:
    from .log import Log
    Log.warning(
        "\n".join([
            "Please install requests",
            "  APT: sudo apt install python3-pycountry",
            "  PIP: sudo pip3 install --upgrade pycountry",
        ])
    )
    raise e

def _country_code_to_name(country_code :str) -> str:
    # For obsolete version of pycountry
    Log.warning("Please update python3-pycountry; apt-get update && apt-get upgrade")

    ret = None

    try:
        country = pycountry.countries.get(alpha_2 = country_code.upper())
        ret = country.name
    except KeyError as e:
示例#7
0
__author__ = "Marc-Olivier Buob"
__maintainer__ = "Marc-Olivier Buob"
__email__ = "*****@*****.**"
__copyright__ = "Copyright (C) 2018, Nokia"
__license__ = "BSD-3"

import os, sys
from minifold.cache import DEFAULT_CACHE_STORAGE_BASE_DIR, DEFAULT_CACHE_STORAGE_LIFETIME
from minifold.filesystem import check_writable_directory, mkdir

try:
    import requests_cache
except ImportError as e:
    from minifold.log import Log
    Log.warning("Please install requests-cache.\n"
                "  APT: sudo apt install python3-requests-cache\n"
                "  PIP: sudo pip3 install --upgrade requests-cache\n")
    raise e


def install_cache(cache_filename: str = None):
    if not cache_filename:
        directory = DEFAULT_CACHE_STORAGE_BASE_DIR
        cache_filename = os.path.join(DEFAULT_CACHE_STORAGE_BASE_DIR,
                                      "requests_cache")
    else:
        directory = os.path.dirname(cache_filename)
    mkdir(directory)
    check_writable_directory(directory)
    requests_cache.install_cache(cache_filename,
                                 expire_after=DEFAULT_CACHE_STORAGE_LIFETIME)