Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==PREPROCESSOR==
- // @import "%fb2k_profile_path%marc2003\common2.js"
- // @name "Last.fm Charts"
- // @author "marc2003"
- // ==/PREPROCESSOR==
- var script_name = "Last.fm Charts";
- var username_file = settings_path + "username";
- var username = read(username_file);
- var api_key_file = settings_path + "api_key";
- var api_key = read(api_key_file);
- var lfm_img = gdi.Image(images_path + "lastfm_red_small.gif");
- var up_img = gdi.Image(images_path + "up.png");
- var down_img = gdi.Image(images_path + "down.png");
- var g_tooltip = window.CreateTooltip();
- var types = ["artist", "album", "track"];
- var type = window.GetProperty("type", 0);
- var methods = ["getTopArtists", "getTopAlbums", "getTopTracks"];
- var nice_periods = ["Overall", "Last 7 days", "3 month", "6 month", "12 month"];
- var periods = ["overall", "7day", "3month", "6month", "12month"];
- var period = window.GetProperty("period", 0);
- var ranks = [];
- var playcounts = [];
- var names = [];
- var urls = [];
- var name_widths = [];
- var show_artist = window.GetProperty("show_artist", true);
- var but_x = 0;
- var offset = 0;
- var rows = 0;
- var img = null;
- var charts_folder = data_folder + "\\charts";
- if (!fso.FolderExists(charts_folder)) fso.CreateFolder(charts_folder);
- load();
- function on_notify_data(name, data) {
- if (name == "lastfm_update" && data == 1) {
- username = read(username_file);
- api_key = read(api_key_file);
- load();
- }
- }
- function on_size() {
- ww = window.Width;
- wh = window.Height;
- but_x = Math.round((ww - 15) / 2);
- offset = 0;
- rows = Math.floor((wh - 56) / 20);
- up_btn = new sb(but_x, 32, 15, 15, up_img, "offset > 0", function () { on_mouse_wheel(1); });
- down_btn = new sb(but_x, wh - 15, 15, 15, down_img, "offset < ranks.length - rows", function () { on_mouse_wheel(-1); });
- }
- function on_paint(gr) {
- gr.FillSolidRect(0, 0, ww, wh, g_backcolor);
- if (username.length == 0 || api_key.length != 32) {
- gr.GdiDrawText("Please use the context menu to set your Last.fm username and API KEY.", normal_font, g_textcolor, 10, 0, ww - 20, wh, DT_VCENTER | DT_CENTER | DT_WORDBREAK | DT_CALCRECT | DT_NOPREFIX);
- } else {
- gr.GdiDrawText(nice_periods[period] + " " + types[type] + " charts for " + username, title_font, g_textcolor_hl, 6, 6, ww - 77, 24, DT_VCENTER | DT_END_ELLIPSIS | DT_CALCRECT | DT_NOPREFIX);
- gr.DrawImage(lfm_img, ww - 60, 8, 50, 18, 0, 0, 50, 18);
- gr.DrawLine(5, 29, ww - 10, 29, 1, g_textcolor_hl);
- if (ranks.length > 0) {
- for (q = 0; q < Math.min(rows, ranks.length); q++) {
- gr.GdiDrawText(ranks[q + offset] + ".", list_font, g_textcolor, 5, 46 + (q * 20), 20, 18, DT_VCENTER | DT_RIGHT | DT_END_ELLIPSIS | DT_CALCRECT | DT_NOPREFIX);
- gr.GdiDrawText(names[q + offset], list_font, g_textcolor, 30, 46 + (q * 20), ww - 100, 18, DT_VCENTER | DT_END_ELLIPSIS | DT_CALCRECT | DT_NOPREFIX);
- gr.GdiDrawText(addCommas(playcounts[q + offset]), list_font, g_textcolor, ww - 50, 46 + (q * 20), 50, 18, DT_VCENTER | DT_END_ELLIPSIS | DT_CALCRECT | DT_NOPREFIX);
- }
- up_btn.draw(gr);
- down_btn.draw(gr);
- } else {
- gr.GdiDrawText("[no data]", normal_font, g_textcolor, 0, 0, ww, wh, DT_VCENTER | DT_CENTER | DT_WORDBREAK | DT_CALCRECT | DT_NOPREFIX);
- }
- }
- }
- function load() {
- filename = charts_folder + "\\" + username+ "_" + types[type] + "_" + periods[period] + ".json";
- ranks = [];
- names = [];
- playcounts = [];
- urls = [];
- name_widths = [];
- offset = 0;
- if (fso.fileExists(filename)) {
- file = fso.Getfile(filename);
- ts = fso.OpenTextFile(filename, 1, false, -1);
- g_text = ts.ReadAll();
- ts.close();
- parsed_data = JSON.parse(g_text);
- if (parsed_data.error > 0) {
- fb.trace(script_name + ": " + g_text);
- } else {
- arr = parsed_data[methods[type].substring(3).toLowerCase()][types[type]];
- if (typeof arr == "object") {
- var temp_bmp = gdi.CreateImage(1, 1);
- var temp_gr = temp_bmp.GetGraphics();
- for (i = 0; i < arr.length; i++) {
- names[i] = type != 0 && show_artist ? arr[i].artist.name + " - " + arr[i].name : arr[i].name;
- urls[i] = arr[i].url;
- playcounts[i] = arr[i].playcount;
- ranks[i] = (i > 0 && playcounts[i] == playcounts[i - 1]) ? ranks[i - 1] : arr[i]["@attr"].rank;
- name_widths[i] = temp_gr.CalcTextWidth(names[i], normal_font);
- }
- temp_bmp.ReleaseGraphics(temp_gr);
- temp_bmp.Dispose();
- temp_gr = null;
- temp_bmp = null;
- }
- }
- if (Date.parse(Date()) - Date.parse(file.DateLastModified) > 86400000) get();
- } else {
- get();
- }
- window.Repaint();
- }
- function get() {
- var fn = filename;
- lastfm("&user=" + username + "&method=user." + methods[type] + "&period=" + periods[period], "foo_wsh_lastfm_charts", function() { save(xmlhttp.responsetext, fn); load(); });
- }
- function on_mouse_wheel(delta) {
- if (ranks.length < rows) return;
- offset -= delta * 3;
- if (offset < 0) offset = 0;
- if (rows + offset > ranks.length) offset = ranks.length - rows;
- window.Repaint();
- }
- function on_mouse_move(x, y) {
- index = Math.floor((y - 46 ) / 20) + offset;
- in_range = index >= offset && index < Math.min(offset + rows, ranks.length);
- switch(true) {
- case (x > ww - 60 && x < ww - 10 && y > 8 && y < 26):
- window.SetCursor(IDC_HAND);
- tt("Change chart type/period");
- break;
- case (in_range && x > 30 && x < 30 + Math.min(ww - 100, name_widths[index])):
- window.SetCursor(IDC_HAND);
- tt(urls[index]);
- break;
- case (up_btn.trace(x, y)):
- case (down_btn.trace(x, y)):
- window.SetCursor(IDC_HAND);
- break;
- default:
- window.SetCursor(IDC_ARROW);
- g_tooltip.Text = '';
- g_tooltip.Deactivate();
- }
- }
- function on_mouse_lbtn_up(x, y) {
- switch(true) {
- case (x > ww - 60 && x < ww - 10 && y > 8 && y < 26):
- _menu = window.CreatePopupMenu();
- _menu.AppendMenuItem(MF_GRAYED, 1, "Type");
- for (i = 0; i < types.length; i++) {
- _menu.AppendMenuItem(MF_STRING, i + 2, types[i].ucfirst());
- }
- _menu.CheckMenuRadioItem(2, 4, type + 2);
- _menu.AppendMenuItem(MF_SEPARATOR, 0, 0);
- _menu.AppendMenuItem(MF_GRAYED, 5, "Period");
- for (i = 0; i < nice_periods.length; i++) {
- _menu.AppendMenuItem(MF_STRING, i + 6, nice_periods[i]);
- }
- _menu.CheckMenuRadioItem(6, 10, period + 6);
- idx = _menu.TrackPopupMenu(ww - 60, 26);
- switch(idx) {
- case 2:
- case 3:
- case 4:
- type = idx - 2;
- window.SetProperty("type", type);
- load();
- break;
- case 6:
- case 7:
- case 8:
- case 9:
- case 10:
- period = idx - 6;
- window.SetProperty("period", period);
- load();
- break;
- }
- _menu.Dispose();
- break;
- case (in_range && x > 30 && x < 30 + Math.min(ww - 100, name_widths[index])):
- try { WshShell.run(urls[index]); } catch(e) {}
- break;
- case (up_btn.trace(x, y)):
- up_btn.click(x, y);
- break;
- case (down_btn.trace(x, y)):
- down_btn.click(x, y);
- break;
- }
- }
- function on_mouse_rbtn_up(x, y) {
- var _menu = window.CreatePopupMenu();
- var idx;
- _menu.AppendMenuItem(username.length > 0 && api_key.length == 32 ? MF_STRING : MF_GRAYED, 1, "Update");
- _menu.AppendMenuItem(MF_SEPARATOR, 0, 0);
- _menu.AppendMenuItem(MF_STRING, 2, "Set your Last.fm username...");
- _menu.AppendMenuItem(MF_STRING, 3, "Set your API KEY...");
- _menu.AppendMenuItem(MF_SEPARATOR, 0, 0);
- _menu.AppendMenuItem(MF_STRING, 4, "Show artist name on album and track charts");
- _menu.CheckMenuItem(4, show_artist);
- _menu.AppendMenuItem(MF_SEPARATOR, 0, 0);
- if (utils.IsKeyPressed(0x10)) _menu.AppendMenuItem(MF_STRING, 9, "Properties");
- _menu.AppendMenuItem(MF_STRING, 10, "Configure...");
- idx = _menu.TrackPopupMenu(x, y);
- switch(idx) {
- case 1:
- get();
- break;
- case 2:
- set_username();
- load();
- break;
- case 3:
- set_api_key();
- load();
- break;
- case 4:
- show_artist = !show_artist;
- window.SetProperty("show_artist", show_artist);
- load();
- break;
- case 9:
- window.ShowProperties();
- break;
- case 10:
- window.ShowConfigure();
- break;
- }
- _menu.Dispose();
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement