Skip to content

Instantly share code, notes, and snippets.

@kazuo
Last active January 7, 2016 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazuo/77dea0a2c7404625d817 to your computer and use it in GitHub Desktop.
Save kazuo/77dea0a2c7404625d817 to your computer and use it in GitHub Desktop.
// useful to get names from http://www.imdb.com/search/name?refine=birth_monthday&birth_monthday=1-22&ref_=hm_brn_hd
(function () {
var $table = $('#main table.results')
, csv = "data:text/csv;charset=utf-8,"
, lines = [['First', 'Last', 'Email', 'Position', 'Company', 'Other']]
, link = document.createElement('a')
, prepareEmailName
, statusesLength = 0
, ticketTypesLength = 0
, urlParameters = {}
;
var options = {
emailTemplate: "{$name}@test.eventfarm.com"/*,
ticketTypes: ['Copper', 'Iron', 'Tin'],
statuses: ['CONFIRMED BY RSVP', 'DECLINED BY RSVP', 'UNCONFIRMED']*/
};
if (options.ticketTypes && options.ticketTypes.length) {
lines[0].push('Ticket Type');
ticketTypesLength = options.ticketTypes.length;
}
if (options.statuses && options.statuses.length) {
lines[0].push('Status');
statusesLength = options.statuses.length;
}
// force lower case and get rid of any dots at the start or end of the string
prepareEmailName = function (name) {
name = name.toLowerCase();
if (name.substr(0, 1) === '.') {
name = name.substr(1);
}
if (name.substr(-1) === '.') {
name = name.substr(0, name.length - 1);
}
return name;
};
$table.find('td.name').each(function (i, element) {
var name = $(element).find('> a').text().trim()
, parts = name.split(' ')
, description = $(element).find('.description').text().split(',')
, position = description[0] ? String(description[0]).trim() : ''
, company = description[1] ? String(description[1]).trim(): ''
, email = options.emailTemplate.replace('{$name}', parts.map(prepareEmailName).join('.'))
, last = parts.pop()
, first = parts.join(' ')
, nameHref = $(element).find('> a').attr('href').split('/')
, other = nameHref[2] ? String(nameHref[2]).trim() : ''
, line = [first, last, email, position, company, other]
;
if (ticketTypesLength) {
line.push(options.ticketTypes[Math.floor(Math.random() * (ticketTypesLength))]);
}
if (statusesLength) {
line.push(options.statuses[Math.floor(Math.random() * (statusesLength))]);
}
lines.push(line.join(','));
});
window.location.href.split('?').pop().split('&').forEach(function (element) {
var pieces = element.split('=');
urlParameters[pieces[0]] = pieces[1];
});
csv += lines.join("\n");
link.setAttribute('href', encodeURI(csv));
link.setAttribute(
'download',
'IMdb-' + (urlParameters['birth_monthday'] ? urlParameters['birth_monthday'] : urlParameters['birth_month'] + '-' + urlParameters['birth_day'])
+ ((ticketTypesLength || statusesLength) ? '-guest-list' : '-group') + '-'
+ parseInt($table.find('tr.detailed:first .number').text()) + '_' + parseInt($table.find('tr.detailed:last .number').text())
+ '.csv'
);
link.style = "visibility:hidden";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment