Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ curl https://emoji-app.com/api/emoji/smiling%20face

# Get a heart emoji
curl https://emoji-app.com/api/emoji/red%20heart

# Get a random emoji
curl https://emoji-app.com/api/random
```

Response:
Response for specific or random emoji:
```json
{
"emoji": "😊",
Expand Down
204 changes: 138 additions & 66 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sqlite3
from datetime import datetime
import os
import random # Add import for random selection


app = Flask(__name__)
Expand All @@ -15,69 +16,6 @@
# Ensure the directory exists
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)

# Database initialization
def init_db():
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
# Create table for emoji usage counts
c.execute('''
CREATE TABLE IF NOT EXISTS emoji_counts (
emoji_name TEXT PRIMARY KEY,
copy_count INTEGER DEFAULT 0,
api_count INTEGER DEFAULT 0,
last_used TIMESTAMP
)
''')
# Create table for usage history
c.execute('''
CREATE TABLE IF NOT EXISTS usage_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
emoji_name TEXT,
usage_type TEXT,
timestamp TIMESTAMP,
FOREIGN KEY (emoji_name) REFERENCES emoji_counts (emoji_name)
)
''')
# Initialize counts for all emojis if they don't exist
for emoji_name in EMOJIS.keys():
c.execute('INSERT OR IGNORE INTO emoji_counts (emoji_name, copy_count, api_count) VALUES (?, 0, 0)', (emoji_name,))
conn.commit()
conn.close()

def get_emoji_counts():
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('SELECT emoji_name, copy_count + api_count as total_count FROM emoji_counts')
counts = {row[0]: row[1] for row in c.fetchall()}
conn.close()
return counts

def increment_count(emoji_name, usage_type):
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
if usage_type == 'copy':
c.execute('UPDATE emoji_counts SET copy_count = copy_count + 1, last_used = ? WHERE emoji_name = ?',
(datetime.now(), emoji_name))
else: # api
c.execute('UPDATE emoji_counts SET api_count = api_count + 1, last_used = ? WHERE emoji_name = ?',
(datetime.now(), emoji_name))

# Record in history
c.execute('INSERT INTO usage_history (emoji_name, usage_type, timestamp) VALUES (?, ?, ?)',
(emoji_name, usage_type, datetime.now()))

conn.commit()

# Get updated count
if usage_type == 'copy':
c.execute('SELECT copy_count FROM emoji_counts WHERE emoji_name = ?', (emoji_name,))
else:
c.execute('SELECT api_count FROM emoji_counts WHERE emoji_name = ?', (emoji_name,))
count = c.fetchone()[0]

conn.close()
return count

# Manual emoji dictionary
EMOJIS = {
# Faces and Expressions
Expand Down Expand Up @@ -402,6 +340,57 @@ def increment_count(emoji_name, usage_type):
"checkered flag": "🏁",
"triangular flag": "🚩",

# Flags
"flag argentina": "🇦🇷",
"flag australia": "🇦🇺",
"flag austria": "🇦🇹",
"flag belgium": "🇧🇪",
"flag brazil": "🇧🇷",
"flag canada": "🇨🇦",
"flag chile": "🇨🇱",
"flag china": "🇨🇳",
"flag colombia": "🇨🇴",
"flag denmark": "🇩🇰",
"flag egypt": "🇪🇬",
"flag finland": "🇫🇮",
"flag france": "🇫🇷",
"flag germany": "🇩🇪",
"flag greece": "🇬🇷",
"flag hong kong": "🇭🇰",
"flag iceland": "🇮🇸",
"flag india": "🇮🇳",
"flag indonesia": "🇮🇩",
"flag ireland": "🇮🇪",
"flag israel": "🇮🇱",
"flag italy": "🇮🇹",
"flag japan": "🇯🇵",
"flag malaysia": "🇲🇾",
"flag mexico": "🇲🇽",
"flag morocco": "🇲🇦",
"flag netherlands": "🇳🇱",
"flag new zealand": "🇳🇿",
"flag nigeria": "🇳🇬",
"flag norway": "🇳🇴",
"flag pakistan": "🇵🇰",
"flag philippines": "🇵🇭",
"flag poland": "🇵🇱",
"flag portugal": "🇵🇹",
"flag russia": "🇷🇺",
"flag saudi arabia": "🇸🇦",
"flag singapore": "🇸🇬",
"flag south africa": "🇿🇦",
"flag south korea": "🇰🇷",
"flag spain": "🇪🇸",
"flag sweden": "🇸🇪",
"flag switzerland": "🇨🇭",
"flag taiwan": "🇹🇼",
"flag thailand": "🇹🇭",
"flag turkey": "🇹🇷",
"flag uk": "🇬🇧",
"flag ukraine": "🇺🇦",
"flag usa": "🇺🇸",
"flag vietnam": "🇻🇳",

# Other
"rocket": "🚀",
"star": "⭐",
Expand All @@ -425,14 +414,84 @@ def increment_count(emoji_name, usage_type):
"books": "📚"
}

# Database initialization
def init_db():
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
# Create table for emoji usage counts
c.execute('''
CREATE TABLE IF NOT EXISTS emoji_counts (
emoji_name TEXT PRIMARY KEY,
copy_count INTEGER DEFAULT 0,
api_count INTEGER DEFAULT 0,
last_used TIMESTAMP
)
''')
# Create table for usage history
c.execute('''
CREATE TABLE IF NOT EXISTS usage_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
emoji_name TEXT,
usage_type TEXT,
timestamp TIMESTAMP,
FOREIGN KEY (emoji_name) REFERENCES emoji_counts (emoji_name)
)
''')
# Initialize counts for all emojis if they don't exist
for emoji_name in EMOJIS.keys():
c.execute('INSERT OR IGNORE INTO emoji_counts (emoji_name, copy_count, api_count) VALUES (?, 0, 0)', (emoji_name,))
conn.commit()
conn.close()

# Initialize database on app startup
init_db()

def get_emoji_counts():
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('SELECT emoji_name, copy_count + api_count as total_count FROM emoji_counts')
counts = {row[0]: row[1] for row in c.fetchall()}
conn.close()
return counts

def increment_count(emoji_name, usage_type):
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
if usage_type == 'copy':
c.execute('UPDATE emoji_counts SET copy_count = copy_count + 1, last_used = ? WHERE emoji_name = ?',
(datetime.now(), emoji_name))
else: # api
c.execute('UPDATE emoji_counts SET api_count = api_count + 1, last_used = ? WHERE emoji_name = ?',
(datetime.now(), emoji_name))

# Record in history
c.execute('INSERT INTO usage_history (emoji_name, usage_type, timestamp) VALUES (?, ?, ?)',
(emoji_name, usage_type, datetime.now()))

conn.commit()

# Get updated count
if usage_type == 'copy':
c.execute('SELECT copy_count FROM emoji_counts WHERE emoji_name = ?', (emoji_name,))
else:
c.execute('SELECT api_count FROM emoji_counts WHERE emoji_name = ?', (emoji_name,))
count = c.fetchone()[0]

conn.close()
return count

def get_all_emojis():
return EMOJIS

@app.route('/')
def index():
emojis = get_all_emojis()
counts = get_emoji_counts()
return render_template('index.html', emojis=emojis, counts=counts)

# Sort emojis by total usage count (descending)
sorted_emojis = dict(sorted(emojis.items(), key=lambda x: counts.get(x[0], 0), reverse=True))

return render_template('index.html', emojis=sorted_emojis, counts=counts)

@app.route('/api/emoji/<emoji_name>')
def get_emoji(emoji_name):
Expand Down Expand Up @@ -481,6 +540,19 @@ def get_stats():
conn.close()
return jsonify(top_emojis)

@app.route('/api/random')
def get_random_emoji():
emoji_name = random.choice(list(EMOJIS.keys()))
count = increment_count(emoji_name, 'api')
return jsonify({
'emoji': EMOJIS[emoji_name],
'name': emoji_name,
'count': count
})

@app.route('/api-docs')
def api_docs():
return render_template('api.html')

if __name__ == '__main__':
init_db()
app.run(debug=True)
app.run(debug=True)
118 changes: 118 additions & 0 deletions templates/api.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{% extends "base.html" %}

{% block title %}API Documentation - Emoji Library{% endblock %}

{% block extra_css %}
<style>
.endpoint {
background-color: #f8f9fa;
border-radius: 8px;
padding: 2rem;
margin-bottom: 2rem;
}
.endpoint h3 {
margin-top: 0;
color: #333;
}
.endpoint p {
margin-bottom: 1rem;
}
pre {
background-color: #333;
color: #fff;
padding: 1rem;
border-radius: 4px;
overflow-x: auto;
}
code {
font-family: 'Courier New', Courier, monospace;
}
.method {
display: inline-block;
padding: 0.25rem 0.5rem;
border-radius: 4px;
font-weight: bold;
margin-right: 0.5rem;
}
.get {
background-color: #28a745;
color: white;
}
.response {
margin-top: 1rem;
}
.url {
color: #007bff;
font-weight: bold;
}
</style>
{% endblock %}

{% block content %}
<h1>API Documentation</h1>
<p>Access our emoji collection programmatically through our REST API endpoints.</p>

<div class="endpoint">
<h3><span class="method get">GET</span> Get a Specific Emoji</h3>
<p>Retrieve a specific emoji by its name.</p>
<p><span class="url">https://emoji-app.com/api/emoji/{emoji_name}</span></p>
<pre><code>curl https://emoji-app.com/api/emoji/smiling%20face</code></pre>
<div class="response">
<p>Response:</p>
<pre><code>{
"emoji": "😊",
"name": "smiling face",
"count": 42
}</code></pre>
</div>
</div>

<div class="endpoint">
<h3><span class="method get">GET</span> Get a Random Emoji</h3>
<p>Get a random emoji from our collection.</p>
<p><span class="url">https://emoji-app.com/api/random</span></p>
<pre><code>curl https://emoji-app.com/api/random</code></pre>
<div class="response">
<p>Response:</p>
<pre><code>{
"emoji": "🚀",
"name": "rocket",
"count": 17
}</code></pre>
</div>
</div>

<div class="endpoint">
<h3><span class="method get">GET</span> View Popular Emojis</h3>
<p>Get the top 10 most used emojis with their usage statistics.</p>
<p><span class="url">https://emoji-app.com/api/stats</span></p>
<pre><code>curl https://emoji-app.com/api/stats</code></pre>
<div class="response">
<p>Response:</p>
<pre><code>[
{
"emoji": "😊",
"name": "smiling face",
"copy_count": 42,
"api_count": 12,
"last_used": "2024-01-01 12:00:00"
},
// ... more emojis
]</code></pre>
</div>
</div>

<div class="endpoint">
<h3><span class="method get">GET</span> Record Emoji Copy</h3>
<p>Record when an emoji is copied from the website.</p>
<p><span class="url">https://emoji-app.com/api/increment/{emoji_name}</span></p>
<pre><code>curl https://emoji-app.com/api/increment/rocket</code></pre>
<div class="response">
<p>Response:</p>
<pre><code>{
"success": true,
"count": 18
}</code></pre>
</div>
</div>
{% endblock %}
Loading
Loading