Guest User

Untitled

a guest
Dec 19th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace PandoraBox
  8. {
  9.     public class APVIDEOPlugin : IPluginMacro
  10.     {
  11.         //пример
  12.         //[APVIDEO]    
  13.         Regex _block = new Regex(@"{APVIDEO}(.*?){/APVIDEO}", RegexOptions.Compiled);
  14.         //кодировка
  15.         Encoding _enc = Encoding.GetEncoding("windows-1251");
  16.         //путь к данным
  17.         String _root = @"c:\ap-data\video";
  18.         //шаблон видео
  19.         String _template = "<iframe width='560' height='315' src='https://www.youtube.com/embed/[CODE]' frameborder='0' allowfullscreen></iframe>";
  20.  
  21.         public string Execute(string template, PluginMacroArgs args)
  22.         {
  23.             return _block.Replace(template, delegate(Match match)
  24.             {
  25.                 String key = CleanKey(args.Key);
  26.                 String inner = match.Groups[1].Value;
  27.                 String file = Path.Combine(_root, key);
  28.                 if (!File.Exists(file)) return "";
  29.                 String[] lines = File.ReadAllLines(file, _enc);
  30.                 String line = lines[Rnd.Next(0, lines.Length - 1)];
  31.                 var parts = line.Split('|');
  32.                 String code = parts[0];
  33.                 if (!code.Contains("=")) return "";
  34.                 code = code.Split('=', '&')[1];
  35.                 String title = parts[2];
  36.                 inner = inner.Replace("[APVIDEO]", _template.Replace("[CODE]", code));              
  37.                 inner = inner.Replace("[APVIDEOTITLE]", title);        
  38.                 return inner;
  39.             });
  40.         }
  41.  
  42.         private string CleanKey(String key)
  43.         {
  44.             String key1 = key;
  45.             key1 = String.Join("", key1.Split(Path.GetInvalidPathChars()));
  46.             key1 = String.Join("", key1.Split(Path.GetInvalidFileNameChars()));
  47.             return key1;
  48.         }
  49.  
  50.         public ushort Level { get { return 5; } }
  51.         public String Name { get { return "APVIDEOPlugin"; } }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment