Python3 - Remote Server Control to analyze Twitter from iPhone | Apple iOS Xcode YouTube Seminar #26


Python3


- Introduction of Twitter API
- Useful library for python analysis
- learn the basic of python3


iPhone controls remote server to analyze Twitter


* Here is the highlight of YouTube tutorial.

In this tutorial, you can overview how the remote server works with python controlled by iPhone. Twitter API would be one of the examples.

The basic skill is very useful for many purposes so let's learn it with me.

Mac PC already has python2. Please try to output the following letter, 777.


jplet = "777"
print(jplet)

python test.py



If you are not English speaker like me, I would definitely recommend you to use python3 rather than python2 because it would not work well with foreign letter such as Japanese.

After you install it, try the following command to execute python3.


python3 test.py



One of the attractive points about python would be you can import many kinds of library. Here is the example to import CSV.


import csv

with open('data/csvtest.csv') as f:
.reader = csv.reader(f)
.for row in reader:
..print(row)



With this code, you can read the csv file from the designated path and covert the data into the array easily.

As for python grammer, indent is the most important part that you should learn first.


cq = 'app'
if cq == 'apps':
.print( 'Catch Questions' )
else:
.print( 'CQ Academy' )



For loop statement, please be careful about the indent. Only one mistake would not work the whole system. This feature is very different from Swift.


for i in range(5):
.print(i)

strings = ['Catch', 'Questions', 'Academy', 'Cat', 'App']

for string in strings:
.if string == 'Cat':
..print('Cat')
..break
.print(string)



On the other hand, to pass the data you input, the following html code is convenient.


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Twitter Analysis</title>


</head>
<body>
<h1>Twitter Analysis</h1><hr>

<form name="Form" method="POST" action="../myDirectory/twtAnalysis.cgi">
Search: <input type="text" size="30" name="name">
<input type="submit" value="submit" name="button">
</form></body>
</html>



You can post the particular key word for Twitter analysis cgi. To get the information, use the following library and code.


import cgi

form = cgi.FieldStorage()
sentName = form.getvalue('name','')



To analyze twitter, I used the followings;


import cgi
import collections
import MeCab
import codecs
import json
from requests_oauthlib import OAuth1Session
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')



This tutorial is for beginners so at the advanced lesson, I will show you how they work.


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

Back to Table List

2020年02月28日