[GO] [Python + Bottle] I tried to release a web service that visualizes Twitter's positioned tweets.

It's May, but it's a story about releasing a web service that visualizes Twitter's positioned tweets. I want to get rid of this sexual root that burns out when I can make what I want to make. It is being played with acclaim.

It's made from a bottle, but it's extremely simple. After all, the duplication remains the same, and the time is not in Japan time. This is a good example of burning out the details as they are.

By the way, for API, of course, use GET search / tweets. Attempts to optionally collect nearby data with the geocode option. If you search nationwide, the top 100 keywords will be searched in the search, so you may not see ranked tweets in the search results. In that case, there is no choice but to display an error page where nothing is displayed.

There may be other good ways, but I couldn't think of that much.

Process by pulling location information from the browser. Then, if you have the option to look for a neighborhood, the distance

geocoding = send_geo + "," + near

Combine with and pass.

@route("/show", method="GET")
def send_geos():
	send_geo = request.query.send_geo
	search_words = request.query.search_words
	near = request.query.near
	geocoding = send_geo + "," + near
	
	
	if search_words:
		C_KEY = "****************"
		C_SECRET = "****************"
		A_KEY = "****************"
		A_SECRET = "****************"

		url = "https://api.twitter.com/1.1/search/tweets.json?"
		
		if near:
			params = {
			"q": (search_words, "utf-8"),
			"lang": "ja",
			"geocode": (geocoding),
			"result_type": "mixed",
			"count": "100"
			}		
			tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
			req = tw.get(url, params = params)
			tweets = json.loads(req.text)
			dic = tweets
		else:
			params = {
			"q": (search_words, "utf-8"),
			"lang": "ja",
			"result_type": "mixed",
			"count": "100"
			}		
			tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)
			req = tw.get(url, params = params)
			tweets = json.loads(req.text)
			dic = tweets

If there is no positioned tweet, a problem will occur, so try to escape there. If it is not positioned, use except to perform another process.


		if req.status_code == 200:
			for tweet in tweets["statuses"]:
				created_at = YmdHMS(tweet["created_at"])
				User = (tweet["user"]["screen_name"].encode("utf-8"))
				U_Name = (tweet["user"]["name"].encode("utf-8"))
				U_img = (tweet["user"]["profile_image_url"])
				Text = (tweet["text"].encode("utf-8"))
				if "http" in "Text":
					Text = Text.split("http", 1)[0]
					Text = Text.split("@")[0]
					Text = Text.split("RT")[0]


				try:
					Place = (tweet["place"]["bounding_box"]["coordinates"])
					
					flat_list = []
					for e in Place:
						flat_list.extend(e)
    	
					flat_list2 = []
					for b in flat_list:
						flat_list2.extend(b)
 
					flat_list3 = []
					for c in flat_list:
						flat_list3.extend(c)
    	
					geolng = flat_list3[0]
					geolat = flat_list3[1]

Geocode is (tweet ["place"] ["bounding_box"] ["coordinates"]) I'm going to get it, but if I do this, the nesting is terrible, so I'm canceling the nesting.

This is the basic Python in-script processing. The rest is written directly in the tpl file.

The process of writing Python in tpl as follows. With this, all tweets are displayed properly.

Tweet data is pulled from what is stored in the database. For example, saving the data as plain text.

%for line in f:			
%	line = line.rstrip()
%	l = line.split(",")
%	User = l[0]
%	U_Name = l[1]
%	U_img = l[2]
%	Text = l[3]
%	created_at = l[-3]
%geolng = l[-1]
%geolat = l[-2]
<li class="twlist">
	<font size="2" color="#d8d8d8">{{created_at}}・{{geolat}},{{geolng}}</font><br>
	<span class="fleft"><a href="https://twitter.com/{{User}}" target="_blank"><img src="{{U_img}}"></a></span>
	<b>{{U_Name}}</b> <font size="2"><a href="https://twitter.com/{{User}}" target="_blank">@{{User}}</a></font><br>{{Text}}<br><hr class="clear">
</li>
%end
<ul>

When writing to tpl, it can be processed even if the indent is ignored. If you export it as it is, the order will be out of order, so it will be formatted with Jquery.

<script type="text/javascript">
$(function () {
  $('ul').html(
    $('li').sort(function(a, b) {
      if ($(a).text() < $(b).text()) {
        return 1;
      } else {
        return -1;
      }
    })
  );
});
</script>

The same is true for plotting on a map, and Python is written in Javascript for Google map.

%for line in f:			
%	line = line.rstrip()
%	l = line.split(",")
%	User = l[0]
%	U_Name = l[1]
%	U_img = l[2]
%	Text = l[3]
%	created_at = l[-3]
%geolng = l[-1]
%geolat = l[-2]
              ['<a href=https://twitter.com/{{User}} target="_blank"><img src={{U_img}}></a><br>{{U_Name}}:{{User}}<br>{{created_at}}<br><marquee>{{Text}}</marquee>', {{geolat}}, {{geolng}}],
              
%end 
              ['You', {{default_tag}}]

{{User}} Something is a variable that was returned template in bottle. A single Python script processes about 200 lines.

So, when I tried running it on the Web, it looked like this.

http://www.geo-twit.com/

use? I wonder if there is ...

Recommended Posts

[Python + Bottle] I tried to release a web service that visualizes Twitter's positioned tweets.
I tried to make a system that fetches only deleted tweets
[Python] A memo that I tried to get started with asyncio
I tried to make a todo application using bottle with python
I tried to make a Web API
I made a web application in Python that converts Markdown to HTML
I tried to develop a Formatter that outputs Python logs in JSON
I tried to create a sample to access Salesforce using Python and Bottle
I tried to create a class that can easily serialize Json in Python
[ES Lab] I tried to develop a WEB application with Python and Flask ②
[Python] I tried to make a Shiritori AI that enhances vocabulary through battles
I tried to draw a route map with Python
I tried to implement a pseudo pachislot in Python
I tried to automatically generate a password with Python3
I tried to implement a one-dimensional cellular automaton in Python
[Markov chain] I tried to read a quote into Python.
I tried "a program that removes duplicate statements in Python"
I tried "How to get a method decorated in Python"
[Python] I tried to visualize tweets about Corona with WordCloud
I tried to make a stopwatch using tkinter in python
[1 hour challenge] I tried to make a fortune-telling site that is too suitable with Python
I made a program to collect images in tweets that I liked on twitter with Python
I tried to make a generator that generates a C # container class from CSV with Python
I tried to make a bot that randomly acquires Wikipedia articles and tweets once a day
I tried to build a service that sells machine-learned data at explosive speed with Docker
[5th] I tried to make a certain authenticator-like tool with python
I tried to create a server environment that runs on Windows 10
I want to use a wildcard that I want to shell with Python remove
I tried to convert a Python file to EXE (Recursion error supported)
Rubyist tried to make a simple API with Python + bottle + MySQL
I tried to touch Python (installation)
[Graduation from article scattering] I tried to develop a service that can list articles by purpose
[2nd] I tried to make a certain authenticator-like tool with python
I tried to make a regular expression of "amount" using Python
[Python] I tried to implement stable sorting, so make a note
I tried web scraping with python.
I tried to make a regular expression of "time" using Python
[3rd] I tried to make a certain authenticator-like tool with python
I tried to create a list of prime numbers with python
I tried to make a regular expression of "date" using Python
I tried to implement a misunderstood prisoner's dilemma game in Python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I wrote a script to extract a web page link in Python
[4th] I tried to make a certain authenticator-like tool with python
[Python] I tried to make a simple program that works on the command line using argparse.
A story that didn't work when I tried to log in with the Python requests module
I made a scaffolding tool for the Python web framework Bottle
[1st] I tried to make a certain authenticator-like tool with python
I made a library that adds docstring to a Python stub file.
Python: I tried to make a flat / flat_map just right with a generator
I tried to communicate with a remote server by Socket communication with Python.
I tried to implement a blockchain that actually works with about 170 lines
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
Mayungo's Python Learning Episode 6: I tried to convert a character string to a number
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to discriminate a 6-digit number with a number discrimination application made with python
[Outlook] I tried to automatically create a daily report email with Python
I tried to build a Mac Python development environment with pythonz + direnv
Start a web server using Bottle and Flask (I also tried using Apache)
I tried to make a skill that Alexa will return as cold