Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Community patreons #318

Merged
merged 8 commits into from
Jun 27, 2016
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
2 changes: 1 addition & 1 deletion all-html.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
INPUT="$@"
electron --enable-logging . -w 1920 -h 1080 --input "//$INPUT/index.html" --script ./script.js --outputDir "//bin" --show \
electron --enable-logging ./site/ -w 1920 -h 1080 --input "//$INPUT/index.html" --script ./site/script.js --outputDir "//bin" --show \
--scripts linkqueue.js checkmissing.js font.characters.js sitemap.js screengrab.js version.js
find $INPUT -name "*.html" | xargs -P 6 -n 1 htmlmin.sh
13 changes: 2 additions & 11 deletions electron-characters/FontCharacters.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,8 @@ import electron.main.App;
class FontCharacters {

public static function main() {
console.log( 'loading...' );
/*if( window.document.readyState == 'complete' ) {
var m = new Script();

} else {
window.document.addEventListener('DOMContentLoaded', function() {
trace( 'dom loaded' );*/
var fs = new FontCharacters();
/*}, false);

}*/
trace( 'loading...' );
var fs = new FontCharacters();
}

public function new() {
Expand Down
2 changes: 1 addition & 1 deletion electron-characters/build.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
--each

-main FontCharacters
-js ../font.characters.js
-js ../site/font.characters.js
2 changes: 1 addition & 1 deletion electron-checkmissing/CheckMissing.hx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class CheckMissing {
[{tag:'meta', name:'msapplication-TileColor', content:"#f15922"}, {}],
[{tag:'meta', name:'msapplication-TileImage'}, {content:"/mstile-144x144.png?v=wAANbdxLQn"}],
[{tag:'meta', name:'theme-color', content:"#ffffff"}, {}],
[{tag:'script', src:'/js/haxe.io.js'}, {async:'async', defer:'defer'}],
[{tag:'script', src:'/js/haxe.io.js'}, {async:'async', defer:'defer', 'data-community-patreons':'/data/patreons.json'}],
[{tag:'script', src:'/js/outbound-link-tracker.js'}, {async:'async', defer:'defer'}],
[{tag:'script', type:'text/javascript'}, {innerHTML:"(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
Expand Down
2 changes: 1 addition & 1 deletion electron-checkmissing/build.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
--each

-main CheckMissing
-js ../checkmissing.js
-js ../site/checkmissing.js
1 change: 1 addition & 0 deletions electron-controller/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class Main {
var inputDir = input.directory();
var ipcMain = require('electron').ipcMain;
var server = require('node-static');
trace( 'server root => ' + (Sys.getCwd() + '$inputDir').normalize() );
var files = untyped __js__("new {0}", server.Server((Sys.getCwd() + '$inputDir').normalize(), {headers:{'Content-Security-Policy': 'script-src \'none\';'}}) );

var ns = require('http').createServer(function (request, response) {
Expand Down
3 changes: 2 additions & 1 deletion electron-controller/Script.hx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class Script {

for (script in scripts) {
var name = script.withoutExtension();
loaded.set( script, require( '$__dirname/$script' ) );
trace( name );
loaded.set( script, require( '$__dirname/$script'.normalize() ) );

}

Expand Down
5 changes: 3 additions & 2 deletions electron-controller/build.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
--each

-main Main
-js ../index.js
-js ../site/index.js

--next

--no-traces
-main Script
-js ../script.js
-js ../site/script.js
4 changes: 2 additions & 2 deletions electron-linkqueue/build.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
-lib cmd
-D dce=full
-cp .
--no-traces
#--no-traces

--each

-main LinkQueue
-js ../linkqueue.js
-js ../site/linkqueue.js
102 changes: 102 additions & 0 deletions electron-patreon/PatreonController.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package ;

import js.Node.*;
import tink.Json.*;
import js.node.Fs.*;
import js.Node.process;
import js.node.Crypto.*;
import haxe.Serializer;
import haxe.Unserializer;
import haxe.ds.StringMap;
import haxe.Constraints.Function;

import CommunityPatreon.PatreonData;
import CommunityPatreon.PatreonPayload;
import CommunityPatreon.PatreonDuration;

using StringTools;
using haxe.io.Path;

@:cmd
class PatreonController {

private static var app:Dynamic;
private static var electron:Dynamic;
private static var ipcMain:{on:String->Function->Dynamic};

public static function main() {
electron = require('electron');
app = electron.app;
ipcMain = electron.ipcMain;
trace( 'starting');
app.on('ready', function() {
var m = new PatreonController( Sys.args() );
} );

app.on('window-all-closed', function() {
if (process.platform != 'darwin') {
app.quit();
}
});
}

@alias('i') public var input:String;
@alias('o') public var output:String;
@alias('s') public var script:String;
private var cwd = Sys.getCwd();
private var config:Dynamic = { webPreferences:{} };
private var payload:PatreonPayload;
private var counter:Int = 0;

public function new(args:Array<String>) {
@:cmd _;

if (script != null) config.webPreferences.preload = '$cwd/$script'.normalize();
if (input == null) app.quit();

readFile( '$cwd/$input'.normalize(), {encoding:'utf8'}, function(e, d) {
if (e != null) throw e;
process( payload = parse(d) );
} );
}

private function process(payload:PatreonPayload):Void {
for (page in payload.data) {
counter++;
var browser = untyped __js__('new {0}', electron.BrowserWindow)( config );
ipcMain.on(page.uri, updatePayload.bind(browser, page.uri, _, _));
browser.on('closed', function() browser = null );
browser.webContents.on('did-finish-load', onLoad.bind(browser, page));
browser.loadURL( page.uri );

}

}

private function onLoad(browser:Dynamic, data:PatreonData):Void {
trace( 'loaded ' + data.uri );
browser.webContents.openDevTools();
browser.webContents.send('payload', stringify(data));
}

private function updatePayload(browser:Dynamic, uri:String, event:Dynamic, json:String):Void {
for (i in 0...payload.data.length) if (payload.data[i].uri == uri) {
trace( uri );
payload.data[i] = parse(json);
break;

}
counter--;
browser.close();
if (counter < 1) {
trace( 'saving $input' );
writeFile( '$cwd/$output'.normalize(), stringify(payload), function(error) {
if (error != null) trace( error );
trace( 'exiting' );
app.quit();
} );

}
}

}
114 changes: 114 additions & 0 deletions electron-patreon/PatreonScraper.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package ;

import js.Node.*;
import js.node.Fs;
import tink.Json.*;
import js.Browser.*;
import js.html.Node;
import uhx.sys.Seri;
import js.html.Element;
import haxe.Serializer;
import haxe.extern.Rest;
import haxe.Unserializer;
import haxe.ds.StringMap;
import haxe.DynamicAccess;
import js.html.DOMElement;
import haxe.Constraints.Function;

import CommunityPatreon.PatreonData;
import CommunityPatreon.PatreonPayload;
import CommunityPatreon.PatreonDuration;

using StringTools;
using unifill.Unifill;

class PatreonScraper {

private static var whitespace = [for (codepoint in Seri.getCategory('Zs')) codepoint];
private var electron:Dynamic;
private var ipcRenderer:{on:String->Function->Dynamic, once:String->Function->Dynamic, send:String->Rest<Dynamic>->Void};

public static function main() {
var con:PatreonScraper = null;

switch (window.document.readyState) {
case 'complete':
con = new PatreonScraper();

case _:
window.document.addEventListener(
'DOMContentLoaded',
function() con = new PatreonScraper(),
false
);

}
}

public function new() {
trace( window.location );
electron = require('electron');
ipcRenderer = electron.ipcRenderer;
if (window.document.documentElement.outerHTML != '<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"></pre></body></html>') {
ipcRenderer.on('payload', function(e, d) process( parse(d) ));

}

}

private function process(data:PatreonData):Void {
trace( 'recieved payload' );
inline function qsa(selector:String):Array<Node> {
return [for (n in window.document.querySelectorAll( selector )) n];
}

var status:Array<Node> = qsa( '[class*="creatorInfoValue"], [class*="creatorInfoLabel"]' );
while (status.length > 0) {
var pair = [status.pop().textContent, status.pop().textContent];
switch (pair) {
case [count, 'patrons'] | ['patrons', count]:
trace( count, 'patrons' );
data.patrons = Std.parseInt( count );

case [value = _.startsWith('$') => true, duration] | [duration, value = _.startsWith('$') => true]:
trace( pair[0], pair[1] );
data.income = Std.parseFloat( value.substring(1) );
data.duration = clean( duration );

case [_, _]:
trace( pair );
}

}

var name = qsa( '[class*="creatorTitleName"]' )[0];
var isCreating = qsa( '[class*="IsCreating"]' )[0];
var lastUpdated = qsa( '[class*="timestamp"]' )[0];
var socials = qsa( '[class*="social"][href]' );
var description = qsa( '[class*="ToggleableContent"][class*="stackable"]' )[0];

if (name != null) data.name = clean( name.textContent.trim() );
if (isCreating != null) data.summary = clean( isCreating.textContent.trim() );
if (lastUpdated != null) data.update = clean( lastUpdated.textContent.trim() );
data.links = [for (social in socials) cast (social,DOMElement).getAttribute('href')];

trace( data );
trace( stringify(data) );
ipcRenderer.send(data.uri, stringify(data));
}

private static function clean(value:String):String {
for (codepoint in whitespace) {
value = value.uSplit( codepoint ).join(' ');

}

for (char in [',', '.']) if (value.endsWith(char)) {
value = value.substring(0, value.length-1);

}

return value.trim();
}

}
19 changes: 19 additions & 0 deletions electron-patreon/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-cp ../js-community-patreon/
-lib cmd
-lib hxnodejs
-lib thx.core
-lib tink_json
-lib seri
-D dce=full
-cp .
#--no-traces

--each

-main PatreonController
-js ../patreon/index.js

--next

-main PatreonScraper
-js ../patreon/scraper.js
2 changes: 1 addition & 1 deletion electron-screengrab/build.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
--each

-main ScreenGrab
-js ../screengrab.js
-js ../site/screengrab.js
2 changes: 1 addition & 1 deletion electron-sitemap/build.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
--each

-main Sitemap
-js ../sitemap.js
-js ../site/sitemap.js
2 changes: 1 addition & 1 deletion electron-version/build.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
--each

-main Version
-js ../version.js
-js ../site/version.js
Loading