-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
179 lines (155 loc) · 6.56 KB
/
Program.cs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using BlueskySharp.Endpoints;
namespace BlueskySharp.Demo.Demo01
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("Your handle >");
var handle = Console.ReadLine();
Console.Write("App Password >");
var password = Console.ReadLine();
Console.WriteLine("Logging in ...");
var service = BlueskyService.LoginWithLoginInfoAsync(
BlueskyServiceInfo.BskySocial,
new BlueskyLoginInfo() { Handle = handle, Password = password }).Result;
Console.WriteLine("Login is success !!");
Console.WriteLine("Get records ...");
// レコード一括取得テスト
var listRecordsResult = service.Repo.ListRecordsAsync(new()
{
Repo = handle,
Collection = "app.bsky.feed.post",
Limit = 10,
}).Result;
foreach (var record in listRecordsResult.Records)
{
var recordValue = record.Value;
Console.WriteLine("{0}: {1}", recordValue.CreatedAt.ToString("yyyy/MM/dd HH:mm:ss"), recordValue.Text.Replace("\n", "\\n"));
}
// 単一レコード取得テスト
var getRecordResult = service.Repo.GetRecordAsync(new()
{
Repo = "a32kita.net",
Collection = "app.bsky.feed.post",
Rkey = "3lbwng4waus2k",
}).Result;
// ログアウトのテスト
//try
//{
// service.Server.DeleteSessionAsync().Wait();
//}
//catch (AggregateException aex)
//{
// var ex = aex.InnerException as BlueskySharpErrorException;
// if (ex == null)
// throw;
// Console.WriteLine("Logout error: {0}, {1}", ex.Error, ex.ErrorMessage);
// Console.WriteLine(ex.SourceJson);
// Console.ReadLine();
//}
//catch (BlueskySharpErrorException ex)
//{
// Console.WriteLine("Logout error: {0}, {1}", ex.Error, ex.ErrorMessage);
// Console.WriteLine(ex.SourceJson);
// Console.ReadLine();
//}
while (true)
{
Console.Write("Post message >>");
var message = Console.ReadLine();
if (String.IsNullOrEmpty(message))
break;
// 画像のアップロード
Endpoints.Blob? blob = null;
using (var testImageStream = File.OpenRead("TestImage.png"))
{
var uploadResult = service.Repo.UploadBlobAsync(new("image/png", testImageStream)).Result;
blob = uploadResult?.Blob;
}
#if true
// com.atproto.repo.createRecord を使った方法
var postParam = new Endpoints.Repo.CreateRecordParam()
{
Repo = handle,
Collection = "app.bsky.feed.post",
Record = new()
{
Text = "TEST POST: " + message + " (" + DateTimeOffset.Now.ToString() + ")",
CreatedAt = DateTimeOffset.Now,
Embed = new()
{
Type = "app.bsky.embed.images",
Images =
[
// Specify image blob
new()
{
Alt = "Test image",
Image = blob,
}
]
}
}
};
var result = service.Repo.CreateRecordAsync(postParam).Result;
#else
// com.atproto.repo.applyWrites を使った方法
var writeParam = new Endpoints.Repo.ApplyWritesParam()
{
Repo = handle,
Validate = true,
Writes =
[
new()
{
Type = "com.atproto.repo.applyWrites#create",
Collection = "app.bsky.feed.post",
Value = new()
{
Text = "TEST POST (W): " + message + " (" + DateTimeOffset.Now.ToString() + ")",
CreatedAt = DateTimeOffset.Now,
Embed = new()
{
Type = "app.bsky.embed.images",
Images =
[
new()
{
Alt = "Test image",
Image = blob,
}
]
}
}
}
]
};
var result = service.Repo.ApplyWrites(writeParam).Result;
#endif
#if false
// Markdown 記法の利用
var md = "こんにちは!これはリンク埋め込みのテストです。\n\nこれは [GitHub へのリンク](https://github.com/) です。見えますか?";
//md = "hello, this is test post. This is [Link to GitHub](https://github.com/). Can you see?";
var recordFromMd = Endpoints.Record.FromMarkdownText(md + "\n\n" + Guid.NewGuid());
recordFromMd.Embed = Endpoints.Embed.FromExternal(new Endpoints.ExternalReference()
{
Title = "Hoge hoge Web site",
Description = "hoge foo bar",
Uri = new Uri("https://www.a32kita.net")
});
var postParam2 = new Endpoints.Repo.CreateRecordParam()
{
Repo = handle,
Collection = "app.bsky.feed.post",
Record = recordFromMd,
};
var result2 = service.Repo.CreateRecordAsync(postParam2).Result;
#endif
}
Console.WriteLine("Please press [Enter] key to exit ...");
Console.ReadLine();
}
}
}