APIデータを自動投稿するpythonプログラム
■複数記事を自動でwordpressに投稿
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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
import pandas as pd import plotly.io as pio import plotly.offline as pyo import plotly.graph_objs as go import urllib.request from time import sleep from datetime import datetime, date, timedelta from dateutil.relativedelta import relativedelta import upftp import math import json from wordpress_xmlrpc import Client, WordPressPost from wordpress_xmlrpc.methods.posts import GetPosts, NewPost from wordpress_xmlrpc.methods.users import GetUserInfo import ssl ssl._create_default_https_context = ssl._create_unverified_context #hsコードから品名を取得する df_hsname = pd.read_csv("article.csv") #パラメーターの作成 classification = "&px=H4" fmt = "&fmt=csv" partner_area = "&p=0" amount_country = 50 #各国のコードを取得する準備で国の数を取得しjsonを読み込む url = urllib.request.urlopen("https://comtrade.un.org/data/cache/partnerAreas.json") jdata = json.loads(url.read().decode()) amount_country = int(len(jdata['results'])) #クエリ作成とトークン入力 def make_url(reporting_area,import_or_export): url = ("https://comtrade.un.org/api/get?type=C" + reporting_area+classification+partner_area + "&rg="+ str(import_or_export)+"&cc="+com + fmt + "&token=") # print(url) return url #APIが混雑している際に再取得する def fetch_url(url): for x in range(30): try: f = urllib.request.urlopen(url) except Exception as e: pass print("トライ30回中" + str(x + 1) + "回目") print("1分間の停止後リトライします。") sleep(60) else: return f break else: print("全て失敗しました") #URLからcsvをダウンロードし、dfに追加していく def get_data(import_or_export): df = pd.DataFrame(columns=[1,2]) for i in range(2,amount_country): reporting_area = "&r=" + (jdata['results'][i]["id"]) added_area = make_url(reporting_area,import_or_export) added_res = fetch_url(added_area) added_df = pd.read_csv(added_res) df = pd.concat([df,added_df],sort=False) # print(str(i)+"回目") df=df.dropna(subset=['Trade Value (US$)']) return df # hsコードから品名を取得する f = open('hscodeH4.json', 'r') jsonData = json.load(f) for i in range(len(df_hsname)): now = datetime.now() print('now:', now) com = str(df_hsname["HS"][i]) if len(com) == 5: com = "0" + com itemname = str(df_hsname["TITLE"][i]) for x in range(len(jsonData["results"])): if jsonData['results'][x]["id"] == com: itemname2 = jsonData['results'][x]["text"] itemname2 = itemname2[9:] fullname = str(itemname2) print("次の対象hsは" + com) print("記事タイトルは" + itemname) print("品名は" + fullname) #輸出グラフ作成用dfを作成 df = get_data(2) df.to_csv("Log/get_expt.csv") #valueが高い順に並べ替え df_s = df.sort_values('Trade Value (US$)', ascending=False) #国名リスト化 partner = [] for var in df_s["Reporter"]: partner.append(var) #金額のみのグラフ data = [] valuee = [] for i in partner: df2 = df[df["Reporter"] == i] trace = go.Bar(x=df2["Reporter"], y=df2['Trade Value (US$)'], text = "M=million$ B=billion$", name = i ) data.append(trace) valuee.append(df2.iat[0,33]) layoute1 = go.Layout( title= itemname + "(HScode:" + com + ") Export Trade Statistics in 2017") fig = go.Figure(data=data, layout=layoute1) # pio.write_image(fig,'IMG/' + com + '.jpeg') # print("画像書き出し完了") pyo.plot(fig,filename= com +"expvalue"+ ".html", auto_open=False) #個数、重量のグラフ df['value/kg'] = 0 df['value/unit'] = 0 df.dropna(subset=['Netweight (kg)'],inplace=True) df3 = df.reset_index(drop=True) df3["Netweight (kg)"] = df3["Netweight (kg)"].astype(int) df3['Year'] = df3['Year'].astype(int) for i in range(len(df3)): if df3["Netweight (kg)"][i] == 0: df3 = df3.drop(i, axis=0) df3 = df3.reset_index(drop=True) for i in range(len(df3)): if df3["Netweight (kg)"][i] < 10000: df3.drop(i, axis=0,inplace=True) df3 = df3.reset_index(drop=True) df3['value/kg'] = df3['value/kg'].astype(float) for i in range(len(df3)): df3['value/kg'][i] = df3['Trade Value (US$)'][i] / df3['Netweight (kg)'][i] #valueが高い順に並べ替え df3.sort_values('value/kg', ascending=True,inplace=True) df3 = df3.reset_index(drop=True) df3.to_csv("expt.csv") #少なすぎる場合のエラーを防ぐ為に20以下でも対応可能にする if len(df3["Reporter"]) > 20: append_times = 20 else: append_times = len(df3["Reporter"]) # #国名リスト化(top10) partner2 = [] for var in range(append_times): partner2.append(df3["Reporter"][var]) data2 = [] pkge = [] for i in partner2: df4 = df3[df3["Reporter"] == i] trace2 = go.Bar(x=df4["Reporter"], y=df4['value/kg'], text = "↑↑Value(US$) Per KG↑↑<br>↓↓Total Export Amount↓↓<br>" + str(df4.iat[0,31]) + "KG", name = i ) data2.append(trace2) pkge.append(round(df4.iat[0,37],2)) layoute2 = go.Layout( title= itemname + " Export Value Per KG for HScode:" + com, width=1000, height=250 ) fig = go.Figure(data=data2, layout=layoute2) pyo.plot(fig,filename= com +"expkg"+ ".html", auto_open=False) #輸入データ #輸入の引数は1 df = get_data(1) df.to_csv("Log/get_impt.csv") #valueが高い順に並べ替え dfi = df.sort_values('Trade Value (US$)', ascending=False) #国名リスト化 partneri = [] for var in dfi["Reporter"]: partneri.append(var) #金額のみのグラフ datai = [] valuei = [] for i in partneri: df2 = dfi[dfi["Reporter"] == i] trace = go.Bar(x=df2["Reporter"], y=df2['Trade Value (US$)'], text = "M=million$ B=billion$", name = i ) datai.append(trace) valuei.append(df2.iat[0,33]) layouti1 = go.Layout( title= itemname + " (HScode:" + com + ")Import Trade Statistics in 2017" ) fig = go.Figure(data=datai, layout=layouti1) pyo.plot(fig,filename= com +"ipvalue"+ ".html",auto_open=False) #個数、重量のグラフ df['value/kg'] = 0 df['value/unit'] = 0 dfi2 = df.dropna(subset=['Netweight (kg)']) dfi3 = dfi2.reset_index(drop=True) for i in range(len(dfi3)): if dfi3["Netweight (kg)"][i] == 0: dfi3 = dfi3.drop(i, axis=0) dfi3 = dfi3.reset_index(drop=True) for i in range(len(dfi3)): if dfi3["Netweight (kg)"][i] < 10000: dfi3.drop(i, axis=0,inplace=True) dfi3 = dfi3.reset_index(drop=True) dfi3['value/kg'] = dfi3['value/kg'].astype(float) dfi3['Year'] = dfi3['Year'].astype(int) for i in range(len(dfi3)): dfi3['value/kg'][i] = dfi3['Trade Value (US$)'][i] / dfi3['Netweight (kg)'][i] #valueが高い順に並べ替え dfi3.sort_values('value/kg', ascending=False,inplace=True) dfi3 = dfi3.reset_index(drop=True) dfi3.to_csv("impt.csv") # #国名リスト化(top10) partner2i = [] #少なすぎる場合のエラーを防ぐ為に20以下でも対応可能にする if len(dfi3["Reporter"]) > 20: append_timesi = 20 else: append_timesi = len(dfi3["Reporter"]) for var in range(append_timesi): partner2i.append(dfi3["Reporter"][var]) data3 = [] pkgi = [] for i in partner2i: dfi4 = dfi3[dfi3["Reporter"] == i] trace3 = go.Bar(x=dfi4["Reporter"], y=dfi4['value/kg'], text = "↑↑Value(US$) Per KG↑↑<br>↓↓Total Import Amount(KG)↓↓<br>" + str(dfi4.iat[0,31]) , name = i ) data3.append(trace3) pkgi.append(round(dfi4.iat[0,37],2)) layouti2 = go.Layout( title= itemname + " Import Value Per KG for HScode:" + com, width=1000, height=250 ) fig = go.Figure(data=data3, layout=layouti2) pyo.plot(fig,filename= com +"impkg"+ ".html", auto_open=False) print("輸入全体グラフ作成作業終了") #'Trade Value (US$)'が高い順に並べ替え df3.sort_values('Trade Value (US$)', ascending=False,inplace=True) df3 = df3.reset_index(drop=True) dfi3.sort_values('Trade Value (US$)', ascending=False,inplace=True) dfi3 = dfi3.reset_index(drop=True) #金額をカンマ区切りにする for i in range(len(df3['Trade Value (US$)'])): a = df3['Trade Value (US$)'][i] df3['Trade Value (US$)'][i] = "{:,}".format(float(a)) for i in range(len(dfi3['Trade Value (US$)'])): a = dfi3['Trade Value (US$)'][i] dfi3['Trade Value (US$)'][i] = "{:,}".format(float(a)) #ウェイトをカンマ区切りにする for i in range(len(df3['Netweight (kg)'])): a = df3['Netweight (kg)'][i] df3['Netweight (kg)'][i] = "{:,}".format(float(a)) for i in range(len(dfi3['Netweight (kg)'])): a = dfi3['Netweight (kg)'][i] dfi3['Netweight (kg)'][i] = "{:,}".format(float(a)) #value/kgを下2桁表示にする for i in range(len(dfi3['value/kg'])): a = dfi3['value/kg'][i] dfi3['value/kg'][i] = round(dfi3['value/kg'][i],2) for i in range(len(df3['value/kg'])): a = df3['value/kg'][i] df3['value/kg'][i] = round(df3['value/kg'][i],2) #テーブル化で数値文字化けを防ぐ dfi3['Trade Value (US$)'] = dfi3['Trade Value (US$)'].astype(str) df3['Trade Value (US$)'] = df3['Trade Value (US$)'].astype(str) upftp.up_ftptest() def bunsho(): #投稿用記事出力 up_url = "\"https://hstariffstat.com/html/" tag_st = "[advanced_iframe use_shortcode_attributes_only=\"true\" src=" end_value = " ""width=\"100%\" height=\"600\" id=\"advanced_iframe\" ]" end_kg = " ""width=\"100%\" height=\"300\" id=\"advanced_iframe\" ]" class_tip = "<div class=\"center-block\">" class_end = "</div>" #タイトル記事 titlekiji = class_tip + "This article has been written to share the Accurate Statistical Trade Ranking by country. <br>" + \ "The graphs that have been shared show the International Trade Price and Weight of <br><b>\"" +fullname + "\"</b>(HScode:" + com +")<br>" + \ "<br>These graphs may help you to find new Buyer and Supplier Country. " + \ "<br>All the Statistical data that is depicted here is taken from <a href=https://comtrade.un.org/>UN Comtrade Database.</a><p>" + class_end #輸出価格 exprice = "<h1 id=\"rittai\">Export Trade Statistics of \""+ itemname + "\"(HScode:" + com +")" + "</h1><br><br>" + class_tip + \ "Here is a graph which shows export statistics of<br><b>" + itemname + "</b>(HScode:" + com + ") worldwide in " + str(df3["Year"][0]) + \ " <br>With the help of these statistics, you can get the information about the ranking of the top export countries worldwide in" + str(df3["Year"][0]) + "<br>" + class_end + \ tag_st + up_url + com +"expvalue"+ ".html\"" + end_value+class_tip + \ "According to the stats, "+ \ partner[0] + " ranked at the top in exports of \"" + itemname +"\"" + " total export value " + "{:,}".format(valuee[0]) + "USD <br>" + \ partner[0] + " is followed by " + \ partner[1] + ": " + "{:,}".format(valuee[1]) + "USD <br>" +\ partner[2] + ": " + "{:,}".format(valuee[2]) + "USD <br>" +\ partner[3] + ": " + "{:,}".format(valuee[3]) + "USD <br>" +\ partner[4] + ": " + "{:,}".format(valuee[4]) + "USD and <br>" +\ partner[5] + ": " + "{:,}".format(valuee[5]) + "USD <br>" + "<p>"+class_end #輸出キロ exkgvalue = "<h1 id=\"rittai\">Supplier country’s \"export value per KG\" of \""+ itemname + "</h1><br><br>"+ class_tip + \ "Another graph below is displaying: <br><b>\"The export value per KG in USD\"</b>"+ \ "(The Total Weight divides the Total Export Value) <br>"+ \ "This graph shows which country exports items at a lower price in comparison with the other countries. <p>"+ \ class_end + \ tag_st +up_url + com +"expkg"+ ".html\"" + end_kg+ \ class_tip + \ partner2[0] + " ranked first in Exports \"" + itemname +"\" at the lowest price of " + str(pkge[0]) + "USD/KG " + \ partner2[0] + " is followed by " + partner2[1] + str(pkge[1]) + "USD/KG" + ", " + \ partner2[2] + str(pkge[2]) + "USD/KG"+ ", " + \ partner2[3] + str(pkge[3]) + "USD/KG"+ ", " + \ partner2[4] + str(pkge[4]) + "USD/KG"+ \ ", and " + \ partner2i[5] + str(pkge[4]) + "USD/KG" + "<p>" + class_end #輸入価格 imprice = "<h1 id=\"rittai\">Import Trade Statistics of \""+ itemname + "\"(HScode:" + com +")" + "</h1><br><br>" + class_tip + \ "The statistics show the ranking of<br><b>" + itemname + "</b>(HScode:" + com + ") worldwide in " + str(df3["Year"][0]) + class_end + \ tag_st + up_url + com + "ipvalue"+ ".html\"" + end_value + class_tip + \ "the top importing countries worldwide in " + str(df3["Year"][0]) + "<br>" +\ partneri[0] + " ranked first in imports of \"" + itemname +"\"" + " Total import value " + "{:,}".format(valuei[0]) + "USD <br>" + \ partneri[0] + " is followed by " + \ partneri[1] + ": " + "{:,}".format(valuei[1]) + "USD <br>" + \ partneri[2] + ": " + "{:,}".format(valuei[2]) + "USD <br>" + \ partneri[3] + ": " + "{:,}".format(valuei[3]) + "USD <br>" + \ partneri[4] + ": " + "{:,}".format(valuei[4]) + "USD and <br>" + \ partneri[5] + ": " + "{:,}".format(valuei[5]) + "USD <br>" + "<p>" + class_end #輸入キロ imkgvalue = "<h1 id=\"rittai\">Buyer country's \"import Value per KG\" of \""+ itemname + "</h1><br><br>" + class_tip + \ "The graph below is displaying: <br><b>\"Import Value per KG in USD\"</b>" + \ "(the total weight divides the total Import value)<br>" + \ "It shows which country imports items at a higher price and comparison with other countries." + class_end + \ tag_st +up_url + com +"impkg"+ ".html\"" + end_kg + class_tip + \ partner2i[0] + " ranked first in imports \"" + itemname +"\" at the highest price of " + str(pkgi[0]) + "/KG " + \ partner2i[0] + " is followed by " + partner2i[1] + ", " + partner2i[2] + ", " + partner2i[3]+ ", " + partner2i[4] + \ ", and " + partner2i[5] + "<p>" + class_end #輸出テーブル作成 cut_e = df3 to_remove = [1,2,'Classification','Period Desc."','Aggregate Level','Is Leaf Code','Trade Flow Code','Reporter Code', 'Partner Code','Partner','Partner ISO','2nd Partner Code','2nd Partner','2nd Partner ISO','Customs Proc. Code', 'Customs','Mode of Transport Code"','Mode of Transport','Commodity Code','Commodity','Qty Unit Code','Flag','value/unit', 'CIF Trade Value (US$)','FOB Trade Value (US$)','Gross weight (kg)','Mode of Transport Code','Period','Period Desc.', 'Qty','Qty Unit','Alt Qty Unit Code'] cut_e = cut_e[cut_e.columns.difference(to_remove)] cut_e = cut_e.rename(columns={'Reporter ISO':'ISO'}) cut_e.sort_values('Trade Value (US$)', ascending=False,inplace=True) cut_e = cut_e.reset_index(drop=True) cut_e2 = cut_e.loc[:,['Reporter','ISO','Year','Trade Flow','Trade Value (US$)','Netweight (kg)','Alt Qty','Alt Qty Unit','value/kg']] exphtml = cut_e2.to_html(classes="extable") cap_exphtml = "<h1 id=\"rittai\">Table data of \""+ itemname + "\"(HS:" + com +")" + "[Export]</h1><br><br>" + \ "<caption>" + itemname + " Export Trading Statistics (HScode:" + com + ")</caption>\n" + exphtml #輸入テーブル作成 cut_i = dfi3 to_remove = [1,2,'Classification','Period Desc."','Aggregate Level','Is Leaf Code','Trade Flow Code','Reporter Code', 'Partner Code','Partner','Partner ISO','2nd Partner Code','2nd Partner','2nd Partner ISO','Customs Proc. Code', 'Customs','Mode of Transport Code"','Mode of Transport','Commodity Code','Commodity','Qty Unit Code','Flag','value/unit', 'CIF Trade Value (US$)','FOB Trade Value (US$)','Gross weight (kg)','Mode of Transport Code','Period','Period Desc.', 'Qty','Qty Unit','Alt Qty Unit Code'] cut_i = cut_i[cut_i.columns.difference(to_remove)] cut_i = cut_i.rename(columns={'Reporter ISO':'ISO'}) cut_i.sort_values('Trade Value (US$)', ascending=False,inplace=True) cut_i = cut_i.reset_index(drop=True) cut_i2 = cut_i.loc[:,['Reporter','ISO','Year','Trade Flow','Trade Value (US$)','Netweight (kg)','Alt Qty','Alt Qty Unit','value/kg']] imphtml = cut_i2.to_html(classes="imtable") cap_imphtml = "<h1 id=\"rittai\">Table data of \""+ itemname + "\"(HS:" + com +")" + "[Import]</h1><br><br>" + \ "<caption>" + itemname + " Import Trading Statistics (HScode:" + com + ")</caption>\n" + imphtml title = itemname + "\"(HS:" + com +")" + "World Trade Statistics" #全て結合 kiji_all= titlekiji + exprice + exkgvalue + imprice + imkgvalue + "<p>" + cap_exphtml + "<p>" + cap_imphtml return kiji_all kiji_all = bunsho() title = itemname + "\"(HS:" + com +")" + "World Trade Statistics" wp = Client('http://hstariffstat.com/xmlrpc.php', 'id', 'pass') post = WordPressPost() post.title = title post.content = kiji_all post.terms_names = {'post_tag': [itemname, 'trade statistics'],'category': ['2017']} post.post_status = 'publish' wp.call(NewPost(post)) print("全て完了") |
■FTPアップロードを行うupftp.py
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 |
#FTPにアップロード import glob import string import os from ftplib import FTP import urllib.request def up_ftp(): filelist=glob.glob('*.html') ftp=FTP('sv2142.xserver.jp') ftp.set_pasv("true") ftp.login('id','pass') ftp.cwd('/hstariffstat.com/public_html/html/') #ホスト側のディレクトリ for file in filelist: # f=open(file,'rb') with open(file, "rb") as f: filename=os.path.basename(file) cmd='STOR '+filename ftp.storbinary(cmd,f) f.close() ftp.quit() for file in filelist: os.remove(file) print("FTPアップロード完了") |
■個別記事を自動でwordpressに投稿
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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
import pandas as pd import plotly.io as pio import plotly.offline as pyo import plotly.graph_objs as go import urllib.request from time import sleep from datetime import datetime, date, timedelta from dateutil.relativedelta import relativedelta import upftp import json from wordpress_xmlrpc import Client, WordPressPost from wordpress_xmlrpc.methods.posts import GetPosts, NewPost from wordpress_xmlrpc.methods.users import GetUserInfo import ssl ssl._create_default_https_context = ssl._create_unverified_context com = input("HSコードを入力してください") com = com.replace('.', '') #hsコードから品名を取得する f = open('hscodeH4.json', 'r') jsonData = json.load(f) for i in range(len(jsonData["results"])): if jsonData['results'][i]["id"] == com: print("発見") itemname = jsonData['results'][i]["text"] itemname = itemname[9:] fullname = itemname print(itemname) prename = input("必要があれば上記品名を変更して下さい。") if prename != "": itemname = prename print("グラフは以下の名前で表示されます" + itemname) #パラメーターの作成 #trade data type C Commodities S Services datatype = "type=C" #china=156,UK=826,US=842,jp=392or all # reporting_area = "&r=842" # #Annual=A,Monthly=M # frequency = "&freq=A" # #YYYY or YYYYMM or now or recent # time_period = "&ps=recent" # Harmonized System (HS) classification = "&px=H4" #china=156,UK=826,US=842,or all=0 partner_area = "&p=0" # 1 (imports) and 2 (exports), # import_or_export = ("&rg=" + imp_or_exp) #HS CODE classification_code = ("&cc=" + com) #csv or json fmt = "&fmt=csv" #対象国の数 amount_country = 50 #TOKEN token = "" #各国のコードを取得する準備で国の数を取得しjsonを読み込む url = urllib.request.urlopen("https://comtrade.un.org/data/cache/partnerAreas.json") jdata = json.loads(url.read().decode()) amount_country = int(len(jdata['results'])) #クエリ作成とトークン入力 def make_url(reporting_area,import_or_export): url = ("https://comtrade.un.org/api/get?"+datatype + reporting_area+classification+partner_area+"&rg=" + str(import_or_export)+classification_code+fmt+"&token=") print(url) return url #APIが混雑している際に再取得する def fetch_url(url): for x in range(30): try: f = urllib.request.urlopen(url) except Exception as e: pass print("トライ30回中" + str(x + 1) + "回目") print("1分間の停止後リトライします。") sleep(60) else: return f break else: print("全て失敗しました") #URLからcsvをダウンロードし、dfに追加していく def get_data(import_or_export): df = pd.DataFrame(columns=[1,2]) for i in range(2,amount_country): reporting_area = "&r=" + (jdata['results'][i]["id"]) added_area = make_url(reporting_area,import_or_export) added_res = fetch_url(added_area) added_df = pd.read_csv(added_res) df = pd.concat([df,added_df],sort=False) print(str(i)+"回目") now = datetime.now() print('now:', now) df=df.dropna(subset=['Trade Value (US$)']) return df # #輸出データ #輸出の引数は2 df = get_data(2) #valueが高い順に並べ替え df_s = df.sort_values('Trade Value (US$)', ascending=False) #国名リスト化 partner = [] for var in df_s["Reporter"]: partner.append(var) #金額のみのグラフ data = [] valuee = [] for i in partner: df2 = df[df["Reporter"] == i] trace = go.Bar(x=df2["Reporter"], y=df2['Trade Value (US$)'], text = "M=million$ B=billion$", name = i ) data.append(trace) valuee.append(df2.iat[0,33]) layoute1 = go.Layout( title= itemname + "(HScode:" + com + ") Export Trade Statistics in 2017") fig = go.Figure(data=data, layout=layoute1) pio.write_image(fig, com + '.jpeg') pyo.plot(fig,filename= com +"expvalue"+ ".html", auto_open=False) #個数、重量のグラフ df['value/kg'] = 0 df['value/unit'] = 0 df.dropna(subset=['Netweight (kg)'],inplace=True) df3 = df.reset_index(drop=True) df3["Netweight (kg)"] = df3["Netweight (kg)"].astype(int) df3['Year'] = df3['Year'].astype(int) for i in range(len(df3)): if df3["Netweight (kg)"][i] == 0: df3 = df3.drop(i, axis=0) df3 = df3.reset_index(drop=True) for i in range(len(df3)): if df3["Netweight (kg)"][i] < 10000: df3.drop(i, axis=0,inplace=True) df3 = df3.reset_index(drop=True) df3['value/kg'] = df3['value/kg'].astype(float) for i in range(len(df3)): df3['value/kg'][i] = df3['Trade Value (US$)'][i] / df3['Netweight (kg)'][i] #valueが高い順に並べ替え df3.sort_values('value/kg', ascending=True,inplace=True) df3 = df3.reset_index(drop=True) df3.to_csv("expt.csv") #少なすぎる場合のエラーを防ぐ為に20以下でも対応可能にする if len(df3["Reporter"]) > 20: append_times = 20 else: append_times = len(df3["Reporter"]) # #国名リスト化(top10) partner2 = [] for var in range(append_times): partner2.append(df3["Reporter"][var]) data2 = [] pkge = [] for i in partner2: df4 = df3[df3["Reporter"] == i] trace2 = go.Bar(x=df4["Reporter"], y=df4['value/kg'], text = "↑↑Value(US$) Per KG↑↑<br>↓↓Total Export Amount↓↓<br>" + str(df4.iat[0,31]) + "KG", name = i ) data2.append(trace2) pkge.append(round(df4.iat[0,37],2)) layoute2 = go.Layout( title= itemname + " Export Value Per KG for HScode:" + com, width=1000, height=250 ) fig = go.Figure(data=data2, layout=layoute2) pyo.plot(fig,filename= com +"expkg"+ ".html", auto_open=False) #輸入データ #輸入の引数は1 df = get_data(1) #valueが高い順に並べ替え dfi = df.sort_values('Trade Value (US$)', ascending=False) #国名リスト化 partneri = [] for var in dfi["Reporter"]: partneri.append(var) #金額のみのグラフ datai = [] valuei = [] for i in partneri: df2 = dfi[dfi["Reporter"] == i] trace = go.Bar(x=df2["Reporter"], y=df2['Trade Value (US$)'], text = "M=million$ B=billion$", name = i ) datai.append(trace) valuei.append(df2.iat[0,33]) layouti1 = go.Layout( title= itemname + " (HScode:" + com + ")Import Trade Statistics in 2017" ) fig = go.Figure(data=datai, layout=layouti1) pyo.plot(fig,filename= com +"ipvalue"+ ".html",auto_open=False) #個数、重量のグラフ df['value/kg'] = 0 df['value/unit'] = 0 dfi2 = df.dropna(subset=['Netweight (kg)']) dfi3 = dfi2.reset_index(drop=True) for i in range(len(dfi3)): if dfi3["Netweight (kg)"][i] == 0: dfi3 = dfi3.drop(i, axis=0) dfi3 = dfi3.reset_index(drop=True) for i in range(len(dfi3)): if dfi3["Netweight (kg)"][i] < 10000: dfi3.drop(i, axis=0,inplace=True) dfi3 = dfi3.reset_index(drop=True) dfi3['value/kg'] = dfi3['value/kg'].astype(float) dfi3['Year'] = dfi3['Year'].astype(int) for i in range(len(dfi3)): dfi3['value/kg'][i] = dfi3['Trade Value (US$)'][i] / dfi3['Netweight (kg)'][i] #valueが高い順に並べ替え dfi3.sort_values('value/kg', ascending=False,inplace=True) dfi3 = dfi3.reset_index(drop=True) dfi3.to_csv("impt.csv") # #国名リスト化(top10) partner2i = [] #少なすぎる場合のエラーを防ぐ為に20以下でも対応可能にする if len(dfi3["Reporter"]) > 20: append_timesi = 20 else: append_timesi = len(dfi3["Reporter"]) for var in range(append_timesi): partner2i.append(dfi3["Reporter"][var]) data3 = [] pkgi = [] for i in partner2i: dfi4 = dfi3[dfi3["Reporter"] == i] trace3 = go.Bar(x=dfi4["Reporter"], y=dfi4['value/kg'], text = "↑↑Value(US$) Per KG↑↑<br>↓↓Total Import Amount(KG)↓↓<br>" + str(dfi4.iat[0,31]) , name = i ) data3.append(trace3) pkgi.append(round(dfi4.iat[0,37],2)) layouti2 = go.Layout( title= itemname + " Import Value Per KG for HScode:" + com, width=1000, height=250 ) fig = go.Figure(data=data3, layout=layouti2) pyo.plot(fig,filename= com +"impkg"+ ".html", auto_open=False) #'Trade Value (US$)'が高い順に並べ替え df3.sort_values('Trade Value (US$)', ascending=False,inplace=True) df3 = df3.reset_index(drop=True) dfi3.sort_values('Trade Value (US$)', ascending=False,inplace=True) dfi3 = dfi3.reset_index(drop=True) #金額をカンマ区切りにする for i in range(len(df3['Trade Value (US$)'])): a = df3['Trade Value (US$)'][i] df3['Trade Value (US$)'][i] = "{:,}".format(float(a)) for i in range(len(dfi3['Trade Value (US$)'])): a = dfi3['Trade Value (US$)'][i] dfi3['Trade Value (US$)'][i] = "{:,}".format(float(a)) #ウェイトをカンマ区切りにする for i in range(len(df3['Netweight (kg)'])): a = df3['Netweight (kg)'][i] df3['Netweight (kg)'][i] = "{:,}".format(float(a)) for i in range(len(dfi3['Netweight (kg)'])): a = dfi3['Netweight (kg)'][i] dfi3['Netweight (kg)'][i] = "{:,}".format(float(a)) #value/kgを下2桁表示にする for i in range(len(dfi3['value/kg'])): a = dfi3['value/kg'][i] dfi3['value/kg'][i] = round(dfi3['value/kg'][i],2) for i in range(len(df3['value/kg'])): a = df3['value/kg'][i] df3['value/kg'][i] = round(df3['value/kg'][i],2) #テーブル化で数値文字化けを防ぐ dfi3['Trade Value (US$)'] = dfi3['Trade Value (US$)'].astype(str) df3['Trade Value (US$)'] = df3['Trade Value (US$)'].astype(str) upftp.up_ftp() #全ての文字列を一つの変数に入れてpyperclip.copy()でコピーできるようにする def bunsho(): #投稿用記事出力 up_url = "\"https://hstariffstat.com/html/" tag_st = "[advanced_iframe use_shortcode_attributes_only=\"true\" src=" end_value = " ""width=\"100%\" height=\"600\" id=\"advanced_iframe\" ]" end_kg = " ""width=\"100%\" height=\"300\" id=\"advanced_iframe\" ]" class_tip = "<div class=\"center-block\">" class_end = "</div>" #タイトル記事 titlekiji = class_tip + "This article has been written to share the Accurate Statistical Trade Ranking by country. <br>" + \ "The graphs that have been shared show the International Trade Price and Weight of <br><b>\"" +fullname + "\"</b>(HScode:" + com +")" + \ "<br>These graphs may help you to find new Buyer and Supplier Country. " + \ "<br>All the Statistical data that is depicted here is taken from <a href=https://comtrade.un.org/>UN Comtrade Database.</a><p>" + class_end #輸出価格 exprice = "<h1 id=\"rittai\">Export Trade Statistics of \""+ itemname + "\"(HScode:" + com +")" + "</h1><br><br>" + class_tip + \ "Here is a graph which shows export statistics of<br><b>" + itemname + "</b>(HScode:" + com + ") worldwide in " + str(df3["Year"][0]) + \ " <br>With the help of these statistics, you can get the information about the ranking of the top export countries worldwide in" + str(df3["Year"][0]) + "<br>" + class_end + \ tag_st + up_url + com +"expvalue"+ ".html\"" + end_value+class_tip + \ "According to the stats, "+ \ partner[0] + " ranked at the top in exports of \"" + itemname +"\"" + " total export value " + "{:,}".format(valuee[0]) + "USD <br>" + \ partner[0] + " is followed by " + \ partner[1] + ": " + "{:,}".format(valuee[1]) + "USD <br>" +\ partner[2] + ": " + "{:,}".format(valuee[2]) + "USD <br>" +\ partner[3] + ": " + "{:,}".format(valuee[3]) + "USD <br>" +\ partner[4] + ": " + "{:,}".format(valuee[4]) + "USD and <br>" +\ partner[5] + ": " + "{:,}".format(valuee[5]) + "USD <br>" + "<p>"+class_end #輸出キロ exkgvalue = "<h1 id=\"rittai\">Supplier country’s \"export value per KG\" of \""+ itemname + "</h1><br><br>"+ class_tip + \ "Another graph below is displaying: <br><b>\"The export value per KG in USD\"</b>"+ \ "(The Total Weight divides the Total Export Value) <br>"+ \ "This graph shows which country exports items at a lower price in comparison with the other countries. <p>"+ \ class_end + \ tag_st +up_url + com +"expkg"+ ".html\"" + end_kg+ \ class_tip + \ partner2[0] + " ranked first in Exports \"" + itemname +"\" at the lowest price of " + str(pkge[0]) + "USD/KG " + \ partner2[0] + " is followed by " + partner2[1] + str(pkge[1]) + "USD/KG" + ", " + \ partner2[2] + str(pkge[2]) + "USD/KG"+ ", " + \ partner2[3] + str(pkge[3]) + "USD/KG"+ ", " + \ partner2[4] + str(pkge[4]) + "USD/KG"+ \ ", and " + \ partner2i[5] + str(pkge[4]) + "USD/KG" + "<p>" + class_end #輸入価格 imprice = "<h1 id=\"rittai\">Import Trade Statistics of \""+ itemname + "\"(HScode:" + com +")" + "</h1><br><br>" + class_tip + \ "The statistics show the ranking of<br><b>" + itemname + "</b>(HScode:" + com + ") worldwide in " + str(df3["Year"][0]) + class_end + \ tag_st + up_url + com + "ipvalue"+ ".html\"" + end_value + class_tip + \ "the top importing countries worldwide in " + str(df3["Year"][0]) + "<br>" +\ partneri[0] + " ranked first in imports of \"" + itemname +"\"" + " Total import value " + "{:,}".format(valuei[0]) + "USD <br>" + \ partneri[0] + " is followed by " + \ partneri[1] + ": " + "{:,}".format(valuei[1]) + "USD <br>" + \ partneri[2] + ": " + "{:,}".format(valuei[2]) + "USD <br>" + \ partneri[3] + ": " + "{:,}".format(valuei[3]) + "USD <br>" + \ partneri[4] + ": " + "{:,}".format(valuei[4]) + "USD and <br>" + \ partneri[5] + ": " + "{:,}".format(valuei[5]) + "USD <br>" + "<p>" + class_end #輸入キロ imkgvalue = "<h1 id=\"rittai\">Buyer country's \"import Value per KG\" of \""+ itemname + "</h1><br><br>" + class_tip + \ "The graph below is displaying: <br><b>\"Import Value per KG in USD\"</b>" + \ "(the total weight divides the total Import value)<br>" + \ "It shows which country imports items at a higher price and comparison with other countries." + class_end + \ tag_st +up_url + com +"impkg"+ ".html\"" + end_kg + class_tip + \ partner2i[0] + " ranked first in imports \"" + itemname +"\" at the highest price of " + str(pkgi[0]) + "/KG " + \ partner2i[0] + " is followed by " + partner2i[1] + ", " + partner2i[2] + ", " + partner2i[3]+ ", " + partner2i[4] + \ ", and " + partner2i[5] + "<p>" + class_end #輸出テーブル作成 cut_e = df3 to_remove = [1,2,'Classification','Period Desc."','Aggregate Level','Is Leaf Code','Trade Flow Code','Reporter Code', 'Partner Code','Partner','Partner ISO','2nd Partner Code','2nd Partner','2nd Partner ISO','Customs Proc. Code', 'Customs','Mode of Transport Code"','Mode of Transport','Commodity Code','Commodity','Qty Unit Code','Flag','value/unit', 'CIF Trade Value (US$)','FOB Trade Value (US$)','Gross weight (kg)','Mode of Transport Code','Period','Period Desc.', 'Qty','Qty Unit','Alt Qty Unit Code'] cut_e = cut_e[cut_e.columns.difference(to_remove)] cut_e = cut_e.rename(columns={'Reporter ISO':'ISO'}) cut_e.sort_values('Trade Value (US$)', ascending=False,inplace=True) cut_e = cut_e.reset_index(drop=True) cut_e2 = cut_e.loc[:,['Reporter','ISO','Year','Trade Flow','Trade Value (US$)','Netweight (kg)','Alt Qty','Alt Qty Unit','value/kg']] exphtml = cut_e2.to_html(classes="extable") cap_exphtml = "<h1 id=\"rittai\">Table data of \""+ itemname + "\"(HS:" + com +")" + "[Export]</h1><br><br>" + \ "<caption>" + itemname + " Export Trading Statistics (HScode:" + com + ")</caption>\n" + exphtml #輸入テーブル作成 cut_i = dfi3 to_remove = [1,2,'Classification','Period Desc."','Aggregate Level','Is Leaf Code','Trade Flow Code','Reporter Code', 'Partner Code','Partner','Partner ISO','2nd Partner Code','2nd Partner','2nd Partner ISO','Customs Proc. Code', 'Customs','Mode of Transport Code"','Mode of Transport','Commodity Code','Commodity','Qty Unit Code','Flag','value/unit', 'CIF Trade Value (US$)','FOB Trade Value (US$)','Gross weight (kg)','Mode of Transport Code','Period','Period Desc.', 'Qty','Qty Unit','Alt Qty Unit Code'] cut_i = cut_i[cut_i.columns.difference(to_remove)] cut_i = cut_i.rename(columns={'Reporter ISO':'ISO'}) cut_i.sort_values('Trade Value (US$)', ascending=False,inplace=True) cut_i = cut_i.reset_index(drop=True) cut_i2 = cut_i.loc[:,['Reporter','ISO','Year','Trade Flow','Trade Value (US$)','Netweight (kg)','Alt Qty','Alt Qty Unit','value/kg']] imphtml = cut_i2.to_html(classes="imtable") cap_imphtml = "<h1 id=\"rittai\">Table data of \""+ itemname + "\"(HS:" + com +")" + "[Import]</h1><br><br>" + \ "<caption>" + itemname + " Import Trading Statistics (HScode:" + com + ")</caption>\n" + imphtml title = itemname + "\"(HS:" + com +")" + "World Trade Statistics" #全て結合 kiji_all= titlekiji + exprice + exkgvalue + imprice + imkgvalue + "<p>" + cap_exphtml + "<p>" + cap_imphtml return kiji_all kiji_all = bunsho() title = itemname + "\"(HS:" + com +")" + "World Trade Statistics" wp = Client('http://hstariffstat.com/xmlrpc.php', 'id', 'pass') post = WordPressPost() post.title = title post.content = kiji_all post.terms_names = {'post_tag': [itemname, 'trade statistics'],'category': ['worldwide']} post.post_status = 'publish' wp.call(NewPost(post)) |