-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestdriver4saxe.py
executable file
·131 lines (83 loc) · 3.59 KB
/
testdriver4saxe.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
126
127
128
129
130
#!/usr/bin/python3
"""
Test and example driver for saxe blue-sky library.
Revised 11 February 2024
A test image (in the public domain) is here.
User will need to prepare a bsky.app account beforehand, and supply
this program with the handle (@example.bsky.social)
"""
import requests
import json
from datetime import date, datetime, time, timezone, timedelta
from saxe_bluesky import bskyURLdict, get_DID, credentials_dict, open_session
from saxe_bluesky import get_author_feed, blob_basic_dict, blob_prep
from saxe_bluesky import blob_upload, simple_post_create, images_post_create
from PIL import Image
# Need to supply the account handle and an app password (established
# ahead of time in the web interface for bsky.app)
# # Get my permanent id (DID) and hang on to it for this session
# Replace example.bsky.social below with your handle, within the ' '
# Don't use the trailing '@'
MyHandle = {'handle':'example.bsky.social'}
Actor = MyHandle.get('handle')
# Provide a real app password here;
# See https://blueskyfeeds.com/en/faq-app-password as an example
APP_PASSWORD = "moo-moo-moo-moo"
# Use this info to start populating the credentials dictionary
bsky_creds = credentials_dict
bsky_creds['handle'] = MyHandle.get('handle')
bsky_creds['app_pwd'] = APP_PASSWORD
# Get the DID that goes with my handle
nicedid = get_DID (MyHandle)
bsky_creds['DID'] = nicedid
# Establish session, get API_Key (ephemeral)
API_token=open_session(bsky_creds)
bsky_creds['API_token'] = API_token
print ("My blue sky credentials are: \n", bsky_creds)
now_iso = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
now = datetime.now()
bsky_creds['API_create_time'] = now_iso
futuredate = datetime.now(timezone.utc) + timedelta(seconds=3)
futuredate_iso = futuredate.isoformat().replace("+00:00", "Z")
bsky_creds['API_Expiration'] = futuredate_iso
print ("My blue sky credentials now are: \n", bsky_creds)
# Get my (author) Feed
# Feed length can be from 1 to 100, inclusive
feed_length = 3
feed = json.loads (get_author_feed (bsky_creds, feed_length))
# Open and check a blob
IMAGE_PATH = "./testimage.webp"
image = Image.open(IMAGE_PATH)
blob_info = blob_prep(image)
# print (blob_info)
with open (IMAGE_PATH, "rb") as imagefile:
blob_bytes = imagefile.read()
# this size limit is specified in the app.bsky.embed.images lexicon
if len(blob_bytes) > 2000000:
raise Exception (
"image file size too large. 2MB maximum, got: {len(blob_bytes)}"
)
# print (len(blob_bytes))
# Note: the library currently doesn't (11 Feb 2024) use the imnage dimensions.
# To be implemented later (an optional item right now in the bsky api)
this_blob = blob_basic_dict
this_blob['imageWidth'] = blob_info['img_width']
this_blob['imageHeight'] = blob_info['img_height']
this_blob['imageMimeType'] = blob_info['img_format']
this_blob['imagesizebytes'] = len(blob_bytes)
this_blob['imageFilePath'] = IMAGE_PATH
this_blob['imageAltText'] = 'Test Image'
# Upload a blob to bsky, to be embedded in a post in a later step
# Provide link to the file, the blob data dictionary, and the credentials dictionary
# returns value of the internal bsky link to the blob, needed in the embed step next
bsky_link = blob_upload(blob_bytes, this_blob, bsky_creds)
this_blob['blobLink'] = bsky_link
# print (this_blob)
# Basic Post to my feed
# supply text to post and the bsky_credentials dictionary structure
simple_post_create ("And Now for more boring tests", bsky_creds)
# P
#images_post_create (text2post, credentials, blob_info_array, num_images)
blobs = [this_blob, this_blob]
images_post_create ("stuff", bsky_creds, blobs, 2)
exit ()