Skip to content

Commit

Permalink
[Closes #1] hunted action
Browse files Browse the repository at this point in the history
  • Loading branch information
nathangathright committed Oct 22, 2020
1 parent 6aadd70 commit c512de1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 19 deletions.
85 changes: 69 additions & 16 deletions hunted.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,100 @@
'use strict';
const path = require('path');
const fs = require('fs');
const https = require('https');
const alfy = require('alfy');
const normalizeUrl = require('normalize-url');

const media = path.join(__dirname, 'media');
const token = alfy.config.get('token') || process.env.TOKEN;

if (!token) {
return alfy.error(new Error(`Please add a Product Hunt token as a variable`));
}

const query = `query { posts(url: "${alfy.input}", order: VOTES) {
const input = normalizeUrl(alfy.input);
const query = `query { posts(url: "${input}", order: VOTES) {
edges{
node{
id
name
url
thumbnail {
url(height: 120, width: 120)
}
}}}}`;

function ensureDirectoryExistence(filePath) {
var dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}

function download(filename, url, callback) {
ensureDirectoryExistence(filename)
let file = fs.createWriteStream(filename);

https.get(url, function (response) {
if (callback !== undefined) {
response.pipe(file).on('finish', () => {
callback(file);
});
}
});
}

if (!token) {
return alfy.error(new Error(`Please add a Product Hunt token as a variable`));
}

alfy
.fetch('https://api.producthunt.com/v2/api/graphql', {
method: 'POST',
// json: true,
json: false,
json: true,
headers: {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json"
},
// body: { query }
body: JSON.stringify({ query })
body: { query }
})
.then((json) => {
const data = json.data;
const edges = data.posts.edges;
const hasResults = (Array.isArray(edges) && edges.length)

if (!data) {
return alfy.log(json.errors ? json.errors : 'Unknown GraphQL Error');
}

alfy.log('All good!');
alfy.log(data.viewer.repositories.nodes);
const noResults = [{
title: 'Looks like this URL hasn’t been hunted!',
subtitle: 'Submit to Product Hunt',
arg: 'https://www.producthunt.com/posts/new'
}];

const results = edges.map(({node}) => {
const iconPath = path.join(media, `${node.id}`);

fs.exists(iconPath, exists => {
if (!exists) {
download(iconPath, node.thumbnail.url, () => {
return true;
});
}
});

const results = data.body.posts.edges.map((node) => {
return {
title: node.name,
arg: node.url
title: 'This URL has already been hunted!',
subtitle: `Open ${node.name} on Product Hunt`,
arg: node.url,
icon: {
path: iconPath
},
};
});

alfy.output(results);
if (hasResults) {
alfy.output(results)
}
else{
alfy.output(noResults)
}
});
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function download(filename, url, callback) {

hits.forEach(hit => {
const iconPath = path.join(media, `${hit.id}`);
const iconUrl = `https://ph-files.imgix.net/${hit.thumbnail.image_uuid}?auto=format&fit=crop&h=128&w=128`;
const iconUrl = `https://ph-files.imgix.net/${hit.thumbnail.image_uuid}?auto=format&fit=crop&h=120&w=120`;

fs.exists(iconPath, exists => {
if (!exists) {
Expand Down
3 changes: 2 additions & 1 deletion info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ fi</string>
<key>readme</key>
<string>Search Product Hunt posts from Alfred
Token is only needed for the `hunted` method, which is currently broken.</string>
The developer token is only needed for the `hunted` method, which is currently broken.
Locate yours by visiting https://api.producthunt.com/v2/docs and clicking “API Dashboard”</string>
<key>uidata</key>
<dict>
<key>1ACB45A9-A438-4781-8FD8-159A129D08ED</key>
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@algolia/cache-in-memory": "^4.5.1",
"alfy": "^0.10.0",
"algoliasearch": "^3.34.0",
"dotenv": "^8.2.0"
"dotenv": "^8.2.0",
"normalize-url": "^5.3.0"
}
}

0 comments on commit c512de1

Please sign in to comment.