跳转到主要内容

用于Giphy API的Python封装器

项目描述

giphypop 是对Giphy api的封装。它旨在提供一种更直观、更Pythonic的方式与Giphy api交互。

https://secure.travis-ci.org/shaunduncan/giphypop.png?branch=master

要求、安装和兼容性

唯一的依赖项包含在 requirements.txt 中,是 requests。如果您正在使用pip,可以安装 giphypop

$ pip install requests giphypop

或者

$ pip install requests
$ pip install -e git+https://github.com/shaunduncan/giphypop.git#egg=giphypop

然后您就可以开始使用。 giphypop 已经在python版本 2.6, 2.7, 3.2 和 3.3 上进行了测试。

入门

使用 giphypop 简单直接,旨在提供与api的交互,而不需要关注大量JSON数据。要开始,您可以使用包含API密钥测试各种功能。但是,请注意,虽然 Giphy 一直很慷慨地提供公开的“测试”密钥,但将来可能会关闭。有关更多信息,请参阅api文档

Giphy API交互的入口是giphypop.Giphy类。该类可选接受两个参数:api_keystrict。当未设置时,api_key参数默认为公开密钥(见上文)。strict参数控制当没有返回结果时API如何响应。如果启用,将引发异常,否则返回None

>>> import giphypop
>>> g = giphypop.Giphy()

现在你可以开始操作了。有几个关键的giphypop.Giphy对象的方法是你需要知道的

search_list

假设你期望search方法只给你一个列表而不是一个生成器。该方法将产生这样的效果。等同于

>>> g = giphypop.Giphy()
>>> results = [x for x in g.search('foo')]

translate

检索一个代表术语或短语翻译成动画Gif的单个图像。标点符号将被忽略。默认情况下,这将执行term翻译。如果你想通过短语翻译,请使用phrase关键字参数。

  • term:搜索术语或术语,字符串

  • phrase:搜索短语,字符串

  • strict:是否在无结果时引发异常,布尔值

gif

根据唯一ID从Giphy检索特定的Gif

  • gif_id:唯一的Giphy Gif ID,字符串

  • strict:是否在无结果时引发异常,布尔值

screensaver

根据给定的标签返回一个随机的Giphy图像,可选基于搜索。请注意,此方法将同时查询屏幕保护图像并获取该图像的完整详细信息(2个请求调用)

  • tag:限制通过标签返回的随机Gif,字符串

  • strict:是否在无结果时引发异常,布尔值

random_gif

giphypop.Giphy.screensaver的别名

upload

将视频或Gif上传到Giphy。上传完成后,请求完整的Gif详细信息并返回一个GiphyImage(2个请求调用)。

  • tags:用于上传Gif的标签列表,列表

  • file_path:要上传的文件路径,字符串

  • username:使用自己的API密钥上传时的账户用户名,字符串



处理结果

所有代表单个图像的结果都被包裹在giphypop.GiphyImage对象中。此对象的行为像字典,但也公开了作为属性的键。请注意,这些不是API响应对象的直接镜像;它们的目的是更简单。结构遵循以下布局

<Result Object>
    - id
    - type: image type (i.e. gif)
    - url: URL to giphy page
    - raw_data: copy of original data response from giphy (JSON)
    - fullscreen: bit.ly link to giphy fullscreen gif
    - tiled: bit.ly link to giphy tiled gif
    - bitly: bit.ly version of `url`
    - media_url: URL directly to image (original size)
    - frames: number of frames
    - height: image height (original image)
    - width: image width (original image)
    - size: filesize (in bytes, original image)
    - fixed_height: (variable width @ 200px height)
        - url: URL directly to image
        - width: image width
        - height: image height
        - downsampled:
            - url: URL directly to image
            - width: image width
            - height: image height
        - still: (a still image of gif)
            - url: URL directly to image
            - width: image width
            - height: image height
    - fixed_width: (variable height @ 200px width)
        - url: URL directly to image
        - width: image width
        - height: image height
        - downsampled:
            - url: URL directly to image
            - width: image width
            - height: image height
        - still: (a still image of gif)
            - url: URL directly to image
            - width: image width
            - height: image height

例如

>>> from giphypop import translate
>>> img = translate('foo')
>>> img.url
'http://giphy.com/foo/bar/baz'
>>> img.width
200
>>> img.fixed_height.downsampled.url
'http://giphy.com/foo/bar/downsampled'

上传

Giphy API 接受上传 GIF 或视频。您可以使用公开 API 密钥进行上传,但无法将它们分配给您的用户名或删除它们。为了上传到您的账户,在上传时设置 用户名 和 API 密钥。

例如

>>> from giphypop import upload
>>> gif = upload(["foo", "bar"], "mycat.gif")
>>> gif
GiphyImage<26BRvG76mOYcvRxss> at http://giphy.com/gifs/bar-foo-26BRvG76mOYcvRxss

或者使用您自己的 API 密钥上传到您的账户

>>> from giphypop import upload
>>> gif = upload(["foo", "bar"], "mycat.gif", username="gifsarefun", api_key="abcdef12345678")
>>> gif
GiphyImage<26BRvG76mOYcvRxss> at http://giphy.com/gifs/bar-foo-26BRvG76mOYcvRxss

变更日志

0.1

  • 初始版本

贡献和许可

Shaun Duncan 开发,并遵循 MIT 许可协议。欢迎并感谢贡献!

项目详情


下载文件

下载适用于您的平台文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

giphypop-0.3.tar.gz (10.3 kB 查看散列)

上传时间