Permalink
Browse files

テスト段階へ

  • Loading branch information...
Nanariya
Nanariya committed Aug 17, 2012
1 parent 9afc132 commit fde8be767c636fc2926a22599db39d05005734b4
@@ -0,0 +1,19 @@
+using System;
+
+namespace DoppelNanariya
+{
+ public class Setting
+ {
+ public Int32 StatusRefreshInterval { get; set; }
+
+ public Setting ()
+ {
+ }
+
+ public void LoadDefault()
+ {
+ this.StatusRefreshInterval = 5 * 60; // 5 minutes
+ }
+ }
+}
+
@@ -0,0 +1,16 @@
+using System;
+
+namespace DoppelNanariya
+{
+ public class UserConfig
+ {
+ public string RequestToken { get; set; }
+ public string RequestTokenSecret { get; set; }
+ public string AccessToken { get; set; }
+ public string AccessTokenSecret { get; set; }
+
+ public UserConfig ()
+ {
+ }
+ }
+}
@@ -0,0 +1,13 @@
+using System;
+
+namespace DoppelNanariya
+{
+ public class UserConfig2
+ {
+ public Int64 LastId { get; set; }
+ public UserConfig2 ()
+ {
+ }
+ }
+}
+
@@ -33,10 +33,31 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
+ <Reference Include="Newtonsoft.Json">
+ <HintPath>Lib\Newtonsoft.Json.dll</HintPath>
+ </Reference>
+ <Reference Include="Twitterizer2">
+ <HintPath>Lib\Twitterizer2.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Main.cs" />
<Compile Include="AssemblyInfo.cs" />
+ <Compile Include="NanaTwitter.cs" />
+ <Compile Include="Config\Setting.cs" />
+ <Compile Include="Config\UserConfig.cs" />
+ <Compile Include="Config\UserConfig2.cs" />
+ <Compile Include="Lib\Nana\NanaTool.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <ItemGroup>
+ <Folder Include="Lib\" />
+ <Folder Include="Config\" />
+ <Folder Include="Lib\Nana\" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="Lib\Newtonsoft.Json.dll" />
+ <None Include="Lib\Twitterizer2.dll" />
+ </ItemGroup>
</Project>
@@ -0,0 +1,76 @@
+using System;
+using System.IO;
+using System.Xml;
+
+namespace Nana
+{
+ public class NanaTool
+ {
+ /// <summary>
+ /// Saves the config.
+ /// </summary>
+ /// <param name='obj'>
+ /// Object.
+ /// </param>
+ /// <param name='objType'>
+ /// Object type.
+ /// </param>
+ /// <param name='fileName'>
+ /// File name.
+ /// </param>
+ public void SaveConfig(Object obj, Type objType, String fileName)
+ {
+ try
+ {
+ System.Xml.Serialization.XmlSerializer serializer =
+ new System.Xml.Serialization.XmlSerializer(objType);
+
+ System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Create);
+
+ serializer.Serialize(fs, obj);
+
+ fs.Close();
+ }
+ catch(Exception e)
+ {
+ throw new Exception("(´A')コンフィグ[" + fileName + "]に書けね", e);
+ }
+ }
+ /// <summary>
+ /// Loads the config.
+ /// </summary>
+ /// <returns>
+ /// The config.
+ /// </returns>
+ /// <param name='objType'>
+ /// Object type.
+ /// </param>
+ /// <param name='fileName'>
+ /// File name.
+ /// </param>
+ public Object LoadConfig(Type objType, String fileName)
+ {
+ Object obj = null;
+ try
+ {
+ System.Xml.Serialization.XmlSerializer serializer =
+ new System.Xml.Serialization.XmlSerializer(objType);
+
+ FileStream fs = new FileStream(fileName, FileMode.Open);
+
+ obj = serializer.Deserialize(fs);
+
+ fs.Close();
+
+
+ }
+ catch(Exception e)
+ {
+ throw new Exception("(´A')コンフィグ[" + fileName + "]が読めねぇ・・・", e);
+ }
+
+ return obj;
+ }
+ }
+}
+
View
@@ -1,12 +1,162 @@
using System;
+using System.Net;
+using System.Timers;
+using System.Collections.Generic;
+using Twitterizer;
namespace DoppelNanariya
{
class MainClass
{
+ private static string SettingFileName { get {return "Setting.xml";}}
+
public static void Main (string[] args)
{
- Console.WriteLine ("Hello World!");
+ Array.Reverse(args);
+
+ String parameter = null;
+
+ UserConfig userConfig = NanaTwitter.LoadConfig();
+ NanaTwitter.Config = NanaTwitter.LoadConfig();
+
+ foreach (String arg in args)
+ {
+ if(arg.Length < 2)
+ {
+ parameter = arg;
+ break;
+ }
+
+ if(arg[0] == '-')
+ {
+ switch(arg[1])
+ {
+ case 'n':
+ Console.WriteLine("新規登録します");
+ //RegisterApplication();
+ return;
+ case 'h':
+ Console.WriteLine("ヘルプ");
+ return;
+
+ case 't':
+ NanaTwitter.SendTweet(parameter);
+ break;
+ case 'd':
+ DaemonProc(userConfig);
+ break;
+ default:
+ break;
+ }
+ }else{
+ parameter = arg;
+ }
+ }
+
+ //通常処理
+ Proc(userConfig);
+
+ }
+
+ private static void Proc(UserConfig userConfig)
+ {
+ while(true)
+ {
+ Console.WriteLine("Waiting for request...");
+ String input = Console.ReadLine();
+
+ switch(input)
+ {
+ case "help":
+ Console.WriteLine("\r\n------------------------\r\n" +
+ "help\tこのヘルプ\r\n" +
+ "show\tホームタイムラインの表示\r\n" +
+ "exit\tアプリの終了\r\n");
+ break;
+ case "show":
+ TwitterResponse<TwitterStatusCollection> homeTimeLine = NanaTwitter.ShowHomeTimeline();
+ List<TwitterStatus> statuses = new List<TwitterStatus>();
+
+ foreach(TwitterStatus status in homeTimeLine.ResponseObject)
+ {
+ statuses.Add(status);
+ //Console.WriteLine(status.Text);
+ }
+ statuses.Reverse();
+ statuses.ForEach(delegate(TwitterStatus obj){Console.WriteLine(obj.Text);});
+
+ break;
+ case "send":
+ Console.WriteLine("ツイートする文字打ってね\r\n");
+ NanaTwitter.SendTweet(Console.ReadLine());
+ break;
+ case "test":
+ Nana.NanaTool nana = new Nana.NanaTool();
+ UserConfig2 conf = new UserConfig2();
+ conf.LastId = 10;
+ nana.SaveConfig(conf, typeof(UserConfig2), "test.txt");
+
+ UserConfig2 conf2 = (UserConfig2)nana.LoadConfig(typeof(UserConfig2), "test.txt");
+ Console.WriteLine(conf2.LastId.ToString());
+
+ break;
+ case "q":
+ case "exit":
+ Environment.Exit(0);
+ break;
+ default:
+ break;
+ }
+
+ }
+ }
+
+ private static void DaemonProc(UserConfig userConfig)
+ {
+ //擬似デーモンモード Ctrl+C で強制終了できるっぽいので特に何もしない
+
+
+ Setting setting = null;
+
+ try
+ {
+ Nana.NanaTool nana = new Nana.NanaTool();
+ setting = (Setting)nana.LoadConfig(typeof(Setting), SettingFileName);
+ }
+ catch(Exception)
+ {
+ setting = new Setting();
+ setting.LoadDefault();
+ }
+
+ Timer timer = new Timer();
+ timer.Elapsed += new ElapsedEventHandler(EveryMinutes);
+ timer.Interval = setting.StatusRefreshInterval;
+ timer.AutoReset = true;
+ timer.Enabled = true;
+
+ Console.WriteLine("バックグラウンドでのTweet機能が有効です");
+
+ try
+ {
+ Nana.NanaTool nana = new Nana.NanaTool();
+ nana.SaveConfig(setting, typeof(Setting), SettingFileName);
+ }
+ catch(Exception)
+ {/* あきらめろん */}
+
}
+
+ public static void EverySeconds(object sender, ElapsedEventArgs e)
+ {
+ //毎秒の処理
+ }
+
+ public static void EveryMinutes(object sender, ElapsedEventArgs e)
+ {
+ //毎分の処理
+
+ }
+
}
}
Oops, something went wrong.

0 comments on commit fde8be7

Please sign in to comment.