-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.py
55 lines (40 loc) · 1.81 KB
/
post.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
from os import environ
from jinja2 import Environment, select_autoescape, FileSystemLoader
import requests
import tweepy
from render import generate_image, generate_properties
import sheets
CONSUMER_KEY = environ["CONSUMER_KEY"]
CONSUMER_SECRET = environ["CONSUMER_SECRET"]
ACCESS_TOKEN = environ["ACCESS_TOKEN"] # straight from the Twitter project (OAuth 1.0)
ACCESS_TOKEN_SECRET = environ["ACCESS_TOKEN_SECRET"]
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
env = Environment(loader=FileSystemLoader("templates"), autoescape=select_autoescape())
def update_status(text: str, media_id: int) -> dict:
"""Posts an update to the Twitter account with attached molecule."""
response = api.update_status(status=text, media_ids=[media_id])
return response
def upload_file(filename: str) -> int:
"""Uploads a media file (jpg) to the Twitter API."""
media_object = api.media_upload(filename, chunked=False)
return media_object.media_id
def build_status(molecule: dict, template: str) -> str:
"""Builds the tweet string based on the Jinja template."""
template = env.get_template(template)
molecule["properties"] = generate_properties(molecule.get("smiles"))
tweet = template.render(**molecule)
return tweet
def main(sheet: str = "malaria"):
"""Posts a tweet with a randomly selected molecule."""
molecule = sheets.choose_molecule(sheet)
smiles_string = molecule.get("smiles")
if smiles_string == None:
return # fail early if we can't find a SMILES string
tweet = build_status(molecule, f"{sheet}.html")
new_filename = "molecule.png"
generate_image(smiles_string) # writes to new_filename
media_id = upload_file(new_filename)
update_status(tweet, media_id)
main()