{"id":294552,"date":"2019-09-11T15:00:25","date_gmt":"2019-09-11T15:00:25","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=294552"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=294552","title":{"rendered":"\u041f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u0448\u0431\u043e\u0440\u0434\u043e\u0432 \u0434\u043b\u044f \u0432\u0435\u0431-\u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0438 e-commerce \u0441\u0430\u0439\u0442\u0430. \u0427\u0430\u0441\u0442\u044c 4: Youtube-\u043a\u0430\u043d\u0430\u043b"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\u041b\u0435\u0433\u043a\u043e \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0440\u0430\u0444\u0438\u043a\u0430 \u043f\u0440\u0438\u0448\u043b\u043e \u0441 \u044e\u0442\u0443\u0431-\u043a\u0430\u043d\u0430\u043b\u0430. \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0437\u0430\u0439\u0442\u0438 \u0432 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u042f\u043d\u0434\u0435\u043a\u0441 \u041c\u0435\u0442\u0440\u0438\u043a\u0438 \u0438\u043b\u0438 Google Analytics. \u0410 \u0432\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0443\u0437\u043d\u0430\u0442\u044c, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u043b\u043e \u0441 \u0432\u0430\u0448\u0438\u043c \u0432\u0438\u0434\u0435\u043e \u043d\u0430 \u043a\u0430\u043d\u0430\u043b\u0435. \u041a\u0442\u043e \u0435\u0433\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043b, \u043a\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0432 \u0444\u0430\u0432\u043e\u0440\u0438\u0442\u044b, \u0430 \u043a\u0442\u043e \u0434\u0438\u0441\u043b\u0430\u0439\u043a\u043d\u0443\u043b. \u0412\u043e\u0442 \u0434\u043b\u044f \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0438 \u0442\u0430\u043a\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043a\u0440\u0438\u043f\u0442 \u043d\u0430 Python. <\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/-v\/b0\/sf\/-vb0sfamlbuklyrfafhmhuzl2d0.png\"><br \/>  <i>\u0414\u0438\u043d\u0430\u043c\u0438\u043a\u0430 Youtube-\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0435\u0439<\/i><br \/>  <a name=\"habracut\"><\/a><br \/>  API Youtube \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043b\u0435\u0439\u043b\u0438\u0441\u0442\u043e\u0432 \u0438 \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u043f\u043b\u0435\u0439\u043b\u0438\u0441\u0442\u0443 \u0432\u044b\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443 \u043f\u043e \u0432\u0438\u0434\u0435\u043e. <\/p>\n<pre><code class=\"python\">YOUTUBE_API_URL = 'https:\/\/www.googleapis.com\/youtube\/v3' YOUTUBE_API_KEY = '...' YOUTUBE_CHANNEL_ID = '...' <\/code><\/pre>\n<p>  \u0418\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043a\u0430\u043d\u0430\u043b\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 \u043a\u043e\u0434\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b. \u041e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u043a\u043b\u044e\u0447\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"https:\/\/developers.google.com\/youtube\/v3\/docs\/\">\u0437\u0434\u0435\u0441\u044c<\/a>. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 report_client \u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0432 \u043d\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b. <\/p>\n<pre><code class=\"python\">class report_client:      def __init__(self, api_url, api_key, proxies):         self._url = api_url         self._key = api_key         self._proxies = proxies              def req_video(self, channel_id = YOUTUBE_CHANNEL_ID):          url = ('{0}\/activities?channelId={1}&amp;key={2}&amp;part=snippet%2CcontentDetails&amp;maxResults=50')         r = requests.get(url.format(self._url, channel_id,self._key)                          , proxies = self._proxies)         parsed = json.loads(r.text)         res_df = pd.DataFrame(columns=['video_id'])                  for i in parsed['items']:             temp = {}             if 'upload' in i['contentDetails']:                 temp['video_id'] = i['contentDetails']['upload']['videoId']                 res_df = res_df.append(temp, ignore_index=True)         return res_df          def req_playlist(self, channel_id = YOUTUBE_CHANNEL_ID):          url = ('{0}\/playlists?channelId={1}&amp;key={2}&amp;part=snippet%2CcontentDetails&amp;maxResults=50')         r = requests.get(url.format(self._url, channel_id, self._key)                          , proxies = self._proxies)         parsed = json.loads(r.text)         res_df = pd.DataFrame(columns=['playlist_id', 'playlist_name'])                  for i in parsed['items']:             temp = {}             temp['playlist_id'] = i['id']             temp['playlist_name'] = i['snippet']['title']             res_df = res_df.append(temp, ignore_index=True)         return res_df          def req_playlist_stat(self, playlist_id, channel_id = YOUTUBE_CHANNEL_ID):         res_df = pd.DataFrame(columns=['video_id', 'playlist_id', 'playlist_name'])         for i,k in playlist_id.iterrows():             url = ('{0}\/playlistItems?playlistId={1}&amp;key={2}&amp;part=snippet&amp;maxResults=50')             r = requests.get(url.format(self._url, k['playlist_id'],self._key)                          , proxies = self._proxies)             parsed = json.loads(r.text)                          for j in parsed['items']:                 temp = {}                 temp['video_id'] = j['snippet']['resourceId']['videoId']                 temp['playlist_id'] = k['playlist_id']                 temp['playlist_name'] = k['playlist_name']                 res_df = res_df.append(temp, ignore_index=True)                              stop = 0                 while 'nextPageToken' in parsed:                 url = ('{0}\/playlistItems?playlistId={1}&amp;key={2}&amp;part=snippet&amp;maxResults=50&amp;pageToken={3}')                 r = requests.get(url.format(self._url, k['playlist_id'],self._key,parsed['nextPageToken'])                          , proxies = self._proxies)                 parsed = json.loads(r.text)                 for j in parsed['items']:                     temp = {}                     temp['video_id'] = j['snippet']['resourceId']['videoId']                     temp['playlist_id'] = k['playlist_id']                     temp['playlist_name'] = k['playlist_name']                     res_df = res_df.append(temp, ignore_index=True)                                  stop = stop + 1                 if stop == 1:                     break         return res_df              def req_stat(self, video_id):         res_df = pd.DataFrame(columns=['video_id','publishedAt','title','description',\\                                            'tags','local_title','viewCount','likeCount',\\                                            'dislikeCount','favoriteCount','commentCount'\\                                       , 'playlist_id', 'playlist_name'])                  for i,k in video_id.iterrows():             url = ('{0}\/videos?id={1}&amp;key={2}&amp;part=snippet,contentDetails,statistics,status&amp;maxResults=50')                      r = requests.get(url.format(self._url, k['video_id'], self._key)                              , proxies = self._proxies)             parsed = json.loads(r.text)             if 'error' in parsed.keys():                 break                         temp = {}             if 'items' in parsed:                 if parsed['items']:                     temp['video_id'] = parsed['items'][0]['id']                     temp['publishedAt'] = parsed['items'][0]['snippet']['publishedAt']                             temp['title'] = parsed['items'][0]['snippet']['title']                     temp['description'] = parsed['items'][0]['snippet']['description']                     temp['playlist_id'] = k['playlist_id']                     temp['playlist_name'] = k['playlist_name']                     if 'tags' in parsed['items'][0]['snippet']:                         temp['tags'] = parsed['items'][0]['snippet']['tags']                        if 'title' in parsed['items'][0]['snippet']['localized']:                         temp['local_title'] = parsed['items'][0]['snippet']['localized']['title']                       if 'viewCount' in parsed['items'][0]['statistics']:                         temp['viewCount'] = parsed['items'][0]['statistics']['viewCount']                     if 'likeCount' in parsed['items'][0]['statistics']:                         temp['likeCount'] = parsed['items'][0]['statistics']['likeCount']                     if 'dislikeCount' in parsed['items'][0]['statistics']:                         temp['dislikeCount'] = parsed['items'][0]['statistics']['dislikeCount']                     if 'favoriteCount' in parsed['items'][0]['statistics']:                         temp['favoriteCount'] = parsed['items'][0]['statistics']['favoriteCount']                     if 'commentCount' in parsed['items'][0]['statistics']:                         temp['commentCount'] = parsed['items'][0]['statistics']['commentCount']                     res_df = res_df.append(temp, ignore_index=True)                      return res_df   <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0438 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0443. <\/p>\n<pre><code class=\"python\">def youtube_reports():      a = report_client(YOUTUBE_API_URL,YOUTUBE_API_KEY,{})          method_rep = getattr(a, \"req_playlist\")     playlist_id = method_rep()     method_rep = getattr(a, \"req_playlist_stat\")     video_id = method_rep(playlist_id)            report_list = [\"req_stat\"]      for rep in report_list:         result_local_file_name = 'df_{0}.csv'.format(rep)                 method_rep = getattr(a, rep)         df = method_rep(video_id)          df.to_csv(r'C:\\\\Users\\\\User\\\\Desktop\\\\youtube.csv',                           index=False,                           header=True,                           sep='\\t',                           quoting = csv.QUOTE_ALL,                           encoding=\"utf-8\")         to_sql_server(df, 'youtube_{0}'.format(rep))                                  youtube_reports() <\/code><\/pre>\n<\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/467035\/\"> https:\/\/habr.com\/ru\/post\/467035\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\u041b\u0435\u0433\u043a\u043e \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0440\u0430\u0444\u0438\u043a\u0430 \u043f\u0440\u0438\u0448\u043b\u043e \u0441 \u044e\u0442\u0443\u0431-\u043a\u0430\u043d\u0430\u043b\u0430. \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0437\u0430\u0439\u0442\u0438 \u0432 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u042f\u043d\u0434\u0435\u043a\u0441 \u041c\u0435\u0442\u0440\u0438\u043a\u0438 \u0438\u043b\u0438 Google Analytics. \u0410 \u0432\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0443\u0437\u043d\u0430\u0442\u044c, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u043b\u043e \u0441 \u0432\u0430\u0448\u0438\u043c \u0432\u0438\u0434\u0435\u043e \u043d\u0430 \u043a\u0430\u043d\u0430\u043b\u0435. \u041a\u0442\u043e \u0435\u0433\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043b, \u043a\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0432 \u0444\u0430\u0432\u043e\u0440\u0438\u0442\u044b, \u0430 \u043a\u0442\u043e \u0434\u0438\u0441\u043b\u0430\u0439\u043a\u043d\u0443\u043b. \u0412\u043e\u0442 \u0434\u043b\u044f \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0438 \u0442\u0430\u043a\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043a\u0440\u0438\u043f\u0442 \u043d\u0430 Python. <\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/-v\/b0\/sf\/-vb0sfamlbuklyrfafhmhuzl2d0.png\"><br \/>  <i>\u0414\u0438\u043d\u0430\u043c\u0438\u043a\u0430 Youtube-\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0435\u0439<\/i>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-294552","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/294552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=294552"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/294552\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=294552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=294552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=294552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}