After some additions to check that the data isn’t too old this is the first proper post to my ‘weather conditions’ account…
I just need to add the cron job now and it’s online.
After some additions to check that the data isn’t too old this is the first proper post to my ‘weather conditions’ account…
I just need to add the cron job now and it’s online.
Cron job is working so tomorrow I’ll be making the script(s) to post a WxSim forecast image 4 times a day.
It looks good. One thing I run into is the Bluesky 300-character limit. The error message you get back, at least to me, is confusing. It references a “rate limit exceeded,” not a character limit. It took me a bit to realize that.
Guy
I’ve improved the post by putting a link on each value that points to the relevant chart on my website. So if you want to see more about the temperature just click on the temperature value link. See the current example at this link…
BSky weather conditions post including links
I’ve linked the entire line for now, but I’m wondering whether to just link the name string or the value.
Yes please for the BlueSky script.
Which one? The original @beun.net PHP one, or the @grwkak modified PHP version or I’ve posted a Python one further up the thread.
I’ve now added tags to the Python script. The script is now quite speciifc to my requirements, but if anyone wants to use Python I’m happy to post it with some comments describing how you’d need to change it for your own purposes.
I’ve got image uploading working now. I still need to do some tidying up but I’m getting close to having what I set out to make.
The original @beun.net PHP one
I’ve sent you the ZIP file.
Including those links is really nice. But I would indeed go for the option of only placing a link on the value itself to make it clearer it’s clickable and why.
Other than that I am impressed by your Python code (and how little seems to be needed).
I think I’ve finished with this posting script, at least until someone suggests an improvement!
Latest changes are:
I have WxSim running every hour and the image is generated on the hour using the latest WxSim data. The BSky post is made a minute after the image generation runs.
The snip below is from this post
ok… this is looking good now, could someone send me the files needed for this please??
It’s not easy to do that, partly because I didn’t set out with the intention of making it general purpose for anyone to use. It comprises a number of components:
I will upload a version of the Python posting script with comments. I’ll leave the image processing bits in so that people can see how to use them, but if you want to post a forecast image then for now you’d need to do that bit for yourself and modify the Python to support your own method.
Here’s my latest Python script with some notes:
#!/root/bsky/bin/python3
from atproto import Client,client_utils
from urllib.request import urlopen
import datefinder
from datetime import datetime
import sys
import json
from pathlib import Path
def main():
client = Client()
profile = client.login('bluesky_handle', 'bluesky_password')
target_url = "URL to text file"
curr_dt = datetime.now()
pt = client_utils.TextBuilder()
i = 1
for line in urlopen(target_url):
text = line.decode("utf-8")
if i == 1:
date_matches = datefinder.find_dates(text)
for date_match in date_matches:
data_dt = datetime.strptime(str(date_match),"%Y-%m-%d %H:%M:%S")
data_age = curr_dt.timestamp() - data_dt.timestamp()
if data_age > 1000:
print("Not updating. Data too old - ",text)
sys.exit()
if text.find(": ") > -1:
parts = text.split(": ")
pt.text(parts[0]+": ")
if "Temperature:" in text:
pt.link(parts[1],'URL to your temperature web page')
elif "Rain Today:" in text:
pt.link(parts[1],'URL to your rain web page')
elif "Wind:" in text:
pt.link(parts[1],'URL to your wind web page')
elif "Max Gust:" in text:
pt.link(parts[1],'URL to your gusts web page (maybe just repeat wind)')
elif "Humidity:" in text:
pt.link(parts[1],'URL to your humidity web page')
elif "Sun:" in text:
pt.link(parts[1],'URL to your solar web page')
elif "UV:" in text:
pt.link(parts[1],'URL to your UV web page (maybe just repeat wind)')
elif "Pressure:" in text:
pt.link(parts[1],'URL to your pressure web page')
else:
pt.text(text)
i += 1
pt.text('\nMore ')
pt.link('conditions','URL of your weather web site')
pt.text(' More ')
pt.link('forecast\n','URL of your forecast web site')
pt.tag('#Blackpool ','blackpool')
pt.tag('#Weather','weather')
# Check if there's a recent forecast image to upload
with open('location of the forecast status JSON file on your server') as j:
image_data = json.load(j)
if curr_dt.timestamp() - image_data['image']['imageTime'] < 3600:
# Upload forecast image file
with open('location of the image JPEG file on your server', 'rb') as f:
img_data = f.read()
# Read alt text from file
alt_text = Path('location of the image alt-text file on your server').read_text()
client.send_image(text=pt, image=img_data, image_alt=alt_text)
else:
post = client.send_post(pt)
if __name__ == '__main__':
main()
To use:
Now that I’ve got the difficult script done I can move on to some others, e.g.
Just being text based makes these pretty easy to do.
Hi, I’m also interested in the PHP code to get posting to Bluesky if you’re able to share please
(Mastodon would be a bonus too if you can share both!)
(sidenote, have you thought about sharing the code on GitHub or even just as a gist on GitHub if it’s just a PHP file or few, to save people asking!)
@beun.net, @grwkak, your scpits work great!
Thank you.
Have you found a way to post the text with an image?
I couldn’t find any information or examples of how to use the PHP/cURL interface to upload and embed an image in a post. That’s why I swapped to using Python.
@beun.net is the main author - I changed a few words!
Guy