Python Forum
Reducing JSON character count in Python for a Twitter Bot
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reducing JSON character count in Python for a Twitter Bot
#1
Redacted - Please delete.
Larz60+ write Apr-14-2021, 12:07 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
Adding some more structure here to see if someone can help.

I have made some changes and started over so it may look a little different than previous code.

import tweepy
import time
#from time import sleep | May not need this line if importing entire time lib
import json
import requests
from credentials import *
import schedule

url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'

#access and authorize our Twitter credentials from credentials.py

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

headers = {
    'X-CMC_PRO_API_KEY' : 'redacted',
    'Accepts' : 'application/json',
}

params = {
    'start' : '1',
    'limit' : '10',
    'convert' : 'USD',
}

json = requests.get(url, params=params, headers=headers).json()
print(json)

coins = json['data']
print("Top 10 Crypto by Market Cap")

for coin in coins:
    #del coin['timestamp']['error_code']['credit_count']['notice']['elapsed']['errormessage']['total_count']['data']['id']
    print("Symbol:", coin['symbol'])
    #,"Price:","$",round(coin['quote']['USD']['price'],2))
    coin = str(coins)
    char_count = len(coin)
    print(char_count)
From OP, I'd like to take this output (minus the char count displayed below each):

Top 10 Crypto by Market Cap
Symbol: BTC
9891
Symbol: ETH
9891
Symbol: BNB
9891
Symbol: XRP
9891
Symbol: USDT
9891
Symbol: ADA
9891
Symbol: DOGE
9891
Symbol: DOT
9891
Symbol: UNI
9891
Symbol: LTC
9891

The goal would be to feed this data through the Tweepy API and auto post this information on a time schedule every day. When I feed this through the API, I get a an error that I have exceeded the character count, which is why I added the print to include the char count for each coin.

Something tells me that with each query to the CMC API, it is attaching a ton more data packaged into the output.

The total character count (minus the char counter information) is 149 including spaces, so theoretically it should work.

Do I need to decode the JSON data or delete irrelevant information from the data set before outputting?

I didn't post the error for the tweeps bot because it's irrelevant at this point, but can update if need be.

Also, this program is an overly simplified version of what my ultimate goal for the program is, but this is my starting point.

Thanks in advance for any help you guys can provide.
Reply
#3
Mods, please delete.

I figured it out.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python rule about the space character surrounding the equal sign ineuw 10 1,519 Sep-21-2023, 09:17 AM
Last Post: ineuw
  How do I handle escape character in parameter arguments in Python? JKR 6 1,039 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
  How do I scrape profile information from Twitter People search results? asdad 0 704 Nov-29-2022, 10:25 AM
Last Post: asdad
  Row Count and coloumn count Yegor123 4 1,268 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  Python Split json into separate json based on node value CzarR 1 5,476 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  Is it possible to write a python script to block twitter feeds? cubangt 0 837 Apr-07-2022, 04:14 PM
Last Post: cubangt
  Reducing runtime memory usage in Cpython interpreter david_the_graower 2 2,175 Oct-18-2021, 09:56 PM
Last Post: david_the_graower
  [solved] unexpected character after line continuation character paul18fr 4 3,296 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  python error: bad character range \|-t at position 12 faustineaiden 0 3,646 May-28-2021, 09:38 AM
Last Post: faustineaiden
Photo Creating column in python based on the other colums value count Bartek635 2 2,893 Apr-15-2021, 03:47 PM
Last Post: Bartek635

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020