Advertisement
Guest User

monitor-twitter-bio-update.py

a guest
Oct 22nd, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. from datetime import datetime
  2. import json
  3. import os
  4. import twint
  5. import uuid
  6. import firebase_admin
  7. from firebase_admin import firestore
  8. import tempfile
  9. from werkzeug.utils import secure_filename
  10. import requests
  11.  
  12.  
  13. def get_file_path(filename):
  14.     file_name = secure_filename(filename)
  15.     return os.path.join(tempfile.gettempdir(), file_name)
  16.  
  17.  
  18. def did_update(a, b):
  19.     r = True
  20.     if a["id"] != b["id"]:
  21.         return r
  22.     if a["name"] != b["name"]:
  23.         return r
  24.     if a["username"] != b["username"]:
  25.         return r
  26.     if a["bio"] != b["bio"]:
  27.         return r
  28.     if a["location"] != b["location"]:
  29.         return r
  30.     if a["url"] != b["url"]:
  31.         return r
  32.     if a["private"] != b["private"]:
  33.         return r
  34.     if a["profile_image_url"] != b["profile_image_url"]:
  35.         return r
  36.     if a["background_image"] != b["background_image"]:
  37.         return r
  38.     return False
  39.  
  40.  
  41. def main(request):
  42.     tmp_json = get_file_path("o.json")
  43.  
  44.     try:
  45.         os.remove(tmp_json)
  46.     except OSError:
  47.         pass
  48.  
  49.     # 現在のbioを取得
  50.     c = twint.Config()
  51.     c.Username = ""
  52.     c.Store_json = True
  53.     c.Output = tmp_json
  54.     twint.run.Lookup(c)
  55.  
  56.     # 直前と比較
  57.     with open(tmp_json) as f:
  58.         current_bio = json.load(f)
  59.  
  60.         if len(firebase_admin._apps) == 0:
  61.             default_app = firebase_admin.initialize_app()
  62.         db = firestore.client()
  63.  
  64.         # 直前のbioを取得
  65.         latest_ref = db.collection("histories").order_by(
  66.             "last_update", direction=firestore.Query.DESCENDING).limit(1).stream()
  67.         for l in latest_ref:
  68.             latest_bio = l.to_dict()
  69.  
  70.         # 差異があれば通知を投げる
  71.         if did_update(latest_bio, current_bio):
  72.             payload = {
  73.                 "id": current_bio["id"],
  74.                 "name": current_bio["name"],
  75.                 "username": current_bio["username"],
  76.                 "bio": current_bio["bio"],
  77.                 "location": current_bio["location"],
  78.                 "url": current_bio["url"],
  79.                 "private": current_bio["private"],
  80.                 "profile_image_url": current_bio["profile_image_url"],
  81.                 "background_image": current_bio["background_image"],
  82.                 "last_update": datetime.now()
  83.             }
  84.  
  85.             # 履歴を更新
  86.             doc_ref = db.collection("histories").document(str(uuid.uuid4()))
  87.             doc_ref.set(payload)
  88.  
  89.             # Discordに通知
  90.             webhook_url = ""
  91.             payload.pop("last_update")
  92.             x = {
  93.                 "content": json.dumps(payload, ensure_ascii=False, indent=4)
  94.             }
  95.             requests.post(webhook_url, headers={
  96.                           'Content-Type': 'application/json'}, data=json.dumps(x))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement