-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
152 lines (133 loc) · 5.02 KB
/
Copy pathindex.js
File metadata and controls
152 lines (133 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
import gplay from 'google-play-scraper';
import { Parser } from 'json2csv';
import fs from 'fs/promises';
import path from 'path';
// Define main categories to search in
const CATEGORIES = [
gplay.category.APPLICATION,
gplay.category.GAME,
gplay.category.SOCIAL,
gplay.category.PRODUCTIVITY,
gplay.category.EDUCATION,
gplay.category.FINANCE,
gplay.category.COMMUNICATION
];
async function fetchTopFreeApps() {
try {
const apps = await gplay.list({
collection: gplay.collection.TOP_FREE,
country: 'in',
num: 100,
fullDetail: true
});
return apps.map((app, index) => ({
rank: index + 1,
title: app.title,
developer: app.developer,
mainCategory: app.type === 'GAME' ? 'Game' : 'Application',
subCategory: getCategoryName(app.genreId),
rating: app.scoreText || 'New',
installs: app.installs,
lastUpdated: new Date(app.updated).toLocaleDateString('en-IN', {
year: 'numeric',
month: 'short',
day: 'numeric'
})
}));
} catch (error) {
console.error('Error fetching top free apps:', error);
return [];
}
}
async function searchNewApps() {
const allNewApps = [];
const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
for (const category of CATEGORIES) {
try {
console.log(`Searching in category: ${category}`);
const apps = await gplay.list({
category: category,
collection: gplay.collection.TOP_FREE,
country: 'in',
num: 100,
fullDetail: true,
sort: gplay.sort.NEWEST
});
// Filter and process apps
const newApps = apps
.filter(app => {
const installCount = parseInt(app.installs.replace(/[^0-9]/g, ''));
return installCount < 1000000; // Filter out apps with > 1M installs as they're likely not new
})
.map((app, index) => ({
title: app.title,
developer: app.developer,
mainCategory: app.type === 'GAME' ? 'Game' : 'Application',
subCategory: getCategoryName(app.genreId),
rating: app.scoreText || 'New',
installs: app.installs,
lastUpdated: new Date(app.updated).toLocaleDateString('en-IN', {
year: 'numeric',
month: 'short',
day: 'numeric'
})
}));
allNewApps.push(...newApps);
// Add delay between category requests
await new Promise(resolve => setTimeout(resolve, 1000));
} catch (error) {
console.error(`Error fetching category ${category}:`, error);
}
}
// Remove duplicates based on app title
const uniqueApps = Array.from(new Map(allNewApps.map(app => [app.title, app])).values());
// Sort by install count (ascending) to prioritize newer apps
return uniqueApps
.sort((a, b) => {
const aInstalls = parseInt(a.installs.replace(/[^0-9]/g, ''));
const bInstalls = parseInt(b.installs.replace(/[^0-9]/g, ''));
return aInstalls - bInstalls;
})
.slice(0, 100);
}
function getCategoryName(appCategory) {
const cleanName = appCategory
.replace('GAME_', '')
.replace('APPLICATION_', '')
.split('_')
.map(word => word.charAt(0) + word.toLowerCase().slice(1))
.join(' ');
return appCategory.startsWith('GAME') ? `Game - ${cleanName}` : `App - ${cleanName}`;
}
async function saveToCSV(data, filename) {
try {
const parser = new Parser();
const csv = parser.parse(data);
const outputPath = path.join(process.cwd(), 'output', filename);
await fs.mkdir(path.join(process.cwd(), 'output'), { recursive: true });
await fs.writeFile(outputPath, csv);
console.log(`✓ Saved ${filename} successfully`);
} catch (error) {
console.error('Error saving CSV:', error);
}
}
async function main() {
const date = new Date().toISOString().split('T')[0];
console.log('Fetching top free apps...');
const topFreeApps = await fetchTopFreeApps();
if (topFreeApps.length > 0) {
await saveToCSV(topFreeApps, `top_free_apps_${date}.csv`);
console.log(`Found ${topFreeApps.length} top free apps`);
}
console.log('\nSearching for new apps across categories...');
const newApps = await searchNewApps();
if (newApps.length > 0) {
await saveToCSV(newApps, `potential_new_apps_${date}.csv`);
console.log(`Found ${newApps.length} potential new apps`);
}
}
main().catch(error => {
console.error('Script failed:', error);
process.exit(1);
});