Example #1
0
def get_new_tweets(oauth: OAuth1Session, latest: int) -> Tuple[requests.Response, list]:
    q = {"since_id": latest} if latest is not None else {"count": 200}
    req = oauth.get(TIMELINE_EP, params=q)
    if req.status_code != 200:
        return req, []
    tweets = []
    for tw in req.json():
        created_at = datetime.strptime(tw["created_at"], "%a %b %d %H:%M:%S %z %Y").astimezone(timezone("Asia/Tokyo"))
        today = datetime.now(tz=timezone("Asia/Tokyo")).date()
        if created_at.date() == today:
            tweets.append({
                "id": tw["id"],
                "text": tw["text"],
                "hashtags": [tag["text"] for tag in tw["entities"]["hashtags"]],
                "media": [media["media_url_https"] for media in
                          tw.get("extended_entities", dict()).get("media", list())],
                "url": f"https://twitter.com/{tw['user']['screen_name']}/status/{tw['id']}",
                "created_at": {
                    "year": created_at.year,
                    "month": created_at.month,
                    "day": created_at.day,
                    "hour": created_at.hour,
                    "minute": created_at.minute,
                    "second": created_at.second
                }
            })
        else:
            break
    return req, tweets
def get_user_attributes(session: OAuth1Session) -> dict:
    session.fetch_access_token('https://www.openstreetmap.org/oauth/access_token')
    userreq = session.get('https://api.openstreetmap.org/api/0.6/user/details')

    userxml = ET.fromstring(userreq.text)
    userattrs = userxml.find('user').attrib

    attrs = {
        'osm_id': userattrs['id'],
        'display_name': userattrs['display_name']
    }

    home = userxml.find('user/home')
    if home is not None:
        lat = home.attrib.get('lat', None)
        lon = home.attrib.get('lon', None)
        if lat and lon:
            attrs['home_location'] = Point(
                float(lon),
                float(lat)
            )

    return attrs