repl.it
@darianmoody/

discover-twitter-avatar

Python

Given an Twitter Profile URL, or a Twitter username - this tool finds the full size image representing the user's avatar.

fork
loading
Files
  • main.py
  • templates
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import json

import requests
from flask import (
  Flask,
  render_template,
  request,
)
from bs4 import BeautifulSoup

app = Flask(__name__, static_folder='.', root_path='/home/runner')

TWITTER_DOMAIN = "https://twitter.com/"

@app.route('/', methods=["GET", "POST"])
def index():
  input_string = ""
  avatar_url = ""
  if request.method == 'POST':
    url_input = request.form['input_string']
    if url_input.startswith(TWITTER_DOMAIN):
      avatar_url = _get_avatar(input_string)
  return render_template(
      'index.html',
      input_string=input_string,
      avatar_url=avatar_url
    )

def _get_avatar(input_string):
  if input_string.starts_with(TWITTER_DOMAIN):
    url = input_string
  else:
    url = f"{TWITTER_DOMAIN}{input_string}"
  response = requests.get(url, timeout=10)
  if not response.status_code == requests.codes.ok:
    return
    
  soup = BeautifulSoup(response.text, "html.parser")
  image_element = soup.find("img", {"class": "ProfileAvatar-image"})
  return image_element["src"]

if __name__ == '__main__':
    app.run(host='0.0.0.0', port='3000')