Python3 - Automatically write the description of YouTube with API | Apple iOS Xcode YouTube Seminar #27


Python3


- Introduction of YouTube API
- Useful library for python3
- learn the basic of how to make text automatically


You can automatically write the description of YouTube with Python3


* Here is the highlight of YouTube tutorial.

In this tutorial, it may sounds different from iOS development. But, YouTube API can be used for many purposes including server control that can help you for iOS development.

So, let me show you the basic of python3 via YouTube API usage.

The items of python library are as follows;


import pandas as pd
import csv
import sys



Pandas are very convenient for handling array data and you can easily handle the csv file with it. Sys is also useful because you can send some variables whenever you call the command. Here is the example.


import sys
args = sys.argv
print(str(args[1]))

lineCode = int(args[1])



In this way, you can substitute something for the variable named "lineCode".

How pandas works is as follows;


import pandas as pd

df = pd.read_csv('en-urlList/cq.csv', sep=',')
print(df.head(lineCode))

videos = df.head(lineCode)
videos.to_csv('exen.csv', index=None)



You can read the specific path of csv file with pandas and export it after you edit it.

From YouTube API, you can get the data frame with some urls with the following code.


while True:
 time.sleep(10)
 response = requests.get(url % (API_KEY, CHANNEL_ID))
 if response.status_code != 200:
  print('Error')
  break
 result = response.json()
 infos.extend([
 [item['id']['videoId'], item['snippet']['title'], item['snippet']['publishedAt']]
 for item in result['items'] if item['id']['kind'] == 'youtube#video'
 ])

 if 'nextPageToken' in result.keys():
  if 'pageToken' in url:
   url = url.split('&pageToken')[0]
  url += f'&pageToken={result["nextPageToken"]}'
 else:
  print('Finished')
  break



You can get the variable infos with the data frame of YouTube. You can handle it with pandas.

The way to write the text automatically is here.


s = 'Description' + "\n\n"

with open('exen.csv', 'r') as file:
 reader = csv.reader(file)
 i = 0
 for row in reader:
  i += 1
  if i != 1:
   titleStr = row[1].split('|')
   timeStr = row[2].split('T')
   s += timeStr[0] + "\n" + titleStr[0] +"\n" + "https://youtu.be/" + row[0]
   s += "\n\n"

with open('urlListEn.txt', mode='w') as f:
 f.write(s)



With this code, you can create the description of YouTube for instance. Let me explain how it works on my video tutorial.


To get the source code, check the comment of my YouTube

Back to Table List

2020年03月06日