-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPiaproParser.cs
71 lines (59 loc) · 1.54 KB
/
PiaproParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Threading.Tasks;
using MikuBot.Commands;
using MikuBot.Helpers;
using MikuBot.Modules;
using PiaproClient;
namespace MikuBot.ExtraPlugins
{
public class PiaproParser : MsgCommandModuleBase
{
private void GetData(Receiver receiver, string url)
{
PostQueryResult data;
try
{
data = new PiaproClient.PiaproClient().ParseByUrl(url);
}
catch (PiaproException x)
{
receiver.Msg(string.Format("Piapro (error): {0}", x.Message));
return;
}
var lengthStr = string.Empty;
if (data.LengthSeconds != null)
{
var length = TimeSpan.FromSeconds((double)data.LengthSeconds);
lengthStr = string.Format(" ({0}:{1})", length.Minutes, length.Seconds);
}
receiver.Msg(string.Format("Piapro: {0} '{1}' by {2}{3}", data.PostType, data.Title, data.Author, lengthStr));
}
public override string HelpText
{
get { return "Parses Piapro links"; }
}
public override InitialModuleStatus InitialStatus
{
get { return InitialModuleStatus.Enabled; }
}
public override bool IsPassive
{
get { return true; }
}
public override string Name
{
get { return "Piapro"; }
}
public override void HandleCommand(MsgCommand cmd, IBotContext bot)
{
if (cmd.BotCommand.Is("NoLink"))
return;
string url;
var receiver = cmd.Reply(bot.Writer);
if (!PiaproUrlHelper.TryGetUrl(cmd.Text, out url))
return;
Task.Factory.StartNew(() => GetData(receiver, url))
.ContinueWith(TaskHelper.HandleTaskException, TaskContinuationOptions.OnlyOnFaulted);
}
}
}