OSDN Git Service

C# 8.0 のnull許容参照型を有効化
[opentween/open-tween.git] / OpenTween / nicoms.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2011      Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8 // All rights reserved.
9 //
10 // This file is part of OpenTween.
11 //
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
15 // any later version.
16 //
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
26
27 #nullable enable
28
29 using System;
30
31 namespace OpenTween
32 {
33     public static class nicoms
34     {
35         private static readonly string[] _nicovideo =
36         {
37             "www.nicovideo.jp/watch/",
38             "live.nicovideo.jp/watch/",
39             "live.nicovideo.jp/gate/",
40             "live.nicolive.jp/gate/",
41             "co.nicovideo.jp/community/",
42             "com.nicovideo.jp/community/",
43             "ch.nicovideo.jp/channel/",
44             "nicovideo.jp/watch/",
45             "seiga.nicovideo.jp/bbs/",
46             "www.niconicommons.jp/material/",
47             "niconicommons.jp/material/",
48             "news.nicovideo.jp/watch/",
49         };
50
51         public static string Shorten(string url)
52         {
53             //整形(http(s)://を削除)
54             if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
55             {
56                 url = url.Substring(7);
57             }
58             else if (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
59             {
60                 url = url.Substring(8);
61             }
62             else
63             {
64                 return url;
65             }
66
67             foreach (var nv in _nicovideo)
68             {
69                 if (url.StartsWith(nv, StringComparison.Ordinal))
70                     return string.Format("{0}{1}", "https://nico.ms/", url.Substring(nv.Length));
71             }
72
73             var i = url.IndexOf("nicovideo.jp/user/", StringComparison.OrdinalIgnoreCase);
74             if (i == 0 || i == 4) return string.Format("{0}{1}", "https://nico.ms/", url.Substring(13 + i));
75
76             i = url.IndexOf("nicovideo.jp/mylist/", StringComparison.OrdinalIgnoreCase);
77             if (i == 0 || i == 4) return string.Format("{0}{1}", "https://nico.ms/", url.Substring(13 + i));
78
79             i = url.IndexOf("seiga.nicovideo.jp/watch/", StringComparison.OrdinalIgnoreCase);
80             if (i == 0) return string.Format("{0}{1}", "https://nico.ms/", url.Substring(25));
81
82             return "https://" + url;
83         }
84     }
85 }