Advertisement
Guest User

Untitled

a guest
Aug 31st, 2011
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==PREPROCESSOR==
  2. // @import "%fb2k_profile_path%marc2003\common2.js"
  3. // @name "Last.fm Charts"
  4. // @author "marc2003"
  5. // ==/PREPROCESSOR==
  6.  
  7. var script_name = "Last.fm Charts";
  8. var username_file = settings_path + "username";
  9. var username = read(username_file);
  10. var api_key_file = settings_path + "api_key";
  11. var api_key = read(api_key_file);
  12.  
  13. var lfm_img = gdi.Image(images_path + "lastfm_red_small.gif");
  14. var up_img = gdi.Image(images_path + "up.png");
  15. var down_img = gdi.Image(images_path + "down.png");
  16. var g_tooltip = window.CreateTooltip();
  17.  
  18. var types = ["artist", "album", "track"];
  19. var type = window.GetProperty("type", 0);
  20. var methods = ["getTopArtists", "getTopAlbums", "getTopTracks"];
  21. var nice_periods = ["Overall", "Last 7 days", "3 month", "6 month", "12 month"];
  22. var periods = ["overall", "7day", "3month", "6month", "12month"];
  23. var period = window.GetProperty("period", 0);
  24. var ranks = [];
  25. var playcounts = [];
  26. var names = [];
  27. var urls = [];
  28. var name_widths = [];
  29. var show_artist = window.GetProperty("show_artist", true);
  30. var but_x = 0;
  31. var offset = 0;
  32. var rows = 0;
  33.  
  34. var img = null;
  35. var charts_folder = data_folder + "\\charts";
  36. if (!fso.FolderExists(charts_folder)) fso.CreateFolder(charts_folder);
  37.  
  38. load();
  39.  
  40. function on_notify_data(name, data) {
  41.     if (name == "lastfm_update" && data == 1) {
  42.         username = read(username_file);
  43.         api_key = read(api_key_file);
  44.         load();
  45.     }
  46. }
  47.  
  48. function on_size() {
  49.     ww = window.Width;
  50.     wh = window.Height;
  51.     but_x = Math.round((ww - 15) / 2);
  52.     offset = 0;
  53.     rows = Math.floor((wh - 56) / 20);
  54.     up_btn = new sb(but_x, 32, 15, 15, up_img, "offset > 0", function () { on_mouse_wheel(1); });
  55.     down_btn = new sb(but_x, wh - 15, 15, 15, down_img, "offset < ranks.length - rows", function () { on_mouse_wheel(-1); });
  56. }
  57.  
  58. function on_paint(gr) {
  59.     gr.FillSolidRect(0, 0, ww, wh, g_backcolor);
  60.     if (username.length == 0 || api_key.length != 32) {
  61.         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);
  62.     } else {
  63.         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);
  64.         gr.DrawImage(lfm_img, ww - 60, 8, 50, 18, 0, 0, 50, 18);
  65.         gr.DrawLine(5, 29, ww - 10, 29, 1, g_textcolor_hl);
  66.         if (ranks.length > 0) {
  67.             for (q = 0; q < Math.min(rows, ranks.length); q++) {
  68.                 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);
  69.                 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);
  70.                 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);
  71.             }
  72.             up_btn.draw(gr);
  73.             down_btn.draw(gr);
  74.         } else {
  75.             gr.GdiDrawText("[no data]", normal_font, g_textcolor, 0, 0, ww, wh, DT_VCENTER | DT_CENTER | DT_WORDBREAK | DT_CALCRECT | DT_NOPREFIX);
  76.         }
  77.     }
  78. }
  79.  
  80. function load() {
  81.     filename = charts_folder + "\\" + username+ "_" + types[type] + "_" + periods[period] + ".json";
  82.     ranks = [];
  83.     names = [];
  84.     playcounts = [];
  85.     urls = [];
  86.     name_widths = [];
  87.     offset = 0;
  88.     if (fso.fileExists(filename)) {
  89.         file = fso.Getfile(filename);
  90.         ts = fso.OpenTextFile(filename, 1, false, -1);
  91.         g_text = ts.ReadAll();
  92.         ts.close();
  93.         parsed_data = JSON.parse(g_text);
  94.         if (parsed_data.error > 0) {
  95.             fb.trace(script_name + ": " + g_text);
  96.         } else {
  97.             arr = parsed_data[methods[type].substring(3).toLowerCase()][types[type]];
  98.             if (typeof arr == "object") {
  99.                 var temp_bmp = gdi.CreateImage(1, 1);
  100.                 var temp_gr = temp_bmp.GetGraphics();
  101.                 for (i = 0; i < arr.length; i++) {
  102.                     names[i] = type != 0 && show_artist ? arr[i].artist.name + " - " + arr[i].name : arr[i].name;
  103.                     urls[i] = arr[i].url;
  104.                     playcounts[i] = arr[i].playcount;
  105.                     ranks[i] = (i > 0 && playcounts[i] == playcounts[i - 1]) ? ranks[i - 1] : arr[i]["@attr"].rank;
  106.                     name_widths[i] = temp_gr.CalcTextWidth(names[i], normal_font);
  107.                 }
  108.                 temp_bmp.ReleaseGraphics(temp_gr);
  109.                 temp_bmp.Dispose();
  110.                 temp_gr = null;
  111.                 temp_bmp = null;
  112.             }
  113.         }
  114.         if (Date.parse(Date()) - Date.parse(file.DateLastModified) > 86400000) get();
  115.     } else {
  116.         get();
  117.     }
  118.     window.Repaint();
  119. }
  120.  
  121. function get() {
  122.     var fn = filename;
  123.     lastfm("&user=" + username + "&method=user." + methods[type] + "&period=" + periods[period], "foo_wsh_lastfm_charts", function() { save(xmlhttp.responsetext, fn); load(); });
  124. }
  125.  
  126. function on_mouse_wheel(delta) {
  127.     if (ranks.length < rows) return;
  128.     offset -= delta * 3;
  129.     if (offset < 0) offset = 0;
  130.     if (rows + offset > ranks.length) offset = ranks.length - rows;
  131.     window.Repaint();
  132. }
  133.  
  134. function on_mouse_move(x, y) {
  135.     index = Math.floor((y - 46 ) / 20) + offset;
  136.     in_range = index >= offset && index < Math.min(offset + rows, ranks.length);
  137.     switch(true) {
  138.         case (x > ww - 60 && x < ww - 10 && y > 8 && y < 26):
  139.             window.SetCursor(IDC_HAND);
  140.             tt("Change chart type/period");
  141.             break;
  142.         case (in_range && x > 30 && x < 30 + Math.min(ww - 100, name_widths[index])):
  143.             window.SetCursor(IDC_HAND);
  144.             tt(urls[index]);
  145.             break;
  146.         case (up_btn.trace(x, y)):
  147.         case (down_btn.trace(x, y)):
  148.             window.SetCursor(IDC_HAND);
  149.             break;
  150.         default:
  151.             window.SetCursor(IDC_ARROW);
  152.             g_tooltip.Text = '';
  153.             g_tooltip.Deactivate();
  154.     }
  155. }
  156.  
  157. function on_mouse_lbtn_up(x, y) {
  158.     switch(true) {
  159.         case (x > ww - 60 && x < ww - 10 && y > 8 && y < 26):
  160.             _menu = window.CreatePopupMenu();
  161.             _menu.AppendMenuItem(MF_GRAYED, 1, "Type");
  162.             for (i = 0; i < types.length; i++) {
  163.                 _menu.AppendMenuItem(MF_STRING, i + 2, types[i].ucfirst());
  164.             }
  165.             _menu.CheckMenuRadioItem(2, 4, type + 2);
  166.             _menu.AppendMenuItem(MF_SEPARATOR, 0, 0);
  167.             _menu.AppendMenuItem(MF_GRAYED, 5, "Period");
  168.             for (i = 0; i < nice_periods.length; i++) {
  169.                 _menu.AppendMenuItem(MF_STRING, i + 6, nice_periods[i]);
  170.             }
  171.             _menu.CheckMenuRadioItem(6, 10, period + 6);
  172.             idx = _menu.TrackPopupMenu(ww - 60, 26);
  173.             switch(idx) {
  174.                 case 2:
  175.                 case 3:
  176.                 case 4:
  177.                     type = idx - 2;
  178.                     window.SetProperty("type", type);
  179.                     load();
  180.                     break;
  181.                 case 6:
  182.                 case 7:
  183.                 case 8:
  184.                 case 9:
  185.                 case 10:
  186.                     period = idx - 6;
  187.                     window.SetProperty("period", period);
  188.                     load();
  189.                     break;
  190.             }
  191.             _menu.Dispose();
  192.             break;
  193.         case (in_range && x > 30 && x < 30 + Math.min(ww - 100, name_widths[index])):
  194.             try { WshShell.run(urls[index]); } catch(e) {}
  195.             break;
  196.         case (up_btn.trace(x, y)):
  197.             up_btn.click(x, y);
  198.             break;
  199.         case (down_btn.trace(x, y)):
  200.             down_btn.click(x, y);
  201.             break;
  202.     }
  203. }
  204.  
  205. function on_mouse_rbtn_up(x, y) {
  206.     var _menu = window.CreatePopupMenu();
  207.     var idx;
  208.     _menu.AppendMenuItem(username.length > 0 && api_key.length == 32 ? MF_STRING : MF_GRAYED, 1, "Update");
  209.     _menu.AppendMenuItem(MF_SEPARATOR, 0, 0);
  210.     _menu.AppendMenuItem(MF_STRING, 2, "Set your Last.fm username...");
  211.     _menu.AppendMenuItem(MF_STRING, 3, "Set your API KEY...");
  212.     _menu.AppendMenuItem(MF_SEPARATOR, 0, 0);
  213.     _menu.AppendMenuItem(MF_STRING, 4, "Show artist name on album and track charts");
  214.     _menu.CheckMenuItem(4, show_artist);
  215.     _menu.AppendMenuItem(MF_SEPARATOR, 0, 0);
  216.     if (utils.IsKeyPressed(0x10)) _menu.AppendMenuItem(MF_STRING, 9, "Properties");
  217.     _menu.AppendMenuItem(MF_STRING, 10, "Configure...");
  218.     idx = _menu.TrackPopupMenu(x, y);
  219.     switch(idx) {
  220.         case 1:
  221.             get();
  222.             break;
  223.         case 2:
  224.             set_username();
  225.             load();
  226.             break;
  227.         case 3:
  228.             set_api_key();
  229.             load();
  230.             break;
  231.         case 4:
  232.             show_artist = !show_artist;
  233.             window.SetProperty("show_artist", show_artist);
  234.             load();
  235.             break;
  236.         case 9:
  237.             window.ShowProperties();
  238.             break;
  239.         case 10:
  240.             window.ShowConfigure();
  241.             break;
  242.     }
  243.     _menu.Dispose();
  244.     return true;
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement