예제 #1
0
파일: cmp.py 프로젝트: giuseppe82/pycon7
def get_data():
    return (
        ("facti", Option(map(lambda item: evaluate_func(facti, item), DEF_VALUES)).map(lambda item: list(item)).map(lambda item: [statistics.mean(element) for element in item]).get_or([])),
        ("fact", Option(map(lambda item: evaluate_func(fact, item), DEF_VALUES)).map(lambda item: list(item)).map(lambda item: [statistics.mean(element) for element in item]).get_or([])),
        ("fact_nt", Option(map(lambda item: evaluate_func(fact_nt, item), DEF_VALUES)).map(lambda item: list(item)).map(lambda item: [statistics.mean(element) for element in item]).get_or([])),
        ("fact_m", Option(map(lambda item: evaluate_func(fact_m, item), DEF_VALUES)).map(lambda item: list(item)).get_or([])),
        ) 
예제 #2
0
    def exctract_metadata(text, isbn, container=dict()):
        soup = BeautifulSoup(response_txt, "html.parser")

        Option(soup.find("span", {"id": "productTitle"}))\
            .map(lambda item: item.text)\
            .map(lambda item: setitem(container, "title", item))\
            .get_or(None)

        Option(soup.find("span", {"class": "author"}))\
            .map(lambda item: item.find("a"))\
            .map(lambda item: item.text)\
            .map(lambda item: setitem(container, "authors", item))\
            .get_or(None)

        Option(soup.find("div", {"id": "bookDescription_feature_div"}))\
            .map(lambda item: item.find("noscript"))\
            .map(lambda item: item.find("div"))\
            .map(lambda item: item.text)\
            .map(lambda item: setitem(container, "description", item))\
            .get_or(None)

        Option(soup.find("div", {"id": "img-canvas"}))\
            .map(lambda item: item.find("img"))\
            .map(lambda item: item.get("data-a-dynamic-image", None))\
            .map(lambda item: item.split("\""))\
            .map(lambda item: item[1])\
            .map(lambda item: setitem(container, "imageLinks", item))\
            .get_or(None)

        container["industryIdentifiers"] = isbn
        container["source"] = "AM"
        return container
예제 #3
0
 def get_embedding_user(cls, adapter, embedding):
     return Option(Oauth.query \
                   .filter(Oauth.oauth_adapter == adapter) \
                   .filter(Oauth.embedding_user == embedding) \
                   .first()) \
         .map(lambda x: x.user.username) \
         .get_or(None)
예제 #4
0
    def add_image(self, file: FileStorage, file_name: str,
                  convert: str) -> Option:
        if file and self.is_supported_filename(file_name):
            filename = secure_filename(file.filename)
            if not os.path.exists(self.upload_dir):
                os.mkdir(self.upload_dir)
            filename = datetime.datetime.now().strftime(
                "Upload_%Y%m%d%H%M%S_") + filename
            filename = ImageConverter(file, filename,
                                      convert).save(self.upload_dir)

            return Option((self.get_url(filename), filename))
        return Empty()
예제 #5
0
    def exctract_metadata(text, isbn, container=dict()):
        soup = BeautifulSoup(response_txt, "html.parser")
        Option(soup.find("span", {"itemprop": "name"}))\
            .map(lambda item: item.text)\
            .map(lambda item: setitem(container, "title", item))\
            .get_or(None)

        Option(soup.find("div", {"class": "head-intro"}))\
            .map(lambda item: item.find("h2"))\
            .map(lambda item: item.find("a"))\
            .map(lambda item: item.text)\
            .map(lambda item: setitem(container, "authors", item))\
            .get_or(None)

        Option(soup.find("div", {"id": "detail-content"}))\
            .map(lambda item: item.find("div", {"class": "block-content"}))\
            .map(lambda item: item.find("p"))\
            .map(lambda item: item.text)\
            .map(lambda item: setitem(container, "description", item))\
            .get_or(None)

        Option(soup.find("img", {"itemprop": "image"}))\
            .map(lambda item: "http:" + item["src"])\
            .map(lambda item: setitem(container, "imageLinks", item))\
            .get_or(None)

        Option(soup.find("div", {"id": "block-more-info"}))\
            .map(lambda item: item.find("ul"))\
            .map(lambda item: item.find("li"))\
            .map(lambda item: item.find("div"))\
            .map(lambda item: item.find("a"))\
            .map(lambda item: setitem(container, "tags", item.text))\
            .get_or(None)

        container["industryIdentifiers"] = isbn
        container["source"] = "LAF"
        print(container)
        return container
예제 #6
0
def index(request):
    amazon_url = "http://www.amazon.it/gp/search/search-alias=stripbooks&field-isbn="
    laf_url = "http://www.lafeltrinelli.it/libri/rfrrrfrre/rdferd/"

    flattened_qs = Option(request.query_string)\
        .map(lambda item: item.split("&"))\
        .map(lambda exqs: [(item.split("=")[0], item.split("=")[1]) for item in exqs])\
        .map(lambda item: dict(item)).get_or(dict())

    data = yield from asyncio.gather(*[
        asyncio.Task(
            handle_amazon_scraping(url="%s%s" %
                                   (amazon_url, flattened_qs["isbn"]),
                                   isbn=flattened_qs["isbn"])),
        asyncio.Task(
            handle_laf_scraping(url="%s%s" % (laf_url, flattened_qs["isbn"]),
                                isbn=flattened_qs["isbn"]))
    ])
    return aiohttp.web.Response(text=json.dumps(data),
                                content_type="application/json")
예제 #7
0
def addphoto(request):
    def save_content(content):
        path = os.path.join("uploaded", str(random.random()) + ".jpeg")
        with open(path, "wb") as output:
            output.write(content)
        return path

    data = yield from request.post()

    Option(data.get("file", None))\
        .map(lambda item: getattr(item, "file"))\
        .map(lambda item: save_content(item.read()))\
        .map(lambda item: resize_images(item))\
        .map(lambda item: get_all_images())\
        .get_or([])

    r = web.Response(text=json.dumps(
        [dict(src="../uploaded/" + value) for value in get_all_images()]),
                     content_type="application/json")
    r.headers['Access-Control-Allow-Origin'] = '*'
    return r
예제 #8
0
 def new_record(cls,
                application: str,
                key: str,
                value: str,
                username=None,
                index_key=None):
     user = Option(username).map(UserManager.find_user).get_or(None)
     if username:
         record = cls.raw_query(application, key, username)
     else:
         record = cls.raw_query(application, key)
     if record.count():
         return False
     record = PluginStorage()
     if user:
         record.user = user.user_id
     record.application = application
     record.key = key
     record.index_key = index_key
     record.content = value
     db.session.add(record)
     db.session.commit()
     return True
예제 #9
0
 def get_user_id(cls, username):
     return Option(username) \
         .map(UserManager.find_user) \
         .map(lambda x: x.user_id) \
         .get_or(None)
예제 #10
0
파일: corpus.py 프로젝트: living1069/libfnl
def GetMeta(root, name) -> Option(str):
    for meta in root.find(HEAD_TAG).iter(META_TAG):
        if meta.get('name') == name:
            return meta.get('content')

    return None
예제 #11
0
파일: corpus.py 프로젝트: living1069/libfnl
def GetEncoding(root) -> Option(str):
    for meta in root.find(HEAD_TAG).iter(META_TAG):
        if meta.get('charset') is not None:
            return meta.get('charset')

    return None
예제 #12
0
 def find_user_option(cls, by):
     return Option(
         User.query.get(by) if isinstance(by, int)
         else User.query.filter(sql_funcs.func.lower(User.username) == sql_funcs.func.lower(by)).first()
     )
예제 #13
0
 def username(self):
     return Option(self.token) \
         .map(token_processor.get_username) \
         .filter(itemgetter(0)) \
         .map(itemgetter(1)).map(itemgetter('username')).get_or(None)
예제 #14
0
 def get_db_post(self, pid) -> Post:
     return Option(
         Post.query.filter(
             sql_funcs.func.lower(Post.display_id) ==
             sql_funcs.func.lower(pid)).first()
         if isinstance(pid, str) else Post.query.get(pid)).get_or(None)