mogupyogu
Posts: 4
Joined: Mon Feb 03, 2014 8:35 pm

Internet of Thing Printer

Mon Feb 03, 2014 8:59 pm

Hi Everyone,

I've recently purchased an "Internet of Things" - Raspberry Pi edition - from AdaFruit to experiment with and learn on. This isn't my first RPi but it is my first real foray into Python. I've been working on a piece of sample sample code that they include in order to help get you started using the device and I've hit a snag that I think should be very easy to fix. Unfortunately, my lack of experience with Python is keeping me from progressing past this spot and I was wondering if anyone here could help me out?

The sample code is for pulling updates from certain Twitter accounts and printing them on a miniature receipt printer. Everything with the printer is working fine. What I need help with is being able to pull more than one account's updates. Here's the sample code I'm working with:

# queryString can be any valid Twitter API search string, including
# boolean operators. See http://dev.twitter.com/docs/using-search
# for options and syntax. Funny characters do NOT need to be URL
# encoded here -- urllib takes care of that.
queryString = 'from:TheTweetOfGod'

I've gone thru the website listed in the comments but I'm still not able to figure out how to add more than one account. Basically, "TheTweetOfGod" is the only one I can view unless I change it to something else. I want to be able to add several. I've tried using things like:

queryString = 'from:TheTweetOfGod'
queryString = 'from:TheOnion'
queryString = 'from:TFLN'

But the only one that prints is the last one. I've tried adding boolean operators like queryString = 'from:TheTweetOfGod'ORqueryString = 'from:TheOnion'OR....etc
But then I get errors regarding the syntax.

I know this is pretty first-rate stuff and I'm missing something very simple. If anyone can point me in the right direction I would really appreciate it.

Thank you.

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Internet of Thing Printer

Mon Feb 03, 2014 11:37 pm

I don't really have a clue about the working details of what you are trying to do but the example you give will jettison whatever was in queryString and replace it with the new tweet. To get a list of strings you would need to do something like

Code: Select all

queryStrings = []
sources = ['TheTweetOfGod', 'TheOnion', 'TFLN',... etc ]
for source in sources:
  queryStrings.append('from:' + source)
queryStrings will then be a list that you can iterate through as I do with sources above. Hope that helps
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

mogupyogu
Posts: 4
Joined: Mon Feb 03, 2014 8:35 pm

Re: Internet of Thing Printer

Mon Feb 03, 2014 11:43 pm

AH! That makes SO much sense! Thank you so much for your response, paddyg. I've been fighting with this for days and you're the first person to give me something I can actually use. Again, thank you for your help. This has been driving me crazy! :lol:

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Internet of Thing Printer

Mon Feb 03, 2014 11:45 pm

Before you go anywhere update your code
https://github.com/adafruit/Python-Thermal-Printer

Twitter changed their API so that you MUST use https (http with ssl) for any interaction with twitter. (That broke my perl code when they implemented that without warning, it also broke @Big_Ben_Clock.)
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

mogupyogu
Posts: 4
Joined: Mon Feb 03, 2014 8:35 pm

Re: Internet of Thing Printer

Mon Feb 03, 2014 11:56 pm

Thanks for the heads up, Dougie. I actually pulled my code from GitHub when I first set this up. It should be the most current but I'll definitely check it and make sure. I was actually following your post regarding "main.py" on the adafruit forums. Without knowing, you actually pointed me in the right direction for a lot of the issues I was experiencing early on.

mogupyogu
Posts: 4
Joined: Mon Feb 03, 2014 8:35 pm

Re: Internet of Thing Printer

Tue Feb 04, 2014 12:24 am

I think I'm still missing something. When using

Code: Select all

queryStrings = []
sources = ['TheTweetOfGod', 'TheOnion', 'TFLN' ]
for source in sources:
  queryStrings.append('from:' + source)
I get an error:
Traceback (most recent call last):
File "twitter.py", line 50, in <module>
queryStrings.append('from:' + sources)
TypeError: cannot concatenate 'str' and 'list' objects
I'm really sorry for my ignorance here. It seems like the code is right. Is it because the indentation is wrong? I know Python is really picky about that.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Internet of Thing Printer

Tue Feb 04, 2014 8:18 am

The error message suggests your actual code is not the same as you've posted here. It says you're trying to append "sources" (which is the original list) rather than "source" which is the individual items in the list that you're looping through.

Just check your actual code again and you should be fine. Ie make sure the code says

Code: Select all

for source in sources:
  queryStrings.append('from:' + source)
and not

Code: Select all

for source in sources:
  queryStrings.append('from:' + sources)
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Internet of Thing Printer

Tue Feb 04, 2014 9:33 am

My poor choice of variables! It's a good example of how to confuse yourself and why thinking of appropriate names is worthwhile. Also it's probably worth getting used to the format method (python's equivalent to sprintf) for assembling strings,.

Code: Select all

for s in sources:
  queryString.append("from:{}".format(s))
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”