[PYTHON] Repeat with While. Scripts to Tweet and search from the terminal

A script that uses OAuth1Session to tweet and search from the terminal.

words = raw_input(u"input-tweet: ") search_words = raw_input(u"Keyword?: ")

Exit the loop if both elements are empty.

Tweeting from the terminal may be surprisingly convenient.

twform.py



#!/user/bin/env python
# -*- coding: utf-8 -*-
from requests_oauthlib import OAuth1Session
import json
import sys, codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)


while True:
	
	words = raw_input(u"input-tweet: ")
	search_words = raw_input(u"Keyword?: ")

	C_KEY = "************************************"
	C_SECRET = "************************************"
	A_KEY = "************************************"
	A_SECRET = "************************************"


	def Post_msg():
		url = "https://api.twitter.com/1.1/statuses/update.json"
		params = {"status": words,"lang": "ja"}
		tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
		req = tw.post(url, params = params)
		if req.status_code == 200:
			print "Success! Your Tweet: ", unicode(words, "utf-8")
		else:
			print req.status_code
		return Post_msg

	def Limit_Status():
		url = "https://api.twitter.com/1.1/application/rate_limit_status.json"
		params = {}
		tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
		req = tw.get(url, params = params)
		if req.status_code == 200:
			limit = req.headers["x-rate-limit-remaining"]
			print ("API remain: " + limit)
		return Limit_Status
	
	def Search_words():
		url = "https://api.twitter.com/1.1/search/tweets.json?"
		params = {
				"q": unicode(search_words, "utf-8"),
				"lang": "ja",
				"result_type": "recent",
				"count": "20"
				}
		tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
		req = tw.get(url, params = params)
		tweets = json.loads(req.text)
		for tweet in tweets["statuses"]:
			print (tweet["created_at"])
			print (tweet["user"]["screen_name"]), (tweet["user"]["name"])
			print (tweet["text"])
			print "=" * 60
			
		return Search_words
		
	if words:
		Post_msg()
		Limit_Status()
	elif search_words:
		Search_words()
		Limit_Status()
	else:
		break

Recommended Posts

Repeat with While. Scripts to Tweet and search from the terminal
Let's tweet by hitting the terminal while screaming with Selenium! !!
Search for Twitter keywords with tweepy and write the results to Excel
I tried to learn the angle from sin and cos with chainer
Script to tweet with multiples of 3 and numbers with 3 !!
How to measure line speed from the terminal
Create a command to search for similar compounds from the target database with RDKit and check the processing time
Perform a Twitter search from Python and try to generate sentences with Markov chains.
Extract images and tables from pdf with python to reduce the burden of reporting
How to instantly launch Jupyter Notebook from the terminal
[pyqtgraph] Add region to the graph and link it with the graph region
Get the package version to register with PyPI from Git
"Deep copy" and "Shallow copy" to understand with the smallest example
Let's search from the procession
I tweeted from the terminal!
I made something with python that NOW LOADING moves from left to right on the terminal
Getting Started with Poetry From installation to execution and version control
[AWS] Search and acquire necessary data from S3 files with S3 Select
How to get followers and followers from python using the Mastodon API
[EC2] How to install and download chromedriver from the command line
Learn Bayesian statistics from the basics to learn the M-H and HMC methods
Terminal association from the server side to Amazon SNS (python + boto3)
The strongest way to use MeCab and CaboCha with Google Colab
To improve the reusability and maintainability of workflows created with Luigi
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
From ubuntu installation to running kinect with docker and ros (overview)
HDA distribution from Houdini to export FBX with hierarchy and transforms
Goodbye to the command prompt in WSL, VSCode and Windows Terminal
From "drawing" to "writing" the configuration diagram: Try drawing the AWS configuration diagram with Diagrams
I compared while reading the documentation to use Jinja2 with Django
Operate Firefox with Selenium from python and save the screen capture
[Python] Try to recognize characters from images with OpenCV and pyocr
Output the report as PDF from DB with Python and automatically attach it to an email and send it
I want to use only the SMTP MAIL FROM command and RCPT TO command without sending mail with Python's smtplib
Create folders from '01' to '12' with python
I liked the tweet with python. ..
Remove and retrieve arrays from fasta according to the ID list file
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Find the inertial spindle and moment of inertia from the inertial tensor with NumPy
Specify the browser to use with Jupyter Notebook. Especially Mac. (And Vivaldi)
Connect to VPN with your smartphone and turn off / on the server
How to write the correct shebang in Perl, Python and Ruby scripts
I tried to express sadness and joy with the stable marriage problem.
How to get the date and time difference in seconds with python
Work memo to migrate and update Python 2 series scripts on the cloud to 3 series
Convert the spreadsheet to CSV and upload it to Cloud Storage with Cloud Functions
Write the result of keyword search with ebaysdk to Google Spread Sheets
No reason to think while writing a crawler with Django and Celery
I measured the speed of list comprehension, for and while with python2.7.
How to search using python's astroquery and get fits images with skyview
[python] Send the image captured from the webcam to the server and save it
How to query BigQuery with Kubeflow Pipelines and save the result and notes
I tried to control the network bandwidth and delay with the tc command
Try to separate the background and moving object of the video with OpenCV