HSPでgoogleニューストピックスを取得する

1.概要

日常、テレビのワイドショーや報道番組でも色んなニュースを観ますが、ネットをやっていると テレビもあまり観なくなり、世界の様子や世間の話題などに疎くなってしまいます。PCで作業をしていると、時間を忘れて 今、どんなことが起こっているのかさえも分からなくなってきています。今回は、ニュースに関心を持ってもらうために rss配信情報を受信してgoogleのニューストピックスを表示するものを作成して見ました。 rssは、OutLookに設定しておけば、常に最新の情報を取得してきますが、すぐに、いっぱい溜まってしまい 見るのがだんだんうっとおしくなってしまいます。そこで、最新の10件のみを取得して表示するようにしました。

2.利用方法

gnews.exe 起動します。起動と同時にニューストピックスの先頭から10件のタイトル情報を取得して表示します。 更新頻度が頻繁ではないので、最新情報を取得する場合は、手動で更新アイコンボタンをクリックして 取得するようにして下さい。

3.ソースコード

ソースコードは下記の通りです。別記事で紹介している「HSPでrss利用して地震情報を取得する」と、ほぼ同じです。 違いは、画面をscreen命令ではなく、bgscr命令として作成してある点です。そのため、マウスドラッグで画面を 移動できる処理を加えてあります。プログラムは、HSP言語で作成しています。HSP3には、便利なモジュールが標準で提供されていて、rssを扱うモジュール(mod_rss.as)があります。 そのため、情報を取得する部分の処理が簡単に実装できます。
gnews.hsp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  ;****************************************************************************
  ;*
  ;*    google ニュースの取得・表示 (gnews.hsp)
  ;*
  ;*    <処理の概要>
  ;*    本プログラムは、googleのニュースRSS配信情報を受信して
  ;*    最新トップニュースを表示するものである。
  ;*
  ;*    出典: googleニュース RSS配信情報
  ;*           URL  https://news.google.com/news/rss/headlines/section/
  ;*                topic/WORLD.ja_jp/%E5%9B%BD%E9%9A%9B?ned=jp&hl=ja&gl=JP
  ;*
  ;*
  ;****************************************************************************
 
  #include "mod_rss.as"
  #include "sqlele.hsp"
 
  ;***** 実行ファイル自動作成 (追加) ****
  #packopt type 0
  #packopt name "gnews"
  #packopt runtime "hsprt"
  #packopt manifest "app.manifest"
  #packopt icon "script.ico"
  #packopt hide 1
  #pack "about.png"
  #pack "update3.png"
  #pack "batsu2.png"
 
  #uselib  "user32.dll"
  #func GetWindowRect "GetWindowRect" int,int
  #func GetSystemMetrics "GetSystemMetrics" int
  #func MoveWindow "MoveWindow" int,int,int,int,int,int
  #func global SetWindowLong "SetWindowLongA" int , int , int
  #cfunc global GetWindowLong "GetWindowLongA" int , int
 
  #define global GWL_STYLE  0xFFFFFFF0
  #define global WS_SYSMENU 0x00080000
  #define global WS_MAXIMIZEBOX 0x00010000
  #define global WS_MINIMIZEBOX 0x00020000
   
  #define SM_CXSCREEN          $00000000
  #define SM_CYSCREEN          $00000001
  #define obj_wmnclbtndown sendmsg hwnd,$00A1,2,0
   
  ;***** カーソルの形状を変更 *****
  #module
  #uselib  "user32.dll"
  #func  LoadCursorFromFile "LoadCursorFromFileA" var
  #func  LoadCursor "LoadCursorA" int,int
  #func   SetClassLong "SetClassLongA" int,int,int
  #func   SetCursor "SetCursor" int
  #define IDC_ARROW $00007F00
 
  #deffunc chengecur int prm1,str prm2,int prm3
  UserMode   = prm1  : if UserMode<0 : return -1
  sdim Curfname,260
  Curfname   = prm2  ;カーソルファイル名
  resourceID = prm3  ;リソースID (0~
 
  ;UserMode=$200 でカーソルファイル名が設定されていた場合は、
  ;ユーザーカーソルを設定
  if (UserMode==$200)&(strlen(Curfname)!0) {
    LoadCursorFromFile varptr(Curfname)
    hcurwnd=stat
    SetClassLong hwnd,-12,hcurwnd
    SetCursor hcurwnd
    sdim Curfname,0
    return 0
  }
  ;リソースIDに対応するカーソルを設定 (object Mode) (ID=0~28)
  hinst=0 : lpszCursor=IDC_ARROW
  if resourceID>=5 : resourceID+=123
  LoadCursor hinst,resourceID+lpszCursor
  hcurwnd=stat
  ;if UserMode==$100 {
  ; hCld=hwnd
  ; }else{
      hCld=objinfo(UserMode,2)
  ;}
  SetClassLong hCld,-12,hcurwnd
  SetCursor hcurwnd
  sdim Curfname,0
  return 0
  #global
   
  #module
  #uselib "kernel32.dll"
  #cfunc  CreateMutex "CreateMutexA" int,int,sptr
  #cfunc  GetLastError "GetLastError"
 
  ;***** 二重起動防止 *****
  #deffunc wexapend str prm1
  strname=prm1
  ;***** 名前の文字列が省略された場合 *****
  if strlen(strname)==0 : strname="HSP340ONIWND"  ;Default String
  ret=CreateMutex(0,1,strname)
  ;二重起動か?
  if GetLastError()==0    : return 0  ;同じジョブが起動していない
  if GetLastError()==183  : return 1  ;既に起動している
  return -1
  #global
   
  ;***** 起動ディレクトリ取得 *****
  sdim Startdir,512
  if hspstat&1=0 { Startdir=dir_exe+"¥¥" : chdir dir_exe
  } else {
    Startdir=dir_cur+"¥¥"
  }
  chdir Startdir
 
  ;***** データベース存在確認 *****
  exist Startdir+"setting¥¥gnews.ini"
  if strsize ==-1 {
    dialog "データベースファイルが見つかりません。",0 : end
  }
  await
  wexapend "gnews" : if stat : end
   
  ;***** データベースオープン *****
  sql_open Startdir+"setting¥¥gnews.ini"
  ;***** 画面表示位置設定 *****
  sql_q "SELECT * FROM TValu WHERE ID = "+1
  xpos=sql_i("wxpos") : ypos=sql_i("wypos")
  if (xpos<0 or xpos>ginfo_dispx) : xpos=0
  if (ypos<0 or ypos>ginfo_dispy) : ypos=0
  buffer 2,20,20,0 : picload "update3.png",0
  buffer 3,11,11,0 : picload "batsu2.png",0
 
*main
  bgscr 0,300,320,0,xpos,ypos
  SetWindowLong hwnd,GWL_STYLE,$B0000|GetWindowLong(hwnd,GWL_STYLE)
  color 64,64,64 : boxf 0,0,300,30
  font "Meiryo UI",12,1
  color 255,255,255 : pos 20,8 : mes "世界 - 最新 - Google ニュース"
  color 64,64,64 : boxf 0,270,300,320
  pos 280,10 : objsize 11,11 : objimage 3,0,0,0,11   : button "",*owari
  pos 20,285 : picload "about.png",1
  pos 120,285 : objsize 30,30 : objimage 2,0,0,30,30 : button "",*movpos
  chengecur 0,"",14
  onexit *owari
  onclick gosub *move
  color 255,220,100 : boxf 0,30,300,270
 
  font "Meiryo UI",24,1
  color 200,0,0 : pos 75,100 : mes "データ取得中!"
  pos 0, 30 : axobj ie, "Shell.Explorer.2", 300, 240
  if stat == -1 {
    dialog "ActiveXコントロールの配置に失敗しました。", 1
    end
  }
 
  url="https://news.google.com/news/rss/headlines/section/topic/WORLD.ja_jp/%E5%9B%BD%E9%9A%9B?ned=jp&hl=ja&gl=JP"
  rssload desc, link, url, 11
 
  title "google - ニュース"
  sdim strtemp,128 : sdim code,5000
  code = "<html><head>¥n"
  code+= "<meta http-equiv=¥"Content-Type¥" content=¥"text/html; charset=shift-jis¥">¥n"
  code+= "<meta http-equiv=¥"Content-Script-Type¥" content=¥"text/javascript¥">¥n"
  code+= "<meta http-equiv=¥"Content-Style-Type¥"  content=¥"text/css¥">¥n"
  code+= "<style type=¥"text/css¥">¥n"
  code+= ".pad {¥n"
  code+= "   padding-left: 5px;¥n"
  code+= "}¥n"
  code+= ".intoro {¥n"
  code+= "   font-family : Meiryo;¥n"
  code+= "   font-size:8pt;¥n"
  code+= "   cursor:hand;¥n"
  code+= "   a         { text-decoration:underline; }
  code+= "   a:link    { text-decoration:none; color:#000099; }¥n"
  code+= "   a:visited { text-decoration:none; color:#000099; }¥n"
  code+= "   a:active  { text-decoration:none; color:#000099; }¥n"
  code+= "   a:hover   { text-decoration:underline; color:#dd0000; }¥n"
  code+= "}¥n"
  code+= ".fmerea {¥n"
  code+= "   font-family : Meiryo;¥n"
  code+= "   font-size:10pt;¥n"
  code+= "   color:#000099;¥n"
  code+= "}¥n"
  code+= "</style></head><body style=¥"overflow:auto;¥" bgcolor=¥"#ffdc64¥">¥n"
   
  if stat == 1 {
    dialog "取得に失敗しました。"
    code += "</body></html>¥n"
    stop
  }
  if stat == 2 {
    dialog "RSSではありません。"
    code += "</body></html>¥n"
    stop
  }
  p=0
 
  foreach desc
    if cnt<1 : continue
    p++
    ;***** 記事のタイトル長さ制限 *****
    q=strlen(desc(cnt))
    if q>40 {
      strtemp=strmid(desc(cnt),0,38)+"....."
    }else{
      strtemp=desc(cnt)
    }
    ;***** 記事番号 *****
    if p<10 {
      no = "0" + p
    }else{
      no = p
    }
    code+= "<span class=\"fmerea\">" + no + "." + " </span>"
    code+= "<a href=\"" + link(cnt) + "\" target=\"_blank\" class=\"intoro\">" + strtemp + "</a>
\n"
  loop
 
    code += "</body></html>¥n"
 
    ie -> "Navigate" "about:blank"
    doc = ie("Document")
    doc -> "write" code
 
    gsel 0,1
     
  ;***** 10分毎に更新 *****
  ;wait 1000*60 : goto *main
  stop
 
*movpos
  xpos=ginfo_wx1 : ypos=ginfo_wy1
  goto *main
 
*owari
  await
  ;***** 終了処理 (DB CLOSE) *****
  wx=str(ginfo_wx1) ; 現在の画面左上 X 座標取得
  wy=str(ginfo_wy1) ; 現在の画面左上 y 座標取得
  ;***** 終了位置をDBへ保存 *****
  sql_q "UPDATE TValu SET wxpos=" + prm_text(wx) + ", wypos=" + prm_text(wy)+ " WHERE ID="+1
  sql_close
  end
 
*move
  ;***** マウスの左クリックボタンで移動 *****
  if wparam == 1 {
    obj_wmnclbtndown
    ;***** 画面終端位置の取得と制御 *****
    dim rc,4
    GetWindowRect hwnd,varptr(rc)
    w = rc(2)-rc(0)
    h = rc(3)-rc(1)
    GetSystemMetrics SM_CXSCREEN : CSX=stat
    GetSystemMetrics SM_CYSCREEN : CSY=stat
    if rc(0)<0 : rc(0)=0
    if rc(1)<0 : rc(1)=0
    if rc(2)>CSX : rc(0)=CSX-w
    if rc(3)>CSY : rc(1)=CSY-h-30
    MoveWindow hwnd,rc(0),rc(1),w,h,1
    }
  return

4.ダウンロード

提供するソースコードのライセンスは、CC0 (クレジット表示不要、改変可、商用可) とします。自由に利用して頂いてかまいません。 尚、データの取得やプログラム実行において損害等が生じた場合は、筆者は一切の責任も負いません。全て自己責任でお願いします。

紹介したrss地震情報取得 (goorss.exe)は、下記よりダウンロードして下さい。

ダウンロード

■関連記事
・HSPでrss利用して地震情報を取得する

コメント

このブログの人気の投稿

Excelアドインで日本語形態素解析

階層構造JSONファイルの作成

キーボードのキーコードの一覧表