silo.pasta/Artstation.py

193 lines
5.4 KiB
Python
Raw Normal View History

2018-12-27 19:50:38 +00:00
import os
import glob
import json
import logging
import arrow
import requests
import keys
import common
import settings
from math import ceil
from pprint import pprint
2018-12-27 19:50:38 +00:00
class ASFavs(common.Favs):
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"DNT": "1",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
}
2018-12-27 19:50:38 +00:00
def __init__(self):
super().__init__("artstation")
self.user = keys.artstation.get("username")
self.session = requests.Session()
2018-12-27 19:50:38 +00:00
def paged_likes(self, page=1):
url = "https://www.artstation.com/users/%s/likes.json?page=%s" % (
self.user,
page,
2018-12-27 19:50:38 +00:00
)
js = self.session.get(url, headers=self.headers)
2018-12-27 19:50:38 +00:00
try:
js = js.json()
if "data" not in js:
2018-12-27 19:50:38 +00:00
return None
return js
except Exception as e:
logging.error("fetching artstation failed: %s, response: %s", e, js.text)
2018-12-27 19:50:38 +00:00
return None
@property
def likes(self):
# init Session and it's cookies before doing anything
# FU cloudflare, I'm trying to access my own likes and followings!
url = "https://www.artstation.com/"
self.session.get(url, headers=self.headers)
# now do the real work
2018-12-27 19:50:38 +00:00
js = self.paged_likes()
if not js:
return []
likes = js.get("data", [])
pages = ceil(js.get("total_count", 1) / 50)
2018-12-27 19:50:38 +00:00
while pages > 1:
extras = self.paged_likes()
if not extras:
continue
likes = likes + extras.get("data", [])
2018-12-27 19:50:38 +00:00
pages = pages - 1
return likes
@property
def feeds(self):
feeds = []
url = "https://www.artstation.com/users/%s/following.json" % (self.user)
js = self.session.get(url, headers=self.headers)
2018-12-27 19:50:38 +00:00
try:
js = js.json()
if "data" not in js:
logging.error("fetching artstation follows failed: missing data")
2018-12-27 19:50:38 +00:00
return feeds
for f in js.get("data"):
feeds.append(
{
"text": f.get("username"),
"xmlUrl": "https://www.artstation.com/%s.rss"
% f.get("subdomain"),
"htmlUrl": "https://www.artstation.com/%s" % f.get("subdomain"),
}
)
2018-12-27 19:50:38 +00:00
except Exception as e:
logging.error("parsing artstation follows failed: %s", e)
2018-12-27 19:50:38 +00:00
return feeds
def run(self):
for like in self.likes:
like = ASLike(like, self.session, self.headers)
2018-12-27 19:50:38 +00:00
like.run()
class ASLike(common.ImgFav):
def __init__(self, like, session, headers):
2018-12-27 19:50:38 +00:00
self.like = like
self.session = session
self.headers = headers
2018-12-27 19:50:38 +00:00
def __str__(self):
return "like-of %s" % (self.url)
@property
def url(self):
return self.like.get("permalink")
2018-12-27 19:50:38 +00:00
@property
def data(self):
purl = "%s.json" % (self.url.replace("artwork", "projects"))
data = self.session.get(purl, headers=self.headers)
2018-12-27 19:50:38 +00:00
try:
data = data.json()
except Exception as e:
logging.error("fetching artstation project %s failed: %s", self.url, e)
2018-12-27 19:50:38 +00:00
return None
return data
@property
def author(self):
return {
"name": self.like.get("user").get("username"),
"url": self.like.get("user").get("permalink"),
2018-12-27 19:50:38 +00:00
}
@property
def id(self):
return self.like.get("id")
2018-12-27 19:50:38 +00:00
@property
def content(self):
return "%s" % self.data.get("description_html", "")
2018-12-27 19:50:38 +00:00
@property
def title(self):
title = self.like.get("title")
2018-12-27 19:50:38 +00:00
if not len(title):
title = self.like.get("slug")
2018-12-27 19:50:38 +00:00
if not len(title):
title = common.slugfname(self.url)
return title
@property
def slug(self):
maybe = self.like.get("slug")
2018-12-27 19:50:38 +00:00
if not len(maybe):
maybe = common.slugfname(self.url)
return maybe
@property
def targetprefix(self):
return os.path.join(
settings.paths.get("archive"),
"favorite",
"artstation_%s_%s_%s"
% (
common.slugfname("%s" % self.like.get("user").get("username")),
self.like.get("hash_id"),
self.slug,
),
2018-12-27 19:50:38 +00:00
)
@property
def published(self):
return arrow.get(self.like.get("published_at"))
2018-12-27 19:50:38 +00:00
@property
def tags(self):
t = []
for c in self.data.get("categories"):
t.append(c.get("name"))
2018-12-27 19:50:38 +00:00
return t
@property
def images(self):
r = {}
cntr = 0
for img in self.data.get("assets"):
if img.get("asset_type") != "image":
logging.debug("skipping asset: %s" % img)
2018-12-27 19:50:38 +00:00
continue
f = "%s_%d%s" % (self.targetprefix, cntr, common.TMPFEXT)
r.update({f: img.get("image_url")})
2018-12-27 19:50:38 +00:00
cntr = cntr + 1
return r
if __name__ == "__main__":
2018-12-27 19:50:38 +00:00
t = ASFavs()
t.run()