Class: Jekyll::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-bluesky.rb

Constant Summary collapse

API_URL =
'https://public.api.bsky.app'

Class Method Summary collapse

Class Method Details

.fetch_post(actor, limit) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jekyll-bluesky.rb', line 48

def self.fetch_post(actor, limit)
  response = HTTP.get("#{API_URL}/xrpc/app.bsky.feed.getAuthorFeed?actor=#{actor}&limit=#{limit}&filter=posts_and_author_threads")
  if response.status.success?
    data = JSON.parse(response.body)
    format_post(data)
  else
    error_details =
      begin
        JSON.parse(response.body)
      rescue JSON::ParserError
        response.body.to_s
      end

    "Error fetching post from Bluesky (status: #{response.status}). Details: #{error_details}"
  end
end

.format_post(data) ⇒ Object



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
# File 'lib/jekyll-bluesky.rb', line 65

def self.format_post(data)
  posts = data['feed']
  styles = <<~HTML
    <style>
      @font-face {
        font-family: 'InterVariable';
        src: url("https://web-cdn.bsky.app/static/media/InterVariable.c504db5c06caaf7cdfba.woff2") format('woff2');
        font-weight: 300 1000;
        font-style: normal;
        font-display: swap;
      }
      .bluesky-post {
        border-bottom: 1px solid #e1e8ed;
        padding: 12px;
        width: 500px;
        font-family: 'InterVariable', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Liberation Sans', Helvetica, Arial, sans-serif;
        background: #fff;
        margin-bottom: 10px;
      }
      .bluesky-header {
        display: flex;
        align-items: center;
        justify-content: flex-start; /* Alinha tudo à esquerda */
        gap: 8px; /* Espaço entre a foto e o nome */
      }
      .bluesky-avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
      }
      .bluesky-author-info {
        display: flex;
        flex-direction: column;
      }
      .author-name {
        font-weight: bold;
        font-size: 14px;
        color: #000;
      }
      .author-handle {
        font-size: 12px;
        color: #657786;
      }
      .bluesky-content {
        font-size: 14px;
        line-height: 1.5;
        color: #14171A;
      }
      .bluesky-footer {
        display: flex;
        justify-content: space-between;
        font-size: 12px;
        color: #657786;
        margin-top: 10px;
      }
      .icon {
        cursor: pointer;
      }
    </style>
  HTML

  formatted_posts = posts.map do |post|
    post_data = post['post']
    author = post_data['author']
    record = post_data['record']
    embed = post_data['embed']

    text = record['text'].gsub("\n", "<br>")
    author_name = author['displayName']
    author_handle = author['handle']
    post_time = "3h"

    image_html = ''
    if embed && embed['$type'] == 'app.bsky.embed.images#view'
      image_html = embed['images'].map do |image|
        <<~HTML
          <img src="#{image['thumb']}" alt="#{image['alt']}" class="bluesky-image" />
        HTML
      end.join
    end

    <<~HTML
      <div class="bluesky-post">
        <div class="bluesky-header">
          <img src="#{author['avatar']}" alt="#{author_name}" class="bluesky-avatar" />
          <div class="bluesky-author-info">
            <span class="author-name">#{author_name}</span>
            <span class="author-handle">@#{author_handle} · #{post_time}</span>
          </div>
        </div>
        <div class="bluesky-content">
          <p>#{text}</p>
          #{image_html}
        </div>
        <div class="bluesky-footer">
          <span class="icon">💬 #{post_data['replyCount']}</span>
          <span class="icon">🔁 #{post_data['repostCount']}</span>
          <span class="icon">❤️ #{post_data['likeCount']}</span>
          <span class="icon">···</span>
        </div>
      </div>
    HTML
  end.join("\n")

  styles + formatted_posts
end