ãã®åããã°ããã以ä¸ã®ããã°ãæ¸ãã¦ãã®ãè¦ã¦ãããããèªåããã£ã¦ã¿ãããã¨æã£ã¦ããã¯ã4æ¥ãä¸éãå½¢ã«ãªã£ãï¼æ°ãããã ãã ã£ãâ¦ï¼ã®ã§ããã°ã«æ¸ãæ®ãã¦ããã¾ãã
æºå
ã¾ãã¯Visual Studio 2013 Update3ãé©ç¨ãã¾ãããã
ãã®ä»ã«Google Analytics APIãTwitter APIã®è¨å®ãå¿ è¦ã§ããããã®è¾ºã®ãã¨ã¯ãã°ããã®ããã°ãè¦ã¦ããã£ããããã¨æãã¾ãã
Microsoft Azure WebJobããã¸ã§ã¯ãã®ä½æ
Visual Studio 2013 Update3ããä½æã§ããããã«ãªã£ãMicrosoft Azure WebJobãã³ãã¬ã¼ãã使ã£ã¦ããã¸ã§ã¯ããä½æãã¾ãã
å¿ è¦ãªããã±ã¼ã¸ã®ã¤ã³ã¹ãã¼ã«
NuGetã§ä»¥ä¸ã®ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãã¦ããã¾ãã
ä¸è¨ãªã³ã¯ãè¦ãã®ãé¢åãªäººã¯ããã±ã¼ã¸ããã¼ã¸ã£ã³ã³ã½ã¼ã«ã§ä»¥ä¸ã®ã³ãã³ããå®è¡ãã¦ä¸ããã
PM> Install-Package Google.Apis.Analytics.v3 PM> Install-Package CoreTweet
ã³ã¼ã
é¢åãªã®ã§staticãããããªæãã§ã¹ãã£ã¨æ¸ãã¾ããGoogle Analytics APIå¨ãã¯ã¯ã£ããè¨ã£ã¦ãã°ããã®ä¸¸ãã¯ãªã§ãã
using System; using System.Configuration; using System.Security.Cryptography.X509Certificates; using CoreTweet; using Google.Apis.Analytics.v3; using Google.Apis.Auth.OAuth2; using Google.Apis.Services; namespace TweetPV_WebJob { // To learn more about Microsoft Azure WebJobs, please see http://go.microsoft.com/fwlink/?LinkID=401557 class Program { static void Main() { try { var analyticsKeyFile = GetSettings("analyticsKeyFile"); var certificate = new X509Certificate2(analyticsKeyFile, "notasecret", X509KeyStorageFlags.Exportable); // Scopes ã¯æå®ããªãã¨ã¨ã©ã¼ã«ãªã var analyticsCredentialId = GetSettings("analyticsCredentialId"); var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(analyticsCredentialId) { Scopes = new[] { AnalyticsService.Scope.Analytics, AnalyticsService.Scope.AnalyticsReadonly } }.FromCertificate(certificate)); // HttpClientInitializer ã« credential å ¥ããã®ã¯éåæãããã©æ£ããããã var service = new AnalyticsService(new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = "TweetPV", }); // Azure 㯠UTC ãªã®ã§ +9 æéã㦠-1 æ¥ var date = DateTime.UtcNow.AddHours(9).AddDays(-1).ToString("yyyy-MM-dd"); // ****** ã¯ã¡ã¢ãããã¥ã¼ã® ID var analyticsViewId = GetSettings("analyticsViewId"); var task = service.Data.Ga.Get("ga:" + analyticsViewId, date, date, "ga:pageviews").ExecuteAsync(); task.Wait(); var pv = int.Parse(task.Result.Rows[0][0]); var message = string.Format("æ¨æ¥ã®ãªãæ¥è¨ã®PV㯠{0} ã§ãã", pv); var consumerKey = GetSettings("consumerKey"); var consumerSecret = GetSettings("consumerSecret"); var accessToken = GetSettings("accessToken"); var accessSecret = GetSettings("accessSecret"); var token = Tokens.Create(consumerKey, consumerSecret, accessToken, accessSecret); token.Statuses.UpdateAsync(status => message).Wait(); } catch (Exception e) { Console.WriteLine(e.StackTrace); } } public static string GetSettings(string key) { var value = Environment.GetEnvironmentVariable(key); try { if (!string.IsNullOrWhiteSpace(value)) return value; value = ConfigurationManager.AppSettings[key]; return value; } finally { Console.WriteLine("{0}:{1}", key, value); } } } }
Azure WebJobããã¸ã§ã¯ãã¯ãã¼ã«ã«ç°å¢ã§ã¯ã³ã³ã½ã¼ã«ã¢ããªã±ã¼ã·ã§ã³ã¨ãã¦å®è¡ã§ãã¾ããã³ã³ã½ã¼ã«ã¢ããªã±ã¼ã·ã§ã³ã¨ä½ãéãã®ï¼ã¨è¨ãããã¨ãæ£ç´éãã¯ãªãã¨æãã¾ããAzure WebJobã¨ãã¦çºè¡ã§ãããã©ããã®éããããªãã§ããããã
ã¡ãªã¿ã«ãã¢ã¯ã»ã¹ãã¼ã¯ã³çã®è¨å®ã¯å æ¥æ¸ããããã°ï¼Î±ã®å¦ç½®ããã¦ã¾ãã
configãã¡ã¤ã«ããè¨å®èªã¿è¾¼ãã ãã§è¯ãã¯ããªã®ã«ä½ã§ç°å¢å¤æ°ã¾ã§è¦ã¦ããã®ãã¯ããã®å¾ã§èª¬æãã¾ãã
App.configã¯ãããªæãã
<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings file="D:\Visual Studio 2013\ç§å¯ã®è¨å®\TweetPV_WebJob\local.config"> <add key="analyticsKeyFile" value="key.p12" /> <add key="analyticsCredentialId" value="*****@developer.gserviceaccount.com" /> <add key="analyticsViewId" value="99999999" /> <add key="consumerKey" value="*****" /> <add key="consumerSecret" value="*****" /> <add key="accessToken" value="*****" /> <add key="accessSecret" value="*****" /> </appSettings> ãï½ç¥ï½ </configuration>
Azureã«çºè¡ãã
ããã¦ãAzureã«ãããã¤ããããã§ãããWebãµã¤ãã®ä½æããWebJobã®è¨å®ã¾ã§å ¨ã¦Visual Studioã§ã§ãã¡ããã¾ãã
ã½ãªã¥ã¼ã·ã§ã³ã¨ã¯ã¹ããã¼ã©ã®ããã¸ã§ã¯ããå³ã¯ãªãã¯ãã¦ãAzure WebJobã¨ãã¦çºè¡ããããå®è¡ãã¾ãã
ããã¨ã¦ã£ã¶ã¼ããèµ·åãã¦ãçºè¡ããWebãµã¤ãã®æå®ï¼æ°è¦ä½æãå¯ï¼ãWebJobã®è¨å®ãã§ããããã«ãªãã¾ãã
ã¡ãã£ã¨ããåé¡
ã¢ã¯ã»ã¹ãã¼ã¯ã³çã®ç§å¯ã®è¨å®ã«ã¤ãã¦ã¡ãã£ã¨æ©ã¿ã¾ãããWebãµã¤ãã®å ´åã¯ã¢ããªã±ã¼ã·ã§ã³è¨å®ã§ä¸æ¸ãããã°ããããã¼ã¨ããäºãæ¸ãã¾ããããWebJobã¯Webãµã¤ãä¸ã§åãexeãªãã§ãããããªã®ã§ãConfigurationManagerã®AppSettingsã§ã¯ã¢ããªã±ã¼ã·ã§ã³è¨å®ã«æ¸ããå¤ãåã£ã¦ãããã¨ãã§ãã¾ããã
ããã§ãVisual Studio Onlineã®ã³ã³ã½ã¼ã«ã§éãã§ããã¨ã¢ããªã±ã¼ã·ã§ã³è¨å®ã«æ¸ããå 容ãç°å¢å¤æ°ã«åæ ããã¦ããã®ãçºè¦ã
ããã§å ã»ã©ã®ã½ã¼ã¹ã«æ»ãã¾ãããAzureä¸ã§ã¯ç°å¢å¤æ°ã«è¨å®ããã¦ããå¤ã使ç¨ãã¦ããã¼ã«ã«ã§ã¯App.configã®å¤ã使ç¨ããæ§ãªå°ç´°å·¥ãããããã§ãã
ããã¾ã§æ¸ãã¦æ°ãã¤ãã
ã¹ã±ã¸ã¥ã¼ã«å®è¡ããå ´åã«ã¯ãWebJobs-[ãªã¼ã¸ã§ã³]ãã¨ããååã§ã¹ã±ã¸ã¥ã¼ã©ãä½æãããæ§ãªã®ã§ããã西æ¥æ¬ãªã¼ã¸ã§ã³ã§ã¯åãã¦ãªãããã§ãã
ã¹ã±ã¸ã¥ã¼ã©åä½ã§ä½æï¼ã¹ã¯ãªã¼ã³ã·ã§ããã§ã¯ãTweetPVãï¼ããå ´åã¯åãã¦ããã¿ãããªã®ã§ã西æ¥æ¬ã¯WebJobsã«å¯¾å¿ãã¦ãªãã®ããªãã
ã¨æã£ã¦ãç±³å½è¥¿é¨ã«ãããã¤ãã¦ã¿ããã©åãã ã£ãâ¦ã«ããã«ã¯ãã以ä¸é£ããã®ã§ãããã人ã«æãã¦ããããã¨æãã¾ãã
2014/08/10 追è¨
Microsoft Azure WebJobã®ã¹ã±ã¸ã¥ã¼ã©ã®ä»¶ - ãªãæ¥è¨ã«æ¸ãã¾ããããã¹ã±ã¸ã¥ã¼ã©ã¡ããã¨åãã¾ãããåå ã¯ããããããªãã®ã§ããâ¦