Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: JavaScript  |  size: 8.61 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // ========================================================================================
  2. // SERVER
  3. // ========================================================================================
  4.  
  5. var require = __meteor_bootstrap__.require
  6. var dirname = __meteor_bootstrap__.__dirname;
  7.  
  8. // On server startup, create some players if the database is empty.
  9. Meteor.startup(function () {
  10.   var fs = require("fs")
  11.   var path = require("path")
  12.   var xmlFile = path.resolve(dirname, "iTunes Library.xml")
  13.  
  14.   // Remove all previous data
  15.   Songs.remove({})
  16.  
  17.   fs.readFile(xmlFile, Meteor.bindEnvironment(function (er, d) {
  18.         var sax = require("sax")
  19.         var xmlstr = d.toString("utf8")
  20.         var parser = sax.parser(true)
  21.         var songs = [];
  22.         var song = null
  23.         var currentTag = null
  24.  
  25.         console.log("Reading XML file.");
  26.        
  27.       parser.onclosetag = function (tagName) {
  28.         if (tagName === "dict") {
  29.           Songs.insert(song)
  30.           console.log("Inserting song.");
  31.           currentTag = song = null
  32.           return
  33.         }
  34.         if (currentTag && currentTag.parent) {
  35.           var p = currentTag.parent
  36.           delete currentTag.parent
  37.           currentTag = p
  38.         }
  39.       }
  40.  
  41.       parser.onopentag = function (tag) {
  42.         if (tag.name !== "dict" && !song) return
  43.         if (tag.name === "dict") {
  44.           song = tag
  45.         }
  46.         tag.parent = currentTag
  47.         tag.children = []
  48.         tag.parent && tag.parent.children.push(tag)
  49.         currentTag = tag
  50.       }
  51.  
  52.       parser.ontext = function (text) {
  53.         if (currentTag) currentTag.children.push(text)
  54.       }
  55.  
  56.       parser.onend = function () {
  57.         console.log("Done parsing!");
  58.       }
  59.  
  60.       parser.write(xmlstr).end()
  61.   }, logException))
  62.  
  63.   // Songs.remove({})
  64.   //  
  65.   //   if (Songs.find().count() === 0) {
  66.   //     var songs = [{
  67.   //                     name:"Holiday",
  68.   //                     artist: "Madonna",
  69.   //                     price: 500,
  70.   //                     genres: ["pop"],
  71.   //                     previewURL: "http://www.youtube.com/watch?v=0X7RyGBq2E8"
  72.   //                   },
  73.   //                   {
  74.   //                     name:"Take Five",
  75.   //                     artist: "Dave Bruebek",
  76.   //                     price: 500,
  77.   //                     genres: ["jazz", "instrumental"],
  78.   //                     previewURL: "http://www.youtube.com/watch?v=nzpnWuk3RjU"
  79.   //                   },
  80.   //                   {
  81.   //                     name:"Take a Minute",
  82.   //                     artist: "K'naan",
  83.   //                     price: 500,
  84.   //                     genres: ["hip hop"],
  85.   //                     previewURL: "http://www.youtube.com/watch?v=APw9ES0JpZo"
  86.   //                   },
  87.   //                   {
  88.   //                     name:"Video Games",
  89.   //                     artist: "Lana del Ray",
  90.   //                     price: 500,
  91.   //                     genres: ["pop"],
  92.   //                     previewURL: "http://www.youtube.com/watch?v=cE6wxDqdOV0"
  93.   //                   },
  94.   //                   {
  95.   //                     name:"Harvest Moon",
  96.   //                     artist: "Neil Young",
  97.   //                     price: 500,
  98.   //                     genres: ["folk"],
  99.   //                     previewURL: "http://www.youtube.com/watch?v=qVi0UvFu8Yo"
  100.   //                   },
  101.   //                   {
  102.   //                     name:"This is the way",
  103.   //                     artist: "Devandra Banhardt",
  104.   //                     price: 500,
  105.   //                     genres: ["folk"],
  106.   //                     previewURL: "http://www.youtube.com/watch?v=w74UVnCZ_QA"
  107.   //                   },
  108.   //                   {
  109.   //                     name:"Symphony No. 32 in G major",
  110.   //                     artist: "Wolfgang Amadeus Mozart",
  111.   //                     price: 500,
  112.   //                     genres: ["classical"],
  113.   //                     previewURL: "http://www.youtube.com/watch?v=W1bR6UJlThQ"
  114.   //                   },
  115.   //                   {
  116.   //                     name:"Madagascar",
  117.   //                     artist: "Art of Trance",
  118.   //                     price: 500,
  119.   //                     genres: ["electronic", "trance"],
  120.   //                     previewURL: "http://www.youtube.com/watch?v=bXFwOjm0_MU"
  121.   //                   },
  122.   //                   {
  123.   //                     name:"I Shall Be Released",
  124.   //                     artist: "Sting",
  125.   //                     price: 500,
  126.   //                     genres: ["pop", "ska"],
  127.   //                     previewURL: "http://www.youtube.com/watch?v=z3MDO3xS584"
  128.   //                   },
  129.   //                   {
  130.   //                     name:"At My Mother's",
  131.   //                     artist: "Warsaw Village Band",
  132.   //                     price: 500,
  133.   //                     genres: ["world","folk"],
  134.   //                     previewURL: "http://www.youtube.com/watch?v=5kc0re5Hcyc"
  135.   //                   },
  136.   //                   {
  137.   //                     name:"Had Me a Girl",
  138.   //                     artist: "Tom Waits",
  139.   //                     price: 500,
  140.   //                     genres: ["blues","folk"],
  141.   //                     previewURL: "http://www.youtube.com/watch?v=b2eGoCcEiIA"
  142.   //                   },
  143.   //                   {
  144.   //                     name:"Alive",
  145.   //                     artist: "Daft Punk",
  146.   //                     price: 500,
  147.   //                     genres: ["electronic","house"],
  148.   //                     previewURL: "http://www.youtube.com/watch?v=Rzt3BCpFj48"
  149.   //                   },
  150.   //                   {
  151.   //                     name:"Goldberg Variations",
  152.   //                     artist: "JS Bach",
  153.   //                     price: 500,
  154.   //                     genres: ["classical"],
  155.   //                     previewURL: "http://www.youtube.com/watch?v=g7LWANJFHEs"
  156.   //                   },
  157.   //                   {
  158.   //                     name:"I Shall Be Released",
  159.   //                     artist: "Bob Dylan",
  160.   //                     price: 500,
  161.   //                     genres: ["folk","rock"],
  162.   //                     previewURL: "http://www.youtube.com/watch?v=nAlMdhZRVXc"
  163.   //                   },
  164.   //                   {
  165.   //                     name:"I Shall Be Released",
  166.   //                     artist: "Nina Simone",
  167.   //                     price: 500,
  168.   //                     genres: ["blues","gospel"],
  169.   //                     previewURL: "http://www.youtube.com/watch?v=XyCn8IC5RpE"
  170.   //                   },
  171.   //                   {
  172.   //                     name:"Helpess",
  173.   //                     artist: "Neil Young",
  174.   //                     price: 500,
  175.   //                     genres: ["folk"],
  176.   //                     previewURL: "http://www.youtube.com/watch?v=7JTy-GCfmvw"
  177.   //                   },
  178.   //                   {
  179.   //                     name:"Hostile Territory",
  180.   //                     artist: "AnJ",
  181.   //                     price: 500,
  182.   //                     genres: ["Soundtrack"],
  183.   //                     previewURL: "http://media.songlily.com/AnJ/Unknown%20Album/Hostile%20Territory.mp3"
  184.   //                   },
  185.   //                   {
  186.   //                     name:"Agent vs Agent",
  187.   //                     artist: "AnJ",
  188.   //                     price: 500,
  189.   //                     genres: ["Soundtrack"],
  190.   //                     previewURL: "http://media.songlily.com/AnJ/Unknown%20Album/Agent%20vs%20Agent.mp3"
  191.   //                   },
  192.   //                   {
  193.   //                     name:"Honey ",
  194.   //                     artist: "AnJ",
  195.   //                     price: 500,
  196.   //                     genres: ["Soundtrack"],
  197.   //                     previewURL: "http://media.songlily.com/AnJ/Unknown%20Album/Agent%20vs%20Agent.mp3"
  198.   //                   },
  199.   //                  
  200.   //                   ]
  201.   //     for (var i = 0; i < songs.length; i++)
  202.   //       Songs.insert(songs[i])
  203.   //   }
  204.  
  205.   setupAppInfo()
  206. })
  207.  
  208. function logException(e) {
  209.   console.log("Exception: " + e);
  210. }
  211.  
  212. function setupAppInfo() {
  213.   Application.remove({})
  214.  
  215.   var sys = require('util')
  216.   var exec = require('child_process').exec
  217.   exec("git describe --abbrev=0 --tags", Meteor.bindEnvironment(function(error, stdout, stderr) {
  218.       var version = stdout
  219.       version = version.trim()
  220.       version = version.replace(/^v/, "")
  221.       Application.insert({version: version})
  222.   }, logException))
  223. }
  224.  
  225. String.prototype.trim = function() {
  226.   return this.replace(/^(\s*)((\S+\s*?)*)(\s*)$/,"$2")
  227. }