Browse Source

initial commit

itec78 2 years ago
commit
c3f8d1ad19
3 changed files with 77 additions and 0 deletions
  1. 3 0
      .gitignore
  2. 3 0
      README.md
  3. 71 0
      mastocats.py

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+media/
+
+

+ 3 - 0
README.md

@@ -0,0 +1,3 @@
+# matocats
+Downloads cats pictures from mastodon by server and hashtag
+

+ 71 - 0
mastocats.py

@@ -0,0 +1,71 @@
+#!/usr/bin/env python3
+
+from mastodon import Mastodon
+import os
+import requests
+import datetime
+#import json
+
+
+
+def mastocats(tag = 'mastocats', server = 'mastodon.social', maxres = 10):
+    print("Tag: %s" % tag)
+    print("Server: %s" % server)
+    print("Downloading toots")
+
+    def default(o):
+        if isinstance(o, (datetime.date, datetime.datetime)):
+            return o.isoformat()
+
+    mastodon = Mastodon(api_base_url = "https://" + server)
+    toots = []
+    last_id = None
+
+    while True:
+        statuses = list(mastodon.timeline_hashtag(tag, local=False, max_id=last_id))
+        if not statuses:
+            break
+        if len(toots) >= maxres:
+            break
+        toots += list(map(
+            lambda s: {
+                'id': s['id'],
+                'created': s['created_at'],
+                'media': s['media_attachments'],
+                'account': s['account']['url']
+            }        
+            , statuses))
+        last_id = statuses[-1]['id']  
+        print(len(toots))
+
+    #print(toots)
+    #print(json.dumps(toots, indent=4, default=default))
+
+
+
+    print("Downloading media")
+
+    os.makedirs('media', exist_ok=True)
+
+    for toot in toots:
+        for media in toot['media']:
+            ext = os.path.splitext(media['preview_url'])[1]
+            acc = toot['account'].replace("http://","").replace("https://","").replace("/","_")
+            fname = "|".join([toot['created'].strftime('%Y%m%d-%H%M%S'), acc, str(toot['media'].index(media) + 1)]) + ext
+            img_name = os.path.join('media', fname)
+            
+            if not os.path.isfile(img_name):
+                print(img_name)
+                img_data = requests.get(media['preview_url']).content
+                with open(img_name, 'wb') as handler:
+                    handler.write(img_data)
+
+
+    print("")
+
+
+if __name__ == "__main__":
+    for tag in ['mastocats', 'catsofmastodon', 'cats', 'caturday', 'catstodon']:
+        for server in ['mastodon.social', 'mastodon.bida.im', 'chaos.social']:
+            mastocats(tag, server, 100)
+