Skip to content
Browse files

テスト段階へ

  • Loading branch information...
1 parent 9afc132 commit fde8be767c636fc2926a22599db39d05005734b4 Nanariya committed
View
19 DoppelNanariya/Config/Setting.cs
@@ -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
+ }
+ }
+}
+
View
16 DoppelNanariya/Config/UserConfig.cs
@@ -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 ()
+ {
+ }
+ }
+}
View
13 DoppelNanariya/Config/UserConfig2.cs
@@ -0,0 +1,13 @@
+using System;
+
+namespace DoppelNanariya
+{
+ public class UserConfig2
+ {
+ public Int64 LastId { get; set; }
+ public UserConfig2 ()
+ {
+ }
+ }
+}
+
View
21 DoppelNanariya/DoppelNanariya.csproj
@@ -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>
View
76 DoppelNanariya/Lib/Nana/NanaTool.cs
@@ -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
152 DoppelNanariya/Main.cs
@@ -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)
+ {
+ //毎分の処理
+
+ }
+
}
}
View
133 DoppelNanariya/NanaTwitter.cs
@@ -0,0 +1,133 @@
+using System;
+using System.IO;
+using System.Xml;
+using System.Net;
+using Twitterizer;
+
+namespace DoppelNanariya
+{
+ public static class NanaTwitter
+ {
+ private static string ConsumerKey { get {return "";}}
+ private static string ConsumerSecret { get {return "";}}
+ private static string FileName { get {return "DoppelNanariya.conf";}}
+ public static UserConfig Config { get; set; }
+
+ private static OAuthTokenResponse RequestToken { get; set; }
+
+ public static bool IsExistUserConfig()
+ {
+ UserConfig userConfig = LoadConfig();
+ if(userConfig == null)
+ {
+ return false;
+ }else{
+ Config = userConfig;
+ return true;
+ }
+ }
+
+ public static String CreateRequestTokenUrl()
+ {
+ OAuthTokenResponse requestToken = OAuthUtility.GetRequestToken(ConsumerKey, ConsumerSecret, "oob");
+ Uri uri = OAuthUtility.BuildAuthorizationUri(requestToken.Token);
+ return uri.ToString();
+ }
+
+ public static void SaveConfig(UserConfig conf)
+ {
+ try
+ {
+ System.Xml.Serialization.XmlSerializer serializer =
+ new System.Xml.Serialization.XmlSerializer(typeof(UserConfig));
+
+ System.IO.FileStream fs = new System.IO.FileStream(FileName, FileMode.Create);
+
+ serializer.Serialize(fs, conf);
+
+ fs.Close();
+ }
+ catch(Exception)
+ {
+ }
+ }
+ public static UserConfig LoadConfig()
+ {
+ UserConfig userConfig = null;
+
+ try
+ {
+ System.Xml.Serialization.XmlSerializer serializer =
+ new System.Xml.Serialization.XmlSerializer(typeof(UserConfig));
+
+ FileStream fs = new FileStream(FileName, FileMode.Open);
+
+ userConfig = (UserConfig) serializer.Deserialize(fs);
+
+ fs.Close();
+ }
+ catch(Exception)
+ {}
+
+ return userConfig;
+ }
+
+
+ public static Uri RegisterApplicationRequestUrl()
+ {
+ NanaTwitter.RequestToken = OAuthUtility.GetRequestToken(NanaTwitter.ConsumerKey,NanaTwitter.ConsumerSecret,"oob");
+ Console.WriteLine(NanaTwitter.RequestToken.Token.ToString());
+ Uri uri = OAuthUtility.BuildAuthorizationUri(NanaTwitter.RequestToken.Token);
+
+ return uri;
+ }
+
+ public static void RegisterApplicationWithPinCode(String pinCode)
+ {
+ OAuthTokenResponse response
+ = OAuthUtility.GetAccessToken(NanaTwitter.ConsumerKey, NanaTwitter.ConsumerSecret, NanaTwitter.RequestToken.Token, pinCode);
+ NanaTwitter.Config.AccessToken = response.Token;
+ NanaTwitter.Config.AccessTokenSecret = response.TokenSecret;
+ }
+
+ public static TwitterResponse<TwitterStatusCollection> ShowHomeTimeline()
+ {
+ OAuthTokens token = new OAuthTokens();
+ token.ConsumerKey = NanaTwitter.ConsumerKey;
+ token.ConsumerSecret = NanaTwitter.ConsumerSecret;
+ token.AccessToken = Config.AccessToken;
+ token.AccessTokenSecret = Config.AccessTokenSecret;
+
+ TwitterResponse<TwitterStatusCollection> status = TwitterTimeline.HomeTimeline(token);
+
+ return status;
+ /*
+ foreach( TwitterStatus tweet in status.ResponseObject)
+ {
+ Console.WriteLine(tweet.Text);
+ }
+ */
+ }
+ public static TwitterResponse<TwitterStatus> SendTweet(String tweet)
+ {
+ if(Config == null)
+ {
+ Console.WriteLine("('A`)");
+ return null;
+ }
+
+ OAuthTokens token = new OAuthTokens();
+ token.ConsumerKey = NanaTwitter.ConsumerKey;
+ token.ConsumerSecret = NanaTwitter.ConsumerSecret;
+ token.AccessToken = Config.AccessToken;
+ token.AccessTokenSecret = Config.AccessTokenSecret;
+
+ TwitterResponse<TwitterStatus> status = TwitterStatus.Update(token, tweet);
+
+ return status;
+ /*
+ Console.WriteLine(status.Result.ToString());
+ */
+ }
+ }
+}

0 comments on commit fde8be7

Please sign in to comment.
Something went wrong with that request. Please try again.