-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowdock_gif.py
executable file
·56 lines (50 loc) · 1.55 KB
/
flowdock_gif.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/bin/python3
import sys
import requests
import argparse
from pathlib import Path
from dotenv import dotenv_values
# Load conf
config = dotenv_values(f"{Path(__file__).parent.absolute()}/.env")
# Parse arguments
parser = argparse.ArgumentParser()
requiredNamed = parser.add_argument_group("required named arguments")
requiredNamed.add_argument(
"-u", "--user", help="Flowdock user recipient id", type=int, required=True
)
parser.add_argument(
"-t", "--tag", help="Giphy tag for the random search", type=str, default="unicorn"
)
parser.add_argument(
"-i",
"--id",
help="Giphy gif ID to send a specific gif instead of a random one",
type=str,
)
args = parser.parse_args()
if args.id:
# Retrieve specific gif
gif_r = requests.get(
f"https://api.giphy.com/v1/gifs/{args.id}",
params={"api_key": config["GIPHY_API_KEY"]},
)
else:
# Get a random gif
gif_r = requests.get(
"https://api.giphy.com/v1/gifs/random",
params={"tag": args.tag, "api_key": config["GIPHY_API_KEY"], "rating": "pg-13"},
)
if gif_r_dict := gif_r.json()["data"]:
gif_url = gif_r_dict["images"]["original"]["url"].split("?")[0]
print(f"Sending {gif_url} gif.")
requests.post(
f"https://api.flowdock.com/private/{args.user}/messages",
auth=(config["FLOWDOCK_API_TOKEN"], ""),
json={"event": "message", "content": f"/giphy {args.tag} \n{gif_url}"},
)
else:
print(
f"No gif found for {'ID' if args.id else 'tag'} '{args.id or args.tag}'.",
file=sys.stderr,
)
sys.exit(1)