Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,39 +11,24 @@ class PaperManager:
|
|
| 11 |
self.papers = []
|
| 12 |
self.total_pages = 1
|
| 13 |
|
| 14 |
-
def calculate_score(self, paper):
|
| 15 |
-
upvotes = paper.get('paper', {}).get('upvotes', 0)
|
| 16 |
-
comments = paper.get('numComments', 0)
|
| 17 |
-
published_at_str = paper.get('publishedAt', datetime.now(timezone.utc).isoformat())
|
| 18 |
-
|
| 19 |
-
try:
|
| 20 |
-
published_time = datetime.fromisoformat(
|
| 21 |
-
published_at_str.replace('Z', '+00:00')
|
| 22 |
-
)
|
| 23 |
-
except ValueError:
|
| 24 |
-
# Handle incorrect date format
|
| 25 |
-
published_time = datetime.now(timezone.utc)
|
| 26 |
-
|
| 27 |
-
age_days = (datetime.now(timezone.utc) - published_time).total_seconds() / 86400 # Convert seconds to days
|
| 28 |
-
age_days = max(age_days, 0) # Prevent negative ages
|
| 29 |
-
|
| 30 |
-
# Calculate score using a logarithmic scale to balance upvotes and comments
|
| 31 |
-
score = (upvotes + comments) / ((age_days + 2) ** 1.5)
|
| 32 |
-
return score
|
| 33 |
-
|
| 34 |
def fetch_papers(self):
|
| 35 |
try:
|
| 36 |
response = requests.get(f"{API_URL}?limit=50")
|
| 37 |
response.raise_for_status()
|
| 38 |
data = response.json()
|
| 39 |
-
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
self.total_pages = (len(self.papers) + self.papers_per_page - 1) // self.papers_per_page
|
| 48 |
self.current_page = 1
|
| 49 |
return True
|
|
|
|
| 11 |
self.papers = []
|
| 12 |
self.total_pages = 1
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def fetch_papers(self):
|
| 15 |
try:
|
| 16 |
response = requests.get(f"{API_URL}?limit=50")
|
| 17 |
response.raise_for_status()
|
| 18 |
data = response.json()
|
| 19 |
+
|
| 20 |
+
# Sort papers primarily by 'publishedAt' descending, then by 'upvotes' descending
|
| 21 |
+
self.papers = sorted(
|
| 22 |
+
data,
|
| 23 |
+
key=lambda x: (
|
| 24 |
+
datetime.fromisoformat(
|
| 25 |
+
x.get('publishedAt', datetime.now(timezone.utc).isoformat()).replace('Z', '+00:00')
|
| 26 |
+
),
|
| 27 |
+
x.get('paper', {}).get('upvotes', 0)
|
| 28 |
+
),
|
| 29 |
+
reverse=True # Ensures descending order
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
self.total_pages = (len(self.papers) + self.papers_per_page - 1) // self.papers_per_page
|
| 33 |
self.current_page = 1
|
| 34 |
return True
|