-
Notifications
You must be signed in to change notification settings - Fork 2
/
gz-bot.py
125 lines (97 loc) · 3.08 KB
/
gz-bot.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
## Imports
from atproto import Client
import pandas as pd
import numpy as np
import requests
import time
from io import BytesIO
import os
## Functions
def pull_galaxy_image(url):
response = requests.get(url)
img_data = BytesIO(response.content)
return img_data
def create_metadata(row):
if type(row.redshift.iloc[0]) == str:
z = row.redshift.iloc[0]
else:
z = "%.2f" % ( row.redshift.iloc[0] )
ra = "%.5f" % row.ra.iloc[0]
dec = "%.5f" % row.dec.iloc[0]
clsf = row.galaxy_description.iloc[0]
survey = row.imaging.iloc[0]
n_class = "%d" % row.n_class.iloc[0]
if "CANDELS-COODS" in survey:
survey = 'CANDELS-GOODS'
project = row.project.iloc[0]
if 'Hubble' in project:
instr = 'Hubble Space Telescope'
elif 'CANDELS' in project:
instr = 'Hubble Space Telescope'
elif 'Galaxy Zoo 2' in project:
instr = 'Apache Point 2.5m Telescope'
t_lookback = row.t_lookback.iloc[0]
if t_lookback < 1:
tmp = t_lookback * 1000
t_lookback_string = '%.1f million years' % tmp
elif t_lookback >= 1:
t_lookback_string = '%.2f billion years' % t_lookback
else:
t_lookback_string = 'unknown'
vowels = ['a', 'e', 'i', 'o', 'u']
start = 'A'
if clsf[0] in vowels:
start = 'An'
else:
start = 'A'
random_no = np.random.random()
if random_no < (1./20.):
feed_emoji = "\U0001f52d"
else:
feed_emoji = ""
metadata = (
"""{} {}, observed with the {} in the {} survey.
It is at redshift {} (lookback time {}) with coordinates ({}, {}).
{} volunteers classified this galaxy in {}.{}
""").format(
start, clsf, instr, survey, z, t_lookback_string, ra, dec, n_class, project, feed_emoji
)
return metadata
def post(image, metadata, client, clsf, project, n_class):
alt_im_text = 'A {} from the {} project, classified by {} volunteers.'.format(clsf, project, n_class)
attempt = 0
while attempt < 5:
try:
response = (
client.send_image(
text = metadata,
image = image,
image_alt = alt_im_text
)
)
attempt = 10
except:
time.sleep(10)
attempt += 1
return response
## Main Function
def main():
# Initialising connection to the BlueSky Client.
client = Client()
# Getting log in details
usrname = os.environ['USRNAME']
pwd = os.environ['BLU_CODE']
cat_path = os.environ['CAT_PATH']
_ = client.login(usrname, pwd)
# Selecting a Galaxy to upload.
gal_data = pd.read_csv(cat_path).sample(1)
url = gal_data['image_url'].iloc[0]
# Creating the Post
image = pull_galaxy_image(url)
post_string = create_metadata(gal_data)
# Posting
response = post(image, post_string, client, gal_data.galaxy_description.iloc[0], gal_data.project.iloc[0], "%d" % gal_data.n_class.iloc[0])
print(response)
## Initialisation
if __name__ == '__main__':
main()