Advertisement
Guest User

Untitled

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