Skip to content

Commit 6817e0c

Browse files
committed
Save
1 parent 9e6e9d4 commit 6817e0c

File tree

4 files changed

+137
-3
lines changed

4 files changed

+137
-3
lines changed

Gemfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
source 'https://rubygems.org'
22

33
gem 'sinatra'
4-
gem 'discordrb'
4+
gem 'sqlite3'
55
gem 'httparty'
6+
gem 'dotenv'
67

Gemfile.lock

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
base64 (0.2.0)
5+
dotenv (2.8.1)
6+
httparty (0.21.0)
7+
mini_mime (>= 1.0.0)
8+
multi_xml (>= 0.5.2)
9+
mini_mime (1.1.5)
10+
multi_xml (0.6.0)
11+
mustermann (3.0.0)
12+
ruby2_keywords (~> 0.0.1)
13+
rack (3.0.9)
14+
rack-protection (4.0.0)
15+
base64 (>= 0.1.0)
16+
rack (>= 3.0.0, < 4)
17+
rack-session (2.0.0)
18+
rack (>= 3.0.0)
19+
ruby2_keywords (0.0.5)
20+
sinatra (4.0.0)
21+
mustermann (~> 3.0)
22+
rack (>= 3.0.0, < 4)
23+
rack-protection (= 4.0.0)
24+
rack-session (>= 2.0.0, < 3)
25+
tilt (~> 2.0)
26+
sqlite3 (1.7.2-aarch64-linux)
27+
sqlite3 (1.7.2-arm-linux)
28+
sqlite3 (1.7.2-arm64-darwin)
29+
sqlite3 (1.7.2-x86-linux)
30+
sqlite3 (1.7.2-x86_64-darwin)
31+
sqlite3 (1.7.2-x86_64-linux)
32+
tilt (2.3.0)
33+
34+
PLATFORMS
35+
aarch64-linux
36+
arm-linux
37+
arm64-darwin
38+
x86-linux
39+
x86_64-darwin
40+
x86_64-linux
41+
42+
DEPENDENCIES
43+
dotenv
44+
httparty
45+
sinatra
46+
sqlite3
47+
48+
BUNDLED WITH
49+
2.5.6

app.rb

+64-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,68 @@
11
require 'sinatra'
2+
require 'dotenv'
3+
require 'httparty'
4+
require 'json'
5+
require 'sqlite3'
26

3-
get '/' do
4-
"Hello, World!"
7+
Dotenv.load
8+
9+
BASE_URL = "https://warpcast.com"
10+
11+
# Initialize SQLite database and create table if not exists
12+
def init_db
13+
db = SQLite3::Database.new "casts.db"
14+
db.execute <<-SQL
15+
CREATE TABLE IF NOT EXISTS processed_casts (
16+
id INTEGER PRIMARY KEY,
17+
cast_hash VARCHAR(255) UNIQUE NOT NULL,
18+
username VARCHAR(255) NOT NULL
19+
);
20+
SQL
21+
db
22+
end
23+
24+
# Fetches the most recent casts from Neynar and processes them
25+
def fetch_and_process_casts
26+
url = "https://api.neynar.com/v2/farcaster/feed/channels?channel_ids=base&with_recasts=true&with_replies=false&limit=100"
27+
response = HTTParty.get(url, headers: { "accept" => "application/json", "api_key" => ENV['NEYNAR_API_KEY'] })
28+
return unless response.success?
29+
30+
casts = JSON.parse(response.body)["casts"]
31+
casts.each do |cast|
32+
cast_hash = cast["hash"]
33+
username = cast["author"]["username"]
34+
next if cast_exists?(cast_hash)
35+
36+
process_cast(cast_hash, username)
37+
end
38+
end
39+
40+
# Check if a cast already exists in the database
41+
def cast_exists?(cast_hash)
42+
DB.execute("SELECT 1 FROM processed_casts WHERE cast_hash = ?", cast_hash).any?
43+
end
44+
45+
# Process a new cast and store it in the database
46+
def process_cast(cast_hash, username)
47+
DB.execute("INSERT INTO processed_casts (cast_hash, username) VALUES (?, ?)", cast_hash, username)
48+
end
49+
50+
# Fetch all processed casts from the database
51+
def fetch_all_links
52+
rows = DB.execute("SELECT username, cast_hash FROM processed_casts")
53+
rows.map { |username, hash| "#{BASE_URL}/#{username}/#{hash}" }
54+
end
55+
56+
# Initialize database
57+
DB = init_db
58+
59+
get '/fetch_and_process' do
60+
fetch_and_process_casts
61+
"Casts fetched and processed successfully!"
62+
end
63+
64+
get '/links' do
65+
@links = fetch_all_links
66+
erb :links
567
end
668

views/links.erb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Cast Links</title>
7+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
8+
</head>
9+
<body class="bg-gray-100 text-gray-900">
10+
<div class="container mx-auto px-4 py-8">
11+
<h1 class="text-2xl font-bold mb-4">Cast Links</h1>
12+
<ul class="list-disc space-y-2">
13+
<% @links.each do |link| %>
14+
<li>
15+
<a href="<%= link %>" class="text-blue-500 hover:text-blue-700" target="_blank"><%= link %></a>
16+
</li>
17+
<% end %>
18+
</ul>
19+
</div>
20+
</body>
21+
</html>
22+

0 commit comments

Comments
 (0)