1
0
card/public/book/1/AGlkOnBsYzo0aHFqZm43bTZuNWhubzNkb2FtdWhnZWY/c4/3.html

771 lines
23 KiB
HTML
Raw Normal View History

2023-03-31 19:52:34 +00:00
<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>base64 · hello world! bluesky</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="@gitbook-ng/gitbook 3.3.6">
<meta name="author" content="syui">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-codeblock-filename/block.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="../end/" />
<link rel="prev" href="2.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="../">
<a href="../">
hello world! bluesky
</a>
</li>
<li class="chapter " data-level="1.2" data-path="../c1/">
<a href="../c1/">
part 1
</a>
<ul class="articles">
<li class="chapter " data-level="1.2.1" data-path="../c1/1.html">
<a href="../c1/1.html">
quick start
</a>
</li>
<li class="chapter " data-level="1.2.2" data-path="../c1/2.html">
<a href="../c1/2.html">
example
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3" data-path="../c2/">
<a href="../c2/">
part 2
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="../c2/1.html">
<a href="../c2/1.html">
bluesky
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="../c2/2.html">
<a href="../c2/2.html">
terminal
</a>
</li>
<li class="chapter " data-level="1.3.3" data-path="../c2/3.html">
<a href="../c2/3.html">
shell
</a>
</li>
<li class="chapter " data-level="1.3.4" data-path="../c2/4.html">
<a href="../c2/4.html">
rust
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" data-path="../c3/">
<a href="../c3/">
part 3
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="../c3/1.html">
<a href="../c3/1.html">
hello world
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="../c3/2.html">
<a href="../c3/2.html">
seahorse
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="../c3/3.html">
<a href="../c3/3.html">
reqwest
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" data-path="./">
<a href="./">
part 4
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="0.html">
<a href="0.html">
ai
</a>
</li>
<li class="chapter " data-level="1.5.2" data-path="1.html">
<a href="1.html">
config
</a>
</li>
<li class="chapter " data-level="1.5.3" data-path="2.html">
<a href="2.html">
mention
</a>
</li>
<li class="chapter active" data-level="1.5.4" data-path="3.html">
<a href="3.html">
base64
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.6" data-path="../end/">
<a href="../end/">
end
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href=".." >base64</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h3 id="base64">base64</h3>
<p>Next, write the code to convert the characters specified in the command options to <a href="https://uma0317.github.io/rust-cookbook-ja/encoding/strings.html" target="_blank">base64</a> and <code>mention</code>.</p>
<p>This completes the program.</p>
<p>First, add the <code>base64</code> package.</p>
<div><p class="code-filename">Cargo.toml</p></div>
<pre><code class="lang-toml"><span class="hljs-section">[package]</span>
<span class="hljs-attr">name</span> = <span class="hljs-string">&quot;ai&quot;</span>
<span class="hljs-attr">version</span> = <span class="hljs-string">&quot;0.1.0&quot;</span>
<span class="hljs-attr">edition</span> = <span class="hljs-string">&quot;2021&quot;</span>
<span class="hljs-section">
[dependencies]</span>
<span class="hljs-attr">seahorse</span> = <span class="hljs-string">&quot;*&quot;</span>
<span class="hljs-attr">reqwest</span> = { version = <span class="hljs-string">&quot;*&quot;</span>, features = [<span class="hljs-string">&quot;blocking&quot;</span>, <span class="hljs-string">&quot;json&quot;</span>] }
<span class="hljs-attr">tokio</span> = { version = <span class="hljs-string">&quot;1&quot;</span>, features = [<span class="hljs-string">&quot;full&quot;</span>] }
<span class="hljs-attr">serde_derive</span> = <span class="hljs-string">&quot;1.0&quot;</span>
<span class="hljs-attr">serde_json</span> = <span class="hljs-string">&quot;1.0&quot;</span>
<span class="hljs-attr">serde</span> = <span class="hljs-string">&quot;*&quot;</span>
<span class="hljs-attr">config</span> = { git = <span class="hljs-string">&quot;https://github.com/mehcode/config-rs&quot;</span>, branch = <span class="hljs-string">&quot;master&quot;</span> }
<span class="hljs-attr">shellexpand</span> = <span class="hljs-string">&quot;*&quot;</span>
<span class="hljs-attr">toml</span> = <span class="hljs-string">&quot;*&quot;</span>
<span class="hljs-attr">iso8601-timestamp</span> = <span class="hljs-string">&quot;0.2.10&quot;</span>
<span class="hljs-attr">base64</span> = <span class="hljs-string">&quot;*&quot;</span>
</code></pre>
<p>Then, in <code>src/main.rs</code>, in the <code>mention</code>, write the code to convert the <code>did</code> to base64.</p>
<p>These are set to suboptions.</p>
<p>Here is a summary of the main points.</p>
<div><p class="code-filename">example</p></div>
<pre><code class="lang-rust">.command(
Command::new(<span class="hljs-string">&quot;mention&quot;</span>)
.alias(<span class="hljs-string">&quot;m&quot;</span>)
.action(c_mention)
.flag(
Flag::new(<span class="hljs-string">&quot;base&quot;</span>, FlagType::<span class="hljs-built_in">String</span>)
.description(<span class="hljs-string">&quot;base flag\n\t\t\t$ ai m syui.bsky.social -p text -b 123&quot;</span>)
.alias(<span class="hljs-string">&quot;b&quot;</span>),
)
.flag(
Flag::new(<span class="hljs-string">&quot;egg&quot;</span>, FlagType::Bool)
.description(<span class="hljs-string">&quot;egg flag\n\t\t\t$ ai m syui.bsky.social -e&quot;</span>)
.alias(<span class="hljs-string">&quot;e&quot;</span>),
)
<span class="hljs-keyword">let</span> did = token_toml(&amp;<span class="hljs-string">&quot;did&quot;</span>);
<span class="hljs-keyword">let</span> body = <span class="hljs-string">&quot;/egg &quot;</span>.to_owned() + &amp;encode(did.as_bytes());
</code></pre>
<p>Allow <code>-b</code> to specify the string to be converted. Be sure to enclose the string in double quotation marks, for example <code>-b &quot;foo bar&quot;</code>. Use <code>-e</code> to get the <code>did</code> and convert it automatically for mention.</p>
<p>``sh</p>
<h1 id="convert-the-specified-string-to-base64-for-mention">convert the specified string to base64 for mention</h1>
<p>$ ai m yui.syui.ai -b &quot;did:plc:4hqjfn7m6n5hno3doamuhgef&quot;
@yui.syui.ai /egg ZGlkOnBsYzo0aHFqZm43bTZuNWhubzNkb2FtdWhnZWY=</p>
<h1 id="mention-your-did-as-base64">MENTION your did as base64</h1>
<p>$ ai m yui.syui.ai -e
@yui.syui.ai /egg ZGlkOnBsYzo0aHFqZm43bTZuNWhubzNkb2FtdWhnZWY=</p>
<pre><code class="lang-`">
Now, let&apos;s write the whole code.
!FILENAME src/main.rs
```rust
pub mod data;
pub mod mention;
pub mod profile;
//pub mod ascii;
use seahorse::{App, Command, Context, Flag, FlagType};
use std::env;
use std::fs;
use std::io::Write;
use std::collections::HashMap;
use data::Data as Datas;
use crate::data::Token;
use crate::data::Tokens;
use crate::data::Profile;
use crate::data::token_toml;
//use crate::ascii::c_ascii;
extern crate base64;
use base64::encode;
fn main() {
let args: Vec&lt;String&gt; = env::args().collect();
let app = App::new(env!(&quot;CARGO_PKG_NAME&quot;))
//.action(c_ascii_art)
.command(
Command::new(&quot;bluesky&quot;)
.alias(&quot;b&quot;)
.action(c_list_records),
)
.command(
Command::new(&quot;login&quot;)
.alias(&quot;l&quot;)
.action(c_access_token),
)
.command(
Command::new(&quot;profile&quot;)
.alias(&quot;p&quot;)
.action(c_profile),
)
.command(
Command::new(&quot;mention&quot;)
.alias(&quot;m&quot;)
.action(c_mention)
.flag(
Flag::new(&quot;post&quot;, FlagType::String)
.description(&quot;post flag\n\t\t\t$ ai m syui.bsky.social -p text&quot;)
.alias(&quot;p&quot;),
)
.flag(
Flag::new(&quot;base&quot;, FlagType::String)
.description(&quot;base flag\n\t\t\t$ ai m syui.bsky.social -p text -b 123&quot;)
.alias(&quot;b&quot;),
)
.flag(
Flag::new(&quot;egg&quot;, FlagType::Bool)
.description(&quot;egg flag\n\t\t\t$ ai m syui.bsky.social -e&quot;)
.alias(&quot;e&quot;),
)
)
;
app.run(args);
}
#[tokio::main]
async fn list_records() -&gt; reqwest::Result&lt;()&gt; {
let client = reqwest::Client::new();
let handle= &quot;support.bsky.team&quot;;
let col = &quot;app.bsky.feed.post&quot;;
let body = client.get(&quot;https://bsky.social/xrpc/com.atproto.repo.listRecords&quot;)
.query(&amp;[(&quot;repo&quot;, &amp;handle),(&quot;collection&quot;, &amp;col),(&quot;limit&quot;, &amp;&quot;1&quot;),(&quot;revert&quot;, &amp;&quot;true&quot;)])
.send()
.await?
.text()
.await?;
println!(&quot;{}&quot;, body);
Ok(())
}
fn c_list_records(_c: &amp;Context) {
list_records().unwrap();
}
#[tokio::main]
async fn access_token() -&gt; reqwest::Result&lt;()&gt; {
let file = &quot;/.config/ai/token.toml&quot;;
let mut f = shellexpand::tilde(&quot;~&quot;).to_string();
f.push_str(&amp;file);
let data = Datas::new().unwrap();
let data = Datas {
host: data.host,
handle: data.handle,
pass: data.pass,
};
let url = &quot;https://&quot;.to_owned() + &amp;data.host + &amp;&quot;/xrpc/com.atproto.server.createSession&quot;;
let mut map = HashMap::new();
map.insert(&quot;identifier&quot;, &amp;data.handle);
map.insert(&quot;password&quot;, &amp;data.pass);
let client = reqwest::Client::new();
let res = client
.post(url)
.json(&amp;map)
.send()
.await?
.text()
.await?;
let json: Token = serde_json::from_str(&amp;res).unwrap();
let tokens = Tokens {
did: json.did.to_string(),
access: json.accessJwt.to_string(),
refresh: json.refreshJwt.to_string(),
handle: json.handle.to_string(),
};
let toml = toml::to_string(&amp;tokens).unwrap();
let mut f = fs::File::create(f.clone()).unwrap();
f.write_all(&amp;toml.as_bytes()).unwrap();
Ok(())
}
fn c_access_token(_c: &amp;Context) {
access_token().unwrap();
}
fn profile(c: &amp;Context) {
let m = c.args[0].to_string();
let h = async {
let str = profile::get_request(m.to_string()).await;
println!(&quot;{}&quot;,str);
};
let res = tokio::runtime::Runtime::new().unwrap().block_on(h);
return res
}
fn c_profile(c: &amp;Context) {
access_token().unwrap();
profile(c);
}
fn mention(c: &amp;Context) {
let m = c.args[0].to_string();
let h = async {
let str = profile::get_request(m.to_string()).await;
let profile: Profile = serde_json::from_str(&amp;str).unwrap();
let udid = profile.did;
let handle = profile.handle;
let at = &quot;@&quot;.to_owned() + &amp;handle;
let e = at.chars().count();
let s = 0;
if let Ok(base) = c.string_flag(&quot;base&quot;) {
let body = &quot;/egg &quot;.to_owned() + &amp;encode(base.as_bytes());
let str = mention::post_request(body.to_string(), at.to_string(), udid.to_string(), s, e.try_into().unwrap()).await;
println!(&quot;{}&quot;,str);
}
if let Ok(post) = c.string_flag(&quot;post&quot;) {
let str = mention::post_request(post.to_string(), at.to_string(), udid.to_string(), s, e.try_into().unwrap()).await;
println!(&quot;{}&quot;,str);
}
if c.bool_flag(&quot;egg&quot;) {
let did = token_toml(&amp;&quot;did&quot;);
let body = &quot;/egg &quot;.to_owned() + &amp;encode(did.as_bytes());
println!(&quot;{}&quot;, body);
let str = mention::post_request(body.to_string(), at.to_string(), udid.to_string(), s, e.try_into().unwrap()).await;
println!(&quot;{}&quot;,str);
}
};
let res = tokio::runtime::Runtime::new().unwrap().block_on(h);
return res
}
fn c_mention(c: &amp;Context) {
access_token().unwrap();
mention(c);
}
//fn c_ascii_art(_c: &amp;Context) {
// c_ascii();
//}
</code></pre>
<pre><code class="lang-sh">cargo build
</code></pre>
<p>Done.</p>
<p>Now, if you specify <code>yui.syui.ai</code> as the <code>mention</code> and use the <code>-e</code> option, it will automatically convert your did to base64 and send it to you.</p>
<pre><code class="lang-sh">./target/debug/ai m yui.syui.ai <span class="hljs-_">-e</span>
</code></pre>
<p>However, this makes it difficult to execute the command.</p>
<p>In order to be able to run this command from anywhere, we will put <code>binary</code>, i.e., <code>.ai</code>, which we can do when we <code>cargo build</code>. /target/debug/ai<code>in</code>$PATH`.</p>
<div><p class="code-filename">linux</p></div>
<pre><code class="lang-sh">$ <span class="hljs-built_in">echo</span> <span class="hljs-variable">$PATH</span>|tr : <span class="hljs-string">&apos;\n&apos;</span>
/usr/bin
/usr/<span class="hljs-built_in">local</span>/bin
$ sudo cp -rf ./target/debug/ai /usr/<span class="hljs-built_in">local</span>/bin/
$ ai -h
Name:
ai
Flags:
-h, --help : Show <span class="hljs-built_in">help</span>
Commands:
b, bluesky :
l, login :
p, profile :
m, mention :
</code></pre>
<div><p class="code-filename">windows</p></div>
<pre><code class="lang-sh"><span class="hljs-variable">$ENV</span>:Path.Split(<span class="hljs-string">&quot;;&quot;</span>)
C:\Users\syui\scoop\apps\rust\current\bin
cp ~/scoop/rust/current/bin/
ai -h
</code></pre>
<p>Let&apos;s play around with making your own commands with <code>rust</code> like this.</p>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="2.html" class="navigation navigation-prev " aria-label="Previous page: mention">
<i class="fa fa-angle-left"></i>
</a>
<a href="../end/" class="navigation navigation-next " aria-label="Next page: end">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"base64","level":"1.5.4","depth":2,"next":{"title":"end","level":"1.6","depth":1,"path":"end/README.md","ref":"end/README.md","articles":[]},"previous":{"title":"mention","level":"1.5.3","depth":2,"path":"c4/2.md","ref":"c4/2.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","codeblock-filename","mermaid-gb3","diff"],"root":"./","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"codeblock-filename":{},"mermaid-gb3":{},"diff":{"type":"markdown","method":"diffChars","options":{}},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"syui","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"hello world! bluesky","gitbook":"*","description":"This is a bluesky ai-card example, and first rust"},"file":{"path":"c4/3.md","mtime":"2023-07-31T06:51:05.927Z","type":"markdown"},"gitbook":{"version":"3.3.6","time":"2023-07-31T07:16:09.509Z"},"basePath":"..","book":{"language":""}});
});
</script>
</div>
<script src="../gitbook/gitbook.js"></script>
<script src="../gitbook/theme.js"></script>
<script src="../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
<script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="../gitbook/gitbook-plugin-search/search.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
<script src="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>
</body>
</html>